edge-connect-client/cmd/root.go
Richard Robert Reitz a42d4af5d1
Some checks failed
test / test (push) Failing after 59s
feat(config): add API version selector for v1 and v2
Add configurable API version selection with three methods:
- Config file: api_version: "v1" or "v2" in .edge-connect.yaml
- CLI flag: --api-version v1/v2
- Environment variable: EDGE_CONNECT_API_VERSION=v1/v2

Changes:
- Update root.go to add api_version config and env var support
- Update app.go and instance.go to support both v1 and v2 clients
- Add example config file with api_version documentation
- Default to v2 for backward compatibility
- Apply command always uses v2 (advanced feature)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 13:41:50 +02:00

78 lines
2.3 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
baseURL string
username string
password string
debug bool
apiVersion string
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "edge-connect",
Short: "A CLI tool for managing Edge Connect applications",
Long: `edge-connect is a command line interface for managing Edge Connect applications
and their instances. It provides functionality to create, show, list, and delete
applications and application instances.`,
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.edge-connect.yaml)")
rootCmd.PersistentFlags().StringVar(&baseURL, "base-url", "", "base URL for the Edge Connect API")
rootCmd.PersistentFlags().StringVar(&username, "username", "", "username for authentication")
rootCmd.PersistentFlags().StringVar(&password, "password", "", "password for authentication")
rootCmd.PersistentFlags().StringVar(&apiVersion, "api-version", "v2", "API version to use (v1 or v2)")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug logging")
viper.BindPFlag("base_url", rootCmd.PersistentFlags().Lookup("base-url"))
viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
viper.BindPFlag("api_version", rootCmd.PersistentFlags().Lookup("api-version"))
}
func initConfig() {
viper.AutomaticEnv()
viper.SetEnvPrefix("EDGE_CONNECT")
viper.BindEnv("base_url", "EDGE_CONNECT_BASE_URL")
viper.BindEnv("username", "EDGE_CONNECT_USERNAME")
viper.BindEnv("password", "EDGE_CONNECT_PASSWORD")
viper.BindEnv("api_version", "EDGE_CONNECT_API_VERSION")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".edge-connect")
}
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}