-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to save OpenAI API key locally
- Extend `README.md` with instructions to save the API key. - Modify `main.go` to include a `--save-key` flag. - Implement functions in `savekey.go` for saving/loading the key.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func configDir() (string, error) { | ||
cdir, err := os.UserConfigDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
err = os.MkdirAll(filepath.Join(cdir, "aicommit"), 0o700) | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(cdir, "aicommit"), nil | ||
} | ||
|
||
func keyPath() (string, error) { | ||
cdir, err := configDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(cdir, "openai.key"), nil | ||
} | ||
|
||
func saveKey(key string) error { | ||
if key == "" { | ||
return errors.New("key is empty") | ||
} | ||
kp, err := keyPath() | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(kp, []byte(key), 0o600) | ||
} | ||
|
||
func loadKey() (string, error) { | ||
kp, err := keyPath() | ||
if err != nil { | ||
return "", err | ||
} | ||
b, err := os.ReadFile(kp) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(b), nil | ||
} |