diff --git a/cmd/spilltea/main.go b/cmd/spilltea/main.go index 2b03d5d..07a68fd 100644 --- a/cmd/spilltea/main.go +++ b/cmd/spilltea/main.go @@ -31,6 +31,7 @@ func main() { flagVersion = flag.BoolP("version", "v", false, "print version") flagProject = flag.StringP("project", "P", "", `project name to open directly, or "tmp" for a temporary session`) flagAddDefaultPlugins = flag.Bool("add-default-plugins", false, "copy built-in example plugins into the plugins dir and exit") + flagAddDefaultConfig = flag.Bool("add-default-config", false, "copy the default config file to the config path and exit") ) flag.CommandLine.SetOutput(os.Stdout) flag.Usage = func() { @@ -66,6 +67,19 @@ func main() { os.Exit(0) } + if *flagAddDefaultConfig { + cfgPath := filepath.Join(os.Getenv("HOME"), ".config", "spilltea", "config.yaml") + if *flagConfig != "" { + cfgPath = *flagConfig + } + if err := config.WriteDefaultConfig(cfgPath); err != nil { + fmt.Fprintf(os.Stderr, "add-default-config: %v\n", err) + os.Exit(1) + } + fmt.Printf("default config written to %s\n", cfgPath) + os.Exit(0) + } + if *flagProject != "" && !homeUI.IsValidProjectName(*flagProject) { fmt.Fprintf(os.Stderr, "project: invalid name %q (only lowercase letters, digits, - and _ are allowed)\n", *flagProject) os.Exit(1) diff --git a/internal/config/config.go b/internal/config/config.go index 54e318f..acdd663 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -74,6 +74,16 @@ func Load(path string) error { return viper.Unmarshal(Global) } +func WriteDefaultConfig(path string) error { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return fmt.Errorf("create config dir: %w", err) + } + if err := os.WriteFile(path, defaultConfig, 0o644); err != nil { + return fmt.Errorf("write config: %w", err) + } + return nil +} + func ExpandPath(p string) string { if strings.HasPrefix(p, "~/") { home, err := os.UserHomeDir() diff --git a/lefthook.yml b/lefthook.yml index 7842814..626bc3a 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -2,8 +2,8 @@ pre-commit: piped: true commands: inject-exec-basics: - glob: "docs/basics.md" - run: python3 .github/scripts/inject-exec.py {staged_files} + glob: "{docs/basics.md,cmd/**}" + run: python3 .github/scripts/inject-exec.py docs/basics.md stage_fixed: true inject-exec: glob: "README.md"