Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the local directory and home directory as fallback for .sauced.yaml #158

Merged
merged 1 commit into from
Sep 6, 2024
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
6 changes: 3 additions & 3 deletions cmd/generate/codeowners/codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Options struct {

const codeownersLongDesc string = `WARNING: Proof of concept feature.

Generates a CODEOWNERS file for a given git repository. This uses a ~/.sauced.yaml
Generates a CODEOWNERS file for a given git repository. This uses a .sauced.yaml
configuration to attribute emails with given entities.

The generated file specifies up to 3 owners for EVERY file in the git tree based on the
Expand All @@ -59,7 +59,7 @@ func NewCodeownersCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "codeowners path/to/repo [flags]",
Short: "Generates a CODEOWNERS file for a given repository using a \"~/.sauced.yaml\" config",
Short: "Generates a CODEOWNERS file for a given repository using a \".sauced.yaml\" config",
Long: codeownersLongDesc,
Args: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewCodeownersCommand() *cobra.Command {
defer opts.telemetry.Done()

configPath, _ := cmd.Flags().GetString("config")
opts.config, err = config.LoadConfig(configPath, filepath.Join(opts.path, ".sauced.yaml"))
opts.config, err = config.LoadConfig(configPath)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewRootCommand() (*cobra.Command, error) {
cmd.PersistentFlags().StringP(constants.FlagNameEndpoint, "e", constants.EndpointProd, "The API endpoint to send requests to")
cmd.PersistentFlags().Bool(constants.FlagNameBeta, false, fmt.Sprintf("Shorthand for using the beta OpenSauced API endpoint (\"%s\"). Supersedes the '--%s' flag", constants.EndpointBeta, constants.FlagNameEndpoint))
cmd.PersistentFlags().Bool(constants.FlagNameTelemetry, false, "Disable sending telemetry data to OpenSauced")
cmd.PersistentFlags().StringP("config", "c", "~/.sauced.yaml", "The saucectl config")
cmd.PersistentFlags().StringP("config", "c", ".sauced.yaml", "The saucectl config")
cmd.PersistentFlags().StringP("log-level", "l", "info", "The logging level. Options: error, warn, info, debug")
cmd.PersistentFlags().Bool("tty-disable", false, "Disable log stylization. Suitable for CI/CD and automation")

Expand Down
40 changes: 20 additions & 20 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ import (
"gopkg.in/yaml.v3"
)

const DefaultConfigPath = "~/.sauced.yaml"

// LoadConfig loads a configuration file at a given path. It attempts to load
// the default location of a ".sauced.yaml" in the user's home directory if an
// empty path is provided. If none is found in the user's home directory, it tries to load
// ".sauced.yaml" from the fallback path, which is the root path of a repository.
func LoadConfig(path string, repoRootPathConfig string) (*Spec, error) {
// the default location of a ".sauced.yaml" in the current working directory if an
// empty path is provided. If none is found, it tries to load
// "~/.sauced.yaml" from the fallback path, which is the user's home directory.
func LoadConfig(path string) (*Spec, error) {
println("Config path loading from -c flag", path)

config := &Spec{}

if path == DefaultConfigPath || path == "" {
// load the default file path under the user's home dir
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not get user home directory: %w", err)
}

path = filepath.Join(usr.HomeDir, ".sauced.yaml")
}

absPath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("error resolving absolute path: %w", err)
Expand All @@ -39,15 +27,27 @@ func LoadConfig(path string, repoRootPathConfig string) (*Spec, error) {
if err != nil {
// If the file does not exist, check if the fallback path exists
if os.IsNotExist(err) {
_, err = os.Stat(repoRootPathConfig)
// load the default file path under the user's home dir
usr, err := user.Current()

if err != nil {
return nil, fmt.Errorf("error reading config file from %s or %s", absPath, repoRootPathConfig)
return nil, fmt.Errorf("could not get user home directory: %w", err)
}

data, err = os.ReadFile(repoRootPathConfig)
homeDirPathConfig, err := filepath.Abs(filepath.Join(usr.HomeDir, ".sauced.yaml"))

if err != nil {
return nil, fmt.Errorf("error home directory absolute path: %w", err)
}

_, err = os.Stat(homeDirPathConfig)
if err != nil {
return nil, fmt.Errorf("error reading config file from %s", homeDirPathConfig)
}

data, err = os.ReadFile(homeDirPathConfig)
if err != nil {
return nil, fmt.Errorf("error reading config file from %s", repoRootPathConfig)
return nil, fmt.Errorf("error reading config file from %s or %s", absPath, homeDirPathConfig)
}
} else {
return nil, fmt.Errorf("error reading config file: %w", err)
Expand Down
10 changes: 4 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ attribution:

require.NoError(t, os.WriteFile(configFilePath, []byte(fileContents), 0600))
jpmcb marked this conversation as resolved.
Show resolved Hide resolved

config, err := LoadConfig(configFilePath, "")
config, err := LoadConfig(configFilePath)
require.NoError(t, err)
assert.NotNil(t, config)

Expand All @@ -47,12 +47,12 @@ attribution:
tmpDir := t.TempDir()
nonExistentPath := filepath.Join(tmpDir, ".sauced.yaml")

config, err := LoadConfig(nonExistentPath, "")
config, err := LoadConfig(nonExistentPath)
require.Error(t, err)
assert.Nil(t, config)
})

t.Run("Non-existent file with fallback", func(t *testing.T) {
t.Run("Providing a custom .sauced.yaml location", func(t *testing.T) {
t.Parallel()
fileContents := `# Configuration for attributing commits with emails to GitHub user profiles
# Used during codeowners generation.
Expand All @@ -78,9 +78,7 @@ attribution:
_, err := os.ReadFile(fallbackPath)
require.NoError(t, err)

nonExistentPath := filepath.Join(tmpDir, "non-existent.yaml")

config, err := LoadConfig(nonExistentPath, fallbackPath)
config, err := LoadConfig(fallbackPath)

require.NoError(t, err)
assert.NotNil(t, config)
Expand Down
Loading