diff --git a/cmd/root.go b/cmd/root.go index 131df377..6e540afa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 { diff --git a/cmd/script.go b/cmd/script.go index f8313587..0451f138 100644 --- a/cmd/script.go +++ b/cmd/script.go @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index d6ebc1c0..7b6e12d9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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")