package main import ( "fmt" "os" "regexp" "strings" ) // handleSet sets a shell variable func (c *KVSClient) handleSet(args []string) { if len(args) < 2 { fmt.Println(red("Usage: set ")) fmt.Println("Example: set USER_ID abc-123") return } name := args[0] value := strings.Join(args[1:], " ") c.variables[name] = value fmt.Printf(green("Set:")+" %s = %s\n", name, value) } // handleUnset removes a shell variable func (c *KVSClient) handleUnset(args []string) { if len(args) < 1 { fmt.Println(red("Usage: unset ")) return } name := args[0] if _, exists := c.variables[name]; exists { delete(c.variables, name) fmt.Println(green("Unset:"), name) } else { fmt.Println(yellow("Variable not set:"), name) } } // handleVars lists all shell variables func (c *KVSClient) handleVars(args []string) { if len(c.variables) == 0 { fmt.Println(yellow("No variables set")) return } fmt.Println(cyan("Shell Variables:")) for name, value := range c.variables { fmt.Printf(" %s = %s\n", name, value) } } // expandVariables performs variable substitution on a string // Supports: // - $VAR or ${VAR} for shell variables // - $ENV:VAR for environment variables func (c *KVSClient) expandVariables(input string) string { // Pattern for $VAR or ${VAR} varPattern := regexp.MustCompile(`\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?`) // Pattern for $ENV:VAR envPattern := regexp.MustCompile(`\$ENV:([A-Za-z_][A-Za-z0-9_]*)`) // First, replace environment variables result := envPattern.ReplaceAllStringFunc(input, func(match string) string { varName := envPattern.FindStringSubmatch(match)[1] if value, exists := os.LookupEnv(varName); exists { return value } return match // Leave unchanged if not found }) // Then, replace shell variables result = varPattern.ReplaceAllStringFunc(result, func(match string) string { varName := varPattern.FindStringSubmatch(match)[1] if value, exists := c.variables[varName]; exists { return value } return match // Leave unchanged if not found }) return result }