Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Map of all the possible configuration options that we expect viper to handle
var confOptions = struct {
authToken, controlplaneAPI, CASAPI, controlplaneCA, CASCA, insecure, organization *confOpt
authToken, controlplaneAPI, CASAPI, controlplaneCA, CASCA, insecure, organization, platformAPI *confOpt
}{
insecure: &confOpt{
viperKey: "api-insecure",
Expand Down Expand Up @@ -54,6 +54,10 @@ var confOptions = struct {
viperKey: "organization",
flagName: "org",
},
platformAPI: &confOpt{
viperKey: "platform.API",
flagName: "platform",
},
}

type confOpt struct {
Expand Down
42 changes: 41 additions & 1 deletion app/cli/cmd/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,19 @@ func createPluginCommand(_ *cobra.Command, plugin *plugins.LoadedPlugin, cmdInfo
Token: viper.GetString(confOptions.authToken.viperKey),
}

// Create platform configuration from viper
platformConfig := plugins.PlatformConfig{
API: viper.GetString(confOptions.platformAPI.viperKey),
}

// Create plugin configuration with command, arguments, and flags
config := plugins.PluginExecConfig{
Command: cmdInfo.Name,
ParentCommand: cmdInfo.ParentCommand,
Args: args,
Flags: flags,
ChainloopConfig: cliConfig,
PlatformConfig: platformConfig,
}

// execute plugin command using the action pattern
Expand Down Expand Up @@ -277,6 +284,27 @@ func newPluginInstallCmd() *cobra.Command {
return cmd
}

// findCommand recursively searches for a command by name in the command tree
func findCommand(rootCmd *cobra.Command, name string) *cobra.Command {
// Check if the root command itself matches
if rootCmd.Name() == name {
return rootCmd
}

// Search through all subcommands recursively
for _, cmd := range rootCmd.Commands() {
if cmd.Name() == name {
return cmd
}
// Recursively search in subcommands
if found := findCommand(cmd, name); found != nil {
return found
}
}

return nil
}

// loadAllPlugins loads all plugins and registers their commands to the root command
func loadAllPlugins(rootCmd *cobra.Command) error {
ctx := rootCmd.Context()
Expand All @@ -301,7 +329,19 @@ func loadAllPlugins(rootCmd *cobra.Command) error {
}

pluginCmd := createPluginCommand(rootCmd, plugin, cmdInfo)
rootCmd.AddCommand(pluginCmd)

// If ParentCommand is specified, try to find the parent and add as subcommand otherwise add as top-level command
if cmdInfo.ParentCommand != "" {
parentCmd := findCommand(rootCmd, cmdInfo.ParentCommand)
if parentCmd == nil {
return fmt.Errorf("parent command '%s' not found for plugin command '%s' from plugin '%s'",
cmdInfo.ParentCommand, cmdInfo.Name, pluginName)
}
parentCmd.AddCommand(pluginCmd)
} else {
rootCmd.AddCommand(pluginCmd)
}

registeredCommands[cmdInfo.Name] = pluginName
}
}
Expand Down
24 changes: 15 additions & 9 deletions app/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ import (
)

var (
flagCfgFile string
flagDebug bool
flagOutputFormat string
actionOpts *action.ActionsOpts
logger zerolog.Logger
defaultCPAPI = "api.cp.chainloop.dev:443"
defaultCASAPI = "api.cas.chainloop.dev:443"
apiToken string
flagYes bool
flagCfgFile string
flagDebug bool
flagOutputFormat string
actionOpts *action.ActionsOpts
logger zerolog.Logger
defaultCPAPI = "api.cp.chainloop.dev:443"
defaultCASAPI = "api.cas.chainloop.dev:443"
defaultPlatformAPI = "api.app.chainloop.dev:443"
apiToken string
flagYes bool
)

const (
Expand Down Expand Up @@ -223,6 +224,11 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
cobra.CheckErr(viper.BindPFlag(confOptions.CASCA.viperKey, rootCmd.PersistentFlags().Lookup(confOptions.CASCA.flagName)))
cobra.CheckErr(viper.BindEnv(confOptions.CASCA.viperKey, calculateEnvVarName(confOptions.CASCA.viperKey)))

// Platform API configuration
rootCmd.PersistentFlags().String(confOptions.platformAPI.flagName, defaultPlatformAPI, fmt.Sprintf("URL for the Platform API ($%s)", calculateEnvVarName(confOptions.platformAPI.viperKey)))
cobra.CheckErr(viper.BindPFlag(confOptions.platformAPI.viperKey, rootCmd.PersistentFlags().Lookup(confOptions.platformAPI.flagName)))
cobra.CheckErr(viper.BindEnv(confOptions.platformAPI.viperKey, calculateEnvVarName(confOptions.platformAPI.viperKey)))

rootCmd.PersistentFlags().BoolP("insecure", "i", false, fmt.Sprintf("Skip TLS transport during connection to the control plane ($%s)", calculateEnvVarName(confOptions.insecure.viperKey)))
cobra.CheckErr(viper.BindPFlag(confOptions.insecure.viperKey, rootCmd.PersistentFlags().Lookup("insecure")))
cobra.CheckErr(viper.BindEnv(confOptions.insecure.viperKey, calculateEnvVarName(confOptions.insecure.viperKey)))
Expand Down
Loading
Loading