@@ -3,38 +3,42 @@ package cmd
33import (
44 "fmt"
55 "log"
6+
7+ "github.com/cocoide/commitify/util"
68 "github.com/spf13/cobra"
7- "github.com/spf13/viper"
89)
910
1011var setAPIKeyCmd = & cobra.Command {
1112 Use : "set-apikey [api_key]" ,
1213 Short : "API Key settings for ChatGPT" ,
13- Args : cobra .ExactArgs (1 ),
14- Run : func (cmd * cobra.Command , args [] string ) {
15- apikey := args [ 0 ]
16- viper . Set ( "chatgpt.api_key" , apikey )
17- if err := viper .WriteConfig (); err != nil {
18- log .Fatal ("An error occurred while writing the configuration file: " , err )
14+ Args : cobra .ExactArgs (1 ),
15+ Run : func (cmd * cobra.Command , args []string ) {
16+ config , _ := util . ReadConfig ()
17+ config . ChatGptToken = args [ 0 ]
18+ if err := util .WriteConfig (config ); err != nil {
19+ log .Fatal ("Failed to write into config " , err )
1920 }
20- fmt .Println ("ChatGPT API key has been set" )
21+ fmt .Println ("ChatGPT Token has been set" )
2122 },
2223}
2324
2425var showAPIKeyCmd = & cobra.Command {
25- Use : "show-apikey" ,
26+ Use : "show-apikey" ,
2627 Short : "Display ChatGPT API key" ,
27- Run : func (cmd * cobra.Command , args [] string ) {
28- apikey := viper .GetString ("chatgpt.api_key" )
29- if apikey == "" {
28+ Run : func (cmd * cobra.Command , args []string ) {
29+ config , err := util .ReadConfig ()
30+ if err != nil {
31+ log .Fatal ("Failed to read config:" , err )
32+ }
33+ if config .ChatGptToken == "" {
3034 fmt .Println ("API key is not set" )
3135 } else {
32- fmt .Println ("ChatGPT APIKey:" , apikey )
36+ fmt .Println ("ChatGPT APIKey:" , config . ChatGptToken )
3337 }
3438 },
3539}
3640
3741func init () {
3842 rootCmd .AddCommand (setAPIKeyCmd )
3943 rootCmd .AddCommand (showAPIKeyCmd )
40- }
44+ }
0 commit comments