87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/wild-cloud/wild-cli/internal/config"
|
|
"github.com/wild-cloud/wild-cli/internal/environment"
|
|
)
|
|
|
|
var checkMode bool
|
|
|
|
func newGetCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "get <path>",
|
|
Short: "Get a configuration value",
|
|
Long: `Get a configuration value from config.yaml using a dot-notation path.
|
|
|
|
Examples:
|
|
wild config get cluster.name
|
|
wild config get apps.myapp.replicas
|
|
wild config get services[0].name`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runGet,
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&checkMode, "check", false, "exit 1 if key doesn't exist (no output)")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runGet(cmd *cobra.Command, args []string) error {
|
|
path := args[0]
|
|
|
|
// Initialize environment
|
|
env := environment.New()
|
|
|
|
// Try to detect WC_HOME from current directory or flags
|
|
if wcHome := cmd.Flag("wc-home").Value.String(); wcHome != "" {
|
|
env.SetWCHome(wcHome)
|
|
} else {
|
|
detected, err := env.DetectWCHome()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to detect Wild Cloud project directory: %w", err)
|
|
}
|
|
if detected == "" {
|
|
return fmt.Errorf("this command requires a Wild Cloud project directory. Run 'wild setup scaffold' to create one, or run from within an existing project")
|
|
}
|
|
env.SetWCHome(detected)
|
|
}
|
|
|
|
if err := env.RequiresProject(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create config manager
|
|
mgr := config.NewManager(env.ConfigPath(), env.SecretsPath())
|
|
|
|
// Get the value
|
|
value, err := mgr.Get(path)
|
|
if err != nil {
|
|
if checkMode {
|
|
os.Exit(1)
|
|
}
|
|
return fmt.Errorf("getting config value: %w", err)
|
|
}
|
|
|
|
// Handle null/missing values
|
|
if value == nil {
|
|
if checkMode {
|
|
os.Exit(1)
|
|
}
|
|
return fmt.Errorf("key path '%s' not found in config file", path)
|
|
}
|
|
|
|
// In check mode, exit 0 if key exists (don't output value)
|
|
if checkMode {
|
|
return nil
|
|
}
|
|
|
|
// Output the value
|
|
fmt.Println(value)
|
|
return nil
|
|
}
|