Przeglądaj źródła

Add the ability to pass custom commands to cmd.Execute

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt 3 lat temu
rodzic
commit
745d41667e
2 zmienionych plików z 6 dodań i 13 usunięć
  1. 1 1
      pkg/cmd/agent/agent.go
  2. 5 12
      pkg/cmd/commands.go

+ 1 - 1
pkg/cmd/agent/agent.go

@@ -195,7 +195,7 @@ func Execute(opts *AgentOpts) error {
 
 	var clusterInfoProvider clusters.ClusterInfoProvider
 	if env.IsExportClusterInfoEnabled() {
-		clusterInfoConf := confManager.ConfigFileAt(path.Join(configPrefix, " cluster-info.json"))
+		clusterInfoConf := confManager.ConfigFileAt(path.Join(configPrefix, "cluster-info.json"))
 		clusterInfoProvider = costmodel.NewClusterInfoWriteOnRequest(localClusterInfo, clusterInfoConf)
 	} else {
 		clusterInfoProvider = localClusterInfo

+ 5 - 12
pkg/cmd/commands.go

@@ -31,26 +31,19 @@ const (
 // then the respective defaults from opencost will be used.
 //
 // Any additional commands passed in will be added to the root command.
-func Execute(costModelCmd *cobra.Command, agentCmd *cobra.Command, cmds ...*cobra.Command) error {
+func Execute(costModelCmd *cobra.Command, cmds ...*cobra.Command) error {
 	// use the open-source cost-model if a command is not provided
 	if costModelCmd == nil {
 		costModelCmd = newCostModelCommand()
 	}
 
-	// use the open-source agent if a command is not provided
-	if agentCmd == nil {
-		agentCmd = newAgentCommand()
-	}
-
 	// validate the commands being passed
 	if err := validate(costModelCmd, CommandCostModel); err != nil {
 		return err
 	}
-	if err := validate(agentCmd, CommandAgent); err != nil {
-		return err
-	}
 
-	rootCmd := newRootCommand(costModelCmd, agentCmd, cmds...)
+	// prepend the -agent command and create a new root command
+	rootCmd := newRootCommand(costModelCmd, cmds...)
 
 	// in the event that no directive/command is passed, we want to default to using the cost-model command
 	// cobra doesn't provide a way within the API to do this, so we'll prepend the command if it is omitted.
@@ -70,7 +63,7 @@ func Execute(costModelCmd *cobra.Command, agentCmd *cobra.Command, cmds ...*cobr
 
 // newRootCommand creates a new root command which will act as a sub-command router for the
 // cost-model application.
-func newRootCommand(costModelCmd *cobra.Command, agentCmd *cobra.Command, cmds ...*cobra.Command) *cobra.Command {
+func newRootCommand(costModelCmd *cobra.Command, cmds ...*cobra.Command) *cobra.Command {
 	cmd := &cobra.Command{
 		Use:          commandRoot,
 		SilenceUsage: true,
@@ -94,7 +87,7 @@ func newRootCommand(costModelCmd *cobra.Command, agentCmd *cobra.Command, cmds .
 	cmd.AddCommand(
 		append([]*cobra.Command{
 			costModelCmd,
-			agentCmd,
+			newAgentCommand(),
 		}, cmds...)...,
 	)