consolidate

This commit is contained in:
Ed Zynda
2025-06-17 23:09:29 +03:00
parent 354e8a09fb
commit 1a5d4ccf8a
3 changed files with 31 additions and 1 deletions
+6
View File
@@ -84,6 +84,12 @@ func initConfig() {
// Use config file from the flag
viper.SetConfigFile(configFile)
} else {
// Ensure a config file exists (create default if none found)
if err := config.EnsureConfigExists(); err != nil {
// If we can't create config, continue silently (non-fatal)
fmt.Fprintf(os.Stderr, "Warning: Could not create default config file: %v\n", err)
}
// Find home directory
home, err := os.UserHomeDir()
if err != nil {
-1
View File
@@ -68,7 +68,6 @@ This will replace ${directory} with "/tmp" and ${name} with "John" in the script
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(scriptCmd)
// Add the same flags as the root command, but they will override script settings
+25
View File
@@ -154,6 +154,31 @@ func LoadSystemPrompt(input string) (string, error) {
return input, nil
}
// EnsureConfigExists checks if a config file exists and creates a default one if not
func EnsureConfigExists() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %v", err)
}
// Check for existing config files (new format first, then legacy)
configNames := []string{".mcphost", ".mcp"}
configTypes := []string{"yaml", "json"}
for _, configName := range configNames {
for _, configType := range configTypes {
configPath := filepath.Join(homeDir, configName+"."+configType)
if _, err := os.Stat(configPath); err == nil {
// Config file exists, no need to create
return nil
}
}
}
// No config file found, create default
return createDefaultConfig(homeDir)
}
// createDefaultConfig creates a default .mcphost.yml file in the user's home directory
func createDefaultConfig(homeDir string) error {
configPath := filepath.Join(homeDir, ".mcphost.yml")