diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index 87122f0..b35ca1e 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -5,34 +5,28 @@ import ( "os" ) -func Decrypt(path string, pass []byte) error { - file, err := os.ReadFile(path) - - if err != nil { - return err - } - - dec, err := cipher.Decrypt(file, pass) +type cipherFunc func(data, pass []byte) ([]byte, error) - if err != nil { - return err - } - - return os.WriteFile(path, dec, 0644) +func Decrypt(path string, pass []byte) error { + return processFile(path, pass, cipher.Decrypt) } func Encrypt(path string, pass []byte) error { + return processFile(path, pass, cipher.Encrypt) +} + +func processFile(path string, pass []byte, process cipherFunc) error { file, err := os.ReadFile(path) if err != nil { return err } - enc, err := cipher.Encrypt(file, pass) + result, err := process(file, pass) if err != nil { return err } - return os.WriteFile(path, enc, 0644) + return os.WriteFile(path, result, 0644) }