Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>` 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:
Expand Down
18 changes: 17 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

Expand All @@ -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",
Expand All @@ -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 {
Expand Down
21 changes: 12 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sentiolabs/argus/internal/config"
)

var (
Expand All @@ -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")
Expand All @@ -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())
}
Expand Down
67 changes: 67 additions & 0 deletions internal/config/paths.go
Original file line number Diff line number Diff line change
@@ -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, <Dir()>/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
}
95 changes: 95 additions & 0 deletions internal/config/paths_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 7 additions & 5 deletions internal/update/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,13 +27,13 @@ const (
// updater treats as stable. Writes need a Path.
type FileStore struct{ Path string }

// DefaultPath returns <os.UserConfigDir()>/argus/update-channel.
// DefaultPath returns <config.Dir()>/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
Expand All @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions internal/update/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading