in internal/onetime/configure/configure.go [38:103]
func NewCommand(lp log.Parameters) *cobra.Command {
cfg := cliconfig.NewConfigure(configPath(runtime.GOOS), lp, nil, nil)
configureCmd := &cobra.Command{
Use: "configure",
Short: "Configure the Google Cloud Agent for Compute Workloads",
// PersistentPreRunE is called before each cli command is run.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
onetime.SetValues("google-cloud-workload-agent", &cfg.Lp, cmd, "configure")
if cfg.Lp.CloudLoggingClient != nil {
defer cfg.Lp.CloudLoggingClient.Close()
}
log.SetupLoggingForOTE("google-cloud-workload-agent", "configure", cfg.Lp)
var err error
cfg.Configuration, err = configuration.ConfigFromFile(configPath(runtime.GOOS), os.ReadFile)
if err != nil {
return fmt.Errorf("failed to load configuration: %w", err)
}
return nil
},
// PersistentPostRunE is called after each cli command is run.
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if !cfg.IsConfigModified() {
cfg.LogToBoth(cmd.Context(), "No configuration changes to save.")
return nil
}
return cfg.WriteFile(cmd.Context())
},
}
// Custom Usage Function
configureCmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Printf("Usage for %s:\n\n", cmd.Name())
fmt.Printf(" %s [subcommand] [flags]\n\n", cmd.CommandPath()) // Dynamic command path
// Manually print the rest, mimicking Cobra's default behavior
if cmd.HasAvailableSubCommands() {
fmt.Println("Available Subcommands:")
for _, subCmd := range cmd.Commands() {
if !subCmd.Hidden && subCmd.Name() != "help" { // Avoid showing the default 'help'
fmt.Printf(" %-15s %s\n", subCmd.Use, subCmd.Short)
}
}
fmt.Println()
}
if cmd.HasAvailableLocalFlags() {
fmt.Println("Flags:")
fmt.Println(cmd.LocalFlags().FlagUsages()) // Use Cobra's built-in flag formatting
}
if cmd.HasAvailableInheritedFlags() {
fmt.Println("Global Flags:")
fmt.Println(cmd.InheritedFlags().FlagUsages())
}
return nil
})
configureCmd.AddCommand(oracle.NewCommand(cfg))
configureCmd.AddCommand(sqlserver.NewCommand(cfg))
configureCmd.AddCommand(mysql.NewCommand(cfg))
configureCmd.AddCommand(redis.NewCommand(cfg))
return configureCmd
}