diff --git a/README.md b/README.md index 7947c25..1e7b69c 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,14 @@ ARGUS_JIRA_TOKEN=xxxxxxxxxxxxxxxx ### Configuration File -Create a `.argus.yaml` file in your working directory: +Argus uses the first config file it finds: + +1. `./.argus.yaml` in the working directory (per-project) +2. `$XDG_CONFIG_HOME/argus/config.yaml`, defaulting to `~/.config/argus/config.yaml` on Linux and macOS (global) + +Pass `--config ` to use a specific file. Run `argus config init` to write an example `./.argus.yaml`, or `argus config init --global` to write the global file. + +Example config: ```yaml defaults: diff --git a/cmd/config.go b/cmd/config.go index edf70d8..49c9aea 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -5,6 +5,7 @@ import ( _ "embed" "fmt" "os" + "path/filepath" "github.com/spf13/cobra" @@ -14,6 +15,8 @@ import ( //go:embed configs/example.yaml var exampleConfig string +var initGlobal bool + var configCmd = &cobra.Command{ Use: "config", Short: "Configuration management commands", @@ -35,10 +38,23 @@ func init() { rootCmd.AddCommand(configCmd) configCmd.AddCommand(configInitCmd) configCmd.AddCommand(configValidateCmd) + + configInitCmd.Flags().BoolVar(&initGlobal, "global", false, + "write to the global config (~/.config/argus/config.yaml) instead of ./.argus.yaml") } func runConfigInit(cmd *cobra.Command, args []string) error { - filename := ".argus.yaml" + filename := config.LocalFile + if initGlobal { + global, err := config.File() + if err != nil { + return fmt.Errorf("failed to resolve global config path: %w", err) + } + filename = global + if err := os.MkdirAll(filepath.Dir(filename), 0o700); err != nil { + return fmt.Errorf("failed to create config dir: %w", err) + } + } // Check if file already exists if _, err := os.Stat(filename); err == nil { diff --git a/cmd/root.go b/cmd/root.go index 144c998..503f273 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + + "github.com/sentiolabs/argus/internal/config" ) var ( @@ -32,7 +34,7 @@ func Execute() error { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.argus.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./.argus.yaml, then ~/.config/argus/config.yaml)") rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "preview changes without creating Jira tickets") rootCmd.PersistentFlags().StringVar(&outputFormat, "output", "table", "output format: json or table") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose output") @@ -45,18 +47,19 @@ func initConfig() { viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - if cfgFile != "" { - viper.SetConfigFile(cfgFile) - } else { - home, err := os.UserHomeDir() + if cfgFile == "" { + found, err := config.FindConfigFile() cobra.CheckErr(err) + cfgFile = found + } - viper.AddConfigPath(home) - viper.AddConfigPath(".") - viper.SetConfigType("yaml") - viper.SetConfigName(".argus") + if cfgFile == "" { + // No config file anywhere: run on environment variables alone. + return } + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } diff --git a/internal/config/paths.go b/internal/config/paths.go new file mode 100644 index 0000000..c250cde --- /dev/null +++ b/internal/config/paths.go @@ -0,0 +1,67 @@ +package config + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" +) + +// Dir returns the argus config directory. +// Respects $XDG_CONFIG_HOME, defaults to ~/.config/argus on every platform. +// os.UserConfigDir is deliberately avoided: on macOS it returns +// ~/Library/Application Support, and argus keeps dotfiles in ~/.config there too. +func Dir() (string, error) { + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { + return filepath.Join(xdg, "argus"), nil + } + + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("resolve home dir: %w", err) + } + + return filepath.Join(home, ".config", "argus"), nil +} + +// LocalFile is the per-project config file, read from the working directory. +const LocalFile = ".argus.yaml" + +// File returns the global config file path, /config.yaml. +func File() (string, error) { + dir, err := Dir() + if err != nil { + return "", err + } + return filepath.Join(dir, "config.yaml"), nil +} + +// candidatePaths returns the config file locations to search, highest +// precedence first. The first one that exists wins: a project-local +// .argus.yaml overrides the global config. +func candidatePaths() []string { + paths := []string{LocalFile} + // Without a resolvable home dir the local file is still usable. + if global, err := File(); err == nil { + paths = append(paths, global) + } + return paths +} + +// FindConfigFile returns the first existing path from candidatePaths. +// It returns "" and a nil error when no config file exists, so callers can +// fall back to environment variables alone. +func FindConfigFile() (string, error) { + for _, p := range candidatePaths() { + _, err := os.Stat(p) + if err == nil { + return p, nil + } + if !errors.Is(err, fs.ErrNotExist) { + return "", fmt.Errorf("stat config file %s: %w", p, err) + } + } + + return "", nil +} diff --git a/internal/config/paths_test.go b/internal/config/paths_test.go new file mode 100644 index 0000000..2687ef9 --- /dev/null +++ b/internal/config/paths_test.go @@ -0,0 +1,95 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +func TestDir_XDG(t *testing.T) { + xdg := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", xdg) + + got, err := Dir() + if err != nil { + t.Fatalf("Dir() error = %v", err) + } + if want := filepath.Join(xdg, "argus"); got != want { + t.Errorf("Dir() = %q, want %q", got, want) + } +} + +func TestDir_DefaultsToDotConfig(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", "") + + got, err := Dir() + if err != nil { + t.Fatalf("Dir() error = %v", err) + } + if want := filepath.Join(home, ".config", "argus"); got != want { + t.Errorf("Dir() = %q, want %q", got, want) + } +} + +func TestFindConfigFile_NoneExist(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Chdir(t.TempDir()) + + got, err := FindConfigFile() + if err != nil { + t.Fatalf("FindConfigFile() error = %v", err) + } + if got != "" { + t.Errorf("FindConfigFile() = %q, want empty", got) + } +} + +func TestFindConfigFile_XDG(t *testing.T) { + xdg := t.TempDir() + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", xdg) + t.Chdir(t.TempDir()) + + want := filepath.Join(xdg, "argus", "config.yaml") + if err := os.MkdirAll(filepath.Dir(want), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(want, []byte("defaults: {}\n"), 0o600); err != nil { + t.Fatal(err) + } + + got, err := FindConfigFile() + if err != nil { + t.Fatalf("FindConfigFile() error = %v", err) + } + if got != want { + t.Errorf("FindConfigFile() = %q, want %q", got, want) + } +} + +func TestFindConfigFile_LocalWinsOverGlobal(t *testing.T) { + xdg := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", xdg) + t.Chdir(t.TempDir()) + + global := filepath.Join(xdg, "argus", "config.yaml") + if err := os.MkdirAll(filepath.Dir(global), 0o700); err != nil { + t.Fatal(err) + } + for _, p := range []string{global, LocalFile} { + if err := os.WriteFile(p, []byte("defaults: {}\n"), 0o600); err != nil { + t.Fatal(err) + } + } + + got, err := FindConfigFile() + if err != nil { + t.Fatalf("FindConfigFile() error = %v", err) + } + if got != LocalFile { + t.Errorf("FindConfigFile() = %q, want %q", got, LocalFile) + } +} diff --git a/internal/update/store.go b/internal/update/store.go index ac968b2..cf6025b 100644 --- a/internal/update/store.go +++ b/internal/update/store.go @@ -10,6 +10,8 @@ import ( "strings" "github.com/sentiolabs/selfupdate-go" + + "github.com/sentiolabs/argus/internal/config" ) // ErrNoPath is returned by SetChannel when Path is empty. @@ -25,13 +27,13 @@ const ( // updater treats as stable. Writes need a Path. type FileStore struct{ Path string } -// DefaultPath returns /argus/update-channel. +// DefaultPath returns /update-channel. func DefaultPath() (string, error) { - dir, err := os.UserConfigDir() + dir, err := config.Dir() if err != nil { - return "", fmt.Errorf("resolve user config dir: %w", err) + return "", fmt.Errorf("resolve config dir: %w", err) } - return filepath.Join(dir, "argus", "update-channel"), nil + return filepath.Join(dir, "update-channel"), nil } // Channel reads Path. An empty Path or a missing file yields "" and a nil @@ -40,7 +42,7 @@ func (s FileStore) Channel() (selfupdate.Channel, error) { if s.Path == "" { return "", nil } - data, err := os.ReadFile(s.Path) //nolint:gosec // Path comes from os.UserConfigDir, not user input + data, err := os.ReadFile(s.Path) //nolint:gosec // Path comes from config.Dir, not user input if errors.Is(err, fs.ErrNotExist) { return "", nil } diff --git a/internal/update/store_test.go b/internal/update/store_test.go index 298bef6..58ff776 100644 --- a/internal/update/store_test.go +++ b/internal/update/store_test.go @@ -77,3 +77,13 @@ func TestDefaultPath(t *testing.T) { assert.True(t, strings.HasSuffix(p, filepath.Join("argus", "update-channel")), p) assert.True(t, filepath.IsAbs(p), p) } + +func TestDefaultPath_DotConfig(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", "") + + p, err := update.DefaultPath() + require.NoError(t, err) + assert.Equal(t, filepath.Join(home, ".config", "argus", "update-channel"), p) +}