package cmd import ( "fmt" "log" "os" "strings" "github.com/spf13/cobra" ) // envCmd represents the env command var envCmd = &cobra.Command{ Use: "env [action] [key] [value]", Short: "Manage environment variables", Long: `Manage environment variables. Actions: - list: List all environment variables. - set: Set an environment variable. - unset: Unset an environment variable.`, Args: cobra.RangeArgs(1, 3), Run: func(cmd *cobra.Command, args []string) { action := args[0] switch action { case "list": listEnvVariables() case "set": if len(args) != 3 { fmt.Println("Usage: dttt env set [key] [value]") return } key := args[1] value := args[2] setEnvVariable(key, value) case "unset": if len(args) != 2 { fmt.Println("Usage: dttt env unset [key]") return } key := args[1] unsetEnvVariable(key) default: fmt.Println("Invalid action. Available actions: list, set, unset") } }, } func init() { rootCmd.AddCommand(envCmd) } func listEnvVariables() { fmt.Println("Environment variables:") for _, env := range os.Environ() { pair := strings.SplitN(env, "=", 2) fmt.Printf("%s=%s\n", pair[0], pair[1]) } } func setEnvVariable(key, value string) { err := os.Setenv(key, value) if err != nil { log.Fatalf("Error setting environment variable: %v", err) } fmt.Printf("Environment variable %s set to %s\n", key, value) } func unsetEnvVariable(key string) { err := os.Unsetenv(key) if err != nil { log.Fatalf("Error unsetting environment variable: %v", err) } fmt.Printf("Environment variable %s unset\n", key) }