@@ -14,38 +14,33 @@ import (
1414 StoreUtils "github.com/dfanso/commit-msg/utils"
1515)
1616
17-
18-
1917type StoreMethods struct {
2018 ring keyring.Keyring
2119}
2220
2321//Initializes Keyring instance
24- func KeyringInit () (* StoreMethods , error ){
22+ func KeyringInit () (* StoreMethods , error ) {
2523 ring , err := keyring .Open (keyring.Config {
2624 ServiceName : "commit-msg" ,
2725 })
2826 if err != nil {
2927 return nil , fmt .Errorf ("failed to open keyring: %w" , err )
3028 }
31- return & StoreMethods {ring :ring },nil
32- }
33-
29+ return & StoreMethods {ring : ring }, nil
30+ }
3431
3532// LLMProvider represents a single stored LLM provider and its credential.
3633type LLMProvider struct {
3734 LLM types.LLMProvider `json:"model"`
3835 APIKey string `json:"api_key"`
3936}
4037
41-
4238// Config describes the on-disk structure for all saved LLM providers.
4339type Config struct {
44- Default types.LLMProvider `json:"default"`
45- LLMProviders []types.LLMProvider `json:"models"`
40+ Default types.LLMProvider `json:"default"`
41+ LLMProviders []types.LLMProvider `json:"models"`
4642}
4743
48-
4944// Save persists or updates an LLM provider entry, marking it as the default.
5045func (s * StoreMethods ) Save (LLMConfig LLMProvider ) error {
5146
@@ -78,13 +73,12 @@ func (s *StoreMethods) Save(LLMConfig LLMProvider) error {
7873 }
7974 }
8075
81-
8276 // If Model already present in config, update the apiKey
8377 updated := false
8478 for _ , p := range cfg .LLMProviders {
8579 if p == LLMConfig .LLM {
8680 err := s .ring .Set (keyring.Item { //save apiKey using keychain to OS credentials
87- Key : string (LLMConfig .LLM ),
81+ Key : string (LLMConfig .LLM ),
8882 Data : []byte (LLMConfig .APIKey ),
8983 })
9084 if err != nil {
@@ -98,13 +92,13 @@ func (s *StoreMethods) Save(LLMConfig LLMProvider) error {
9892 // If fresh Model is saved, means model not exists in config file
9993 if ! updated {
10094 cfg .LLMProviders = append (cfg .LLMProviders , LLMConfig .LLM )
101- err := s .ring .Set (keyring.Item { //save apiKey using keychain to OS credentials
102- Key : string (LLMConfig .LLM ),
103- Data : []byte (LLMConfig .APIKey ),
104- })
95+ err := s .ring .Set (keyring.Item { //save apiKey using keychain to OS credentials
96+ Key : string (LLMConfig .LLM ),
97+ Data : []byte (LLMConfig .APIKey ),
98+ })
10599 if err != nil {
106- return errors .New ("error storing credentials" )
107- }
100+ return errors .New ("error storing credentials" )
101+ }
108102 }
109103
110104 cfg .Default = LLMConfig .LLM
@@ -117,11 +111,9 @@ func (s *StoreMethods) Save(LLMConfig LLMProvider) error {
117111 return os .WriteFile (configPath , data , 0600 )
118112}
119113
120-
121114// DefaultLLMKey returns the currently selected default LLM provider, if any.
122115func (s * StoreMethods ) DefaultLLMKey () (* LLMProvider , error ) {
123116
124-
125117 var cfg Config
126118 var useModel LLMProvider
127119
@@ -153,10 +145,10 @@ func (s *StoreMethods) DefaultLLMKey() (*LLMProvider, error) {
153145
154146 for i , p := range cfg .LLMProviders {
155147 if p == defaultLLM {
156- useModel .LLM = cfg .LLMProviders [i ] // Fetches default Model from config json
157- i ,err := s .ring .Get (string (useModel .LLM )) //Fetches apiKey from OS credential for default model
148+ useModel .LLM = cfg .LLMProviders [i ] // Fetches default Model from config json
149+ i , err := s .ring .Get (string (useModel .LLM )) //Fetches apiKey from OS credential for default model
158150 if err != nil {
159- return nil ,err
151+ return nil , err
160152 }
161153 useModel .APIKey = string (i .Data )
162154 return & useModel , nil
@@ -280,8 +272,8 @@ func (s *StoreMethods) DeleteModel(Model types.LLMProvider) error {
280272 } else {
281273 err := s .ring .Remove (string (Model )) // Removes the apiKey from OS credentials
282274 if err != nil {
283- return err
284- }
275+ return err
276+ }
285277 return os .WriteFile (configPath , []byte ("{}" ), 0600 )
286278 }
287279 } else {
@@ -292,7 +284,7 @@ func (s *StoreMethods) DeleteModel(Model types.LLMProvider) error {
292284 newCfg .LLMProviders = append (newCfg .LLMProviders , p )
293285 }
294286 }
295-
287+
296288 err := s .ring .Remove (string (Model )) //Remove the apiKey from OS credentials
297289 if err != nil {
298290 return err
@@ -338,8 +330,8 @@ func (s *StoreMethods) UpdateAPIKey(Model types.LLMProvider, APIKey string) erro
338330 updated := false
339331 for _ , p := range cfg .LLMProviders {
340332 if p == Model {
341- err := s .ring .Set (keyring.Item { // Update the apiKey in OS credential
342- Key : string (Model ),
333+ err := s .ring .Set (keyring.Item { // Update the apiKey in OS credential
334+ Key : string (Model ),
343335 Data : []byte (APIKey ),
344336 })
345337 if err != nil {
@@ -361,4 +353,3 @@ func (s *StoreMethods) UpdateAPIKey(Model types.LLMProvider, APIKey string) erro
361353 return os .WriteFile (configPath , data , 0600 )
362354
363355}
364-
0 commit comments