Skip to content

Commit

Permalink
[internal] - refactor: improve override config search and update titles
Browse files Browse the repository at this point in the history
 - Search for override config in parent directories until the root directory
 - Use model ID as the title for embed, diff, and chat commands
 - Change default role name in chat command to "anon"
  • Loading branch information
malonaz committed Aug 30, 2023
1 parent 0fc2b3a commit ca428fb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewCmd(openAIClient *openai.Client, config *configuration.Config) *cobra.Co
cobra.CheckErr(err)

// Headers.
roleName := "default"
roleName := "anon"
if role != nil {
roleName = role.Name
}
Expand Down
2 changes: 1 addition & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewCmd(openAIClient *openai.Client, config *configuration.Config) *cobra.Co
cobra.CheckErr(err)

// Headers.
cli.Title("SGPT DIFF [%s]", model.ID)
cli.Title(model.ID)

// Run git diff.
path, err := exec.LookPath("git")
Expand Down
2 changes: 1 addition & 1 deletion embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewCmd(openAIClient *openai.Client, config *configuration.Config) *cobra.Co
cobra.CheckErr(err)

// Headers.
cli.Title("SGPT EMBED [%s]", model.ID)
cli.Title(model.ID)

// Run git diff.
path, err := exec.LookPath("git")
Expand Down
34 changes: 30 additions & 4 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ func Parse(path string) (*Config, error) {
}

// Parse override configuration if present.
overrideConfigPath := ".sgpt.json"
ok, err := file.Exists(overrideConfigPath)
overrideConfigPath, err := findOverrideConfigPath()
if err != nil {
return nil, errors.Wrap(err, "checking override config existence")
return nil, errors.Wrap(err, "finding override config path")
}
if ok {
if overrideConfigPath != "" {
bytes, err := os.ReadFile(overrideConfigPath)
if err != nil {
return nil, errors.Wrap(err, "reading override config")
Expand Down Expand Up @@ -161,3 +160,30 @@ func initializeIfNotPresent(path string) error {
}
return nil
}

func findOverrideConfigPath() (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "getting working directory")
}

for {
overrideConfigPath := filepath.Join(currentDir, ".sgpt.json")
ok, err := file.Exists(overrideConfigPath)
if err != nil {
return "", errors.Wrap(err, "checking override config existence")
}

if ok {
return overrideConfigPath, nil
}

if currentDir == filepath.Dir(currentDir) { // Reached root
break
}

currentDir = filepath.Dir(currentDir)
}

return "", nil
}

0 comments on commit ca428fb

Please sign in to comment.