Skip to content

Commit

Permalink
🎨 Refactor config file creation and prompting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonBirchall committed Dec 5, 2024
1 parent b051901 commit 8015a4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
38 changes: 34 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand All @@ -25,13 +26,42 @@ in the user's home directory.`,
fmt.Println("Error getting home directory:", err)
return
}
_ = createConfigFile(homeDir)
configFilePath := filepath.Join(homeDir, ".lazycommit.yaml")
if !checkForExistingConfig(configFilePath) {
return
}
apiKey := promptForAPIKey()
_ = createConfigFile(configFilePath, apiKey)
},
}

func createConfigFile(homeDir string) string {
configFilePath := filepath.Join(homeDir, ".lazycommit.yaml")
err := os.WriteFile(configFilePath, []byte(configContent), 0644)
func promptForAPIKey() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your OpenAI API Key: ")
apiKey, _ := reader.ReadString('\n')
return apiKey
}

func promptForOverwrite() bool {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Configuration file already exists. Overwrite? (y/n): ")
overwrite, _ := reader.ReadString('\n')
return overwrite == "y\n"
}

func checkForExistingConfig(configFilePath string) bool {
if _, err := os.Stat(configFilePath); err == nil {
if !promptForOverwrite() {
fmt.Println("Configuration file not overwritten.")
return false
}
}
return true
}

func createConfigFile(configFilePath, apiKey string) string {
configWithAPIKey := configContent + "\nopenai_api_key: " + apiKey
err := os.WriteFile(configFilePath, []byte(configWithAPIKey), 0644)
if err != nil {
fmt.Println("Error writing config file:", err)
return ""
Expand Down
26 changes: 0 additions & 26 deletions cmd/init_test.go

This file was deleted.

0 comments on commit 8015a4b

Please sign in to comment.