@@ -174,15 +174,10 @@ func (s *sicher) Edit(editor ...string) error {
174174 }
175175
176176 // read the encryption key. if key not in file, try getting from env
177- key , err := os . ReadFile (fmt .Sprintf ("%s%s.key" , s .Path , s .Environment ))
177+ key , err := s . getEncryptionKey (fmt .Sprintf ("%s%s.key" , s .Path , s .Environment ))
178178 if err != nil {
179- if os .Getenv (masterKey ) != "" {
180- key = []byte (os .Getenv (masterKey ))
181- } else {
182- return fmt .Errorf ("encryption key(%s.key) is not available. Provide a key file or enter one through the command line" , s .Environment )
183- }
179+ return err
184180 }
185- strKey := string (key )
186181
187182 // open the encrypted credentials file
188183 credFile , err := os .OpenFile (fmt .Sprintf ("%s%s.enc" , s .Path , s .Environment ), os .O_RDWR | os .O_APPEND | os .O_CREATE , 0644 )
@@ -225,7 +220,7 @@ func (s *sicher) Edit(editor ...string) error {
225220
226221 var plaintext []byte
227222 if nonce != nil && fileText != nil {
228- plaintext , err = decrypt (strKey , nonce , fileText )
223+ plaintext , err = decrypt (key , nonce , fileText )
229224 if err != nil {
230225 return fmt .Errorf ("error decrypting file: %s" , err )
231226 }
@@ -266,7 +261,7 @@ func (s *sicher) Edit(editor ...string) error {
266261
267262 //encrypt and overwrite credentials file
268263 // the encrypted file is encoded in hexadecimal format
269- nonce , encrypted , err := encrypt (strKey , file )
264+ nonce , encrypted , err := encrypt (key , file )
270265 if err != nil {
271266 return fmt .Errorf ("error encrypting file: %s " , err )
272267 }
@@ -343,3 +338,15 @@ func (s *sicher) SetGitignorePath(path string) {
343338 path , _ = filepath .Abs (path )
344339 s .gitignorePath = path
345340}
341+
342+ func (s * sicher ) getEncryptionKey (filePath string ) (string , error ) {
343+ encKey := os .Getenv (masterKey )
344+ if encKey == "" {
345+ key , err := os .ReadFile (filePath )
346+ if err != nil {
347+ return "" , fmt .Errorf ("encryption key(%s.key) is not available. Provide a key file or enter one through the command line" , s .Environment )
348+ }
349+ encKey = string (key )
350+ }
351+ return encKey , nil
352+ }
0 commit comments