diff --git a/cmd/generate/codeowners/codeowners.go b/cmd/generate/codeowners/codeowners.go index 4741a67..d91f7ac 100644 --- a/cmd/generate/codeowners/codeowners.go +++ b/cmd/generate/codeowners/codeowners.go @@ -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 @@ -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 { @@ -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 } diff --git a/cmd/root/root.go b/cmd/root/root.go index 379baaa..9dbbc45 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -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") diff --git a/pkg/config/config.go b/pkg/config/config.go index 1bc53ba..ed722c5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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) @@ -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) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index bec5564..c0da2e6 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -30,7 +30,7 @@ attribution: require.NoError(t, os.WriteFile(configFilePath, []byte(fileContents), 0600)) - config, err := LoadConfig(configFilePath, "") + config, err := LoadConfig(configFilePath) require.NoError(t, err) assert.NotNil(t, config) @@ -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. @@ -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)