Skip to content

Commit

Permalink
unify server flag handling across commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin committed Nov 15, 2023
1 parent b633398 commit 0579f36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
11 changes: 11 additions & 0 deletions cmd/collect/collect.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package collect

import (
"log"

"github.com/metal-toolbox/mctl/cmd"
"github.com/spf13/cobra"
)

var serverIDStr string

var collect = &cobra.Command{
Use: "collect",
Short: "Collect current server firmware status and bios configuration",
Expand All @@ -16,4 +20,11 @@ var collect = &cobra.Command{

func init() {
cmd.RootCmd.AddCommand(collect)

pflags := collect.PersistentFlags()
pflags.StringVarP(&serverIDStr, "server", "s", "", "server id (typically a UUID)")

if err := collect.MarkPersistentFlagRequired("server"); err != nil {
log.Fatalf("marking server flag as required: %s", err.Error())
}
}
17 changes: 7 additions & 10 deletions cmd/collect/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var (
)

var collectInventoryCmd = &cobra.Command{
Use: "inventory",
Use: "inventory --server | -s <server uuid>",
Short: "Collect current server firmware status and bios configuration",
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
collectInventory(cmd.Context())

},
Expand All @@ -38,7 +38,7 @@ var collectInventoryCmd = &cobra.Command{
func collectInventory(ctx context.Context) {
theApp := mctl.MustCreateApp(ctx)

serverID, err := uuid.Parse(flagsDefinedCollectInventory.serverID)
serverID, err := uuid.Parse(serverIDStr)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -81,11 +81,8 @@ func init() {
flagsDefinedCollectInventory = &collectInventoryFlags{}

collect.AddCommand(collectInventoryCmd)
collectInventoryCmd.PersistentFlags().StringVar(&flagsDefinedCollectInventory.serverID, "server", "", "server UUID")
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipFirmwareStatusCollect, "skip-fw-status", false, "Skip firmware status data collection")
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipBiosConfigCollect, "skip-bios-config", false, "Skip BIOS configuration data collection")

if err := collectInventoryCmd.MarkPersistentFlagRequired("server"); err != nil {
log.Fatal(err)
}
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipFirmwareStatusCollect,
"skip-fw-status", false, "Skip firmware status data collection")
collectInventoryCmd.PersistentFlags().BoolVar(&flagsDefinedCollectInventory.skipBiosConfigCollect,
"skip-bios-config", false, "Skip BIOS configuration data collection")
}
9 changes: 0 additions & 9 deletions cmd/collect/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
rctypes "github.com/metal-toolbox/rivets/condition"
)

var serverIDStr string

var inventoryStatus = &cobra.Command{
Use: "status --server | -s <server uuid>",
Short: "check the progress of a inventory collection for a server",
Expand Down Expand Up @@ -53,12 +51,5 @@ func statusCheck(ctx context.Context) {
}

func init() {
flags := inventoryStatus.Flags()
flags.StringVarP(&serverIDStr, "server", "s", "", "server id (typically a UUID)")

if err := inventoryStatus.MarkFlagRequired("server"); err != nil {
log.Fatalf("marking server flag as required: %s", err.Error())
}

collect.AddCommand(inventoryStatus)
}

0 comments on commit 0579f36

Please sign in to comment.