Files
panic-cli/cmd/persona/set.go
2025-12-08 07:22:02 -07:00

85 lines
2.0 KiB
Go

package persona
import (
"fmt"
"os"
"github.com/Print-and-Panic/panic-cli/internal/config"
"github.com/Print-and-Panic/panic-cli/internal/git"
"github.com/Print-and-Panic/panic-cli/internal/system"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var setCmd = &cobra.Command{
Use: "set",
Short: "Set the persona to use",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var personas []string
for k := range config.AppConfig.PersonaManagement.Personas {
personas = append(personas, k)
}
return personas, cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
targetName := args[0]
// Update Config
config.AppConfig.PersonaManagement.CurrentPersona = targetName
viper.Set("persona_management.current_persona", targetName)
if err := viper.WriteConfig(); err != nil {
fmt.Printf("❌ Failed to save config: %v\n", err)
} else {
fmt.Println("✅ Configuration saved.")
}
// Access the global config from the internal package
persona, exists := config.AppConfig.PersonaManagement.Personas[targetName]
if !exists {
fmt.Printf("❌ Persona '%s' not found.\n", targetName)
os.Exit(1)
}
fmt.Printf("🎭 Switching to: %s\n", targetName)
err := setPersona(persona)
if err != nil {
fmt.Printf("❌ %s\n", err)
os.Exit(1)
}
// Verify the active identity (checks for local overrides, env vars, etc.)
err = validatePersona()
if err != nil {
fmt.Printf("❌ %s\n", err)
os.Exit(1)
}
},
}
func init() {
PersonaCmd.AddCommand(setCmd)
}
func setPersona(persona config.PersonaConfig) error {
// Execute Logic
err := system.CurrentSystem.SetGitIdentity(git.GitIdentity{
Name: persona.GitName,
Email: persona.GitEmail,
})
if err != nil {
fmt.Printf("❌ Failed to set Git Identity: %v\n", err)
}
err = system.CurrentSystem.SetTheme(persona.KdeTheme)
if err != nil {
fmt.Printf("❌ Failed to set KDE Theme: %v\n", err)
}
return nil
}