diff --git a/main.go b/main.go index f97f88f..1afd92c 100644 --- a/main.go +++ b/main.go @@ -31,36 +31,82 @@ func main() { } else if options[0] == "-w" { // write file functions filename := options[1] - cliInput, err := cli.CaptureInputFromEditor() - if err != nil { - logger.Critical(err.Error()) - } + if utils.FileExists(filename) { + Key := DefaultKey + if options[2] != "" { + Key = options[2] + } - Key := DefaultKey - if options[2] != "" { - Key = options[2] - } + dataFromFile, err := utils.ReadFromFile(filename) + if err != nil { + logger.Critical(err.Error()) + } - Encrypted, err := crypt.Encrypt(cliInput, Key) - if err != nil { - logger.Critical(err.Error()) - } + Decrypted, err := crypt.Decrypt(dataFromFile, Key) + if err != nil { + logger.Critical(err.Error()) + } - err = utils.WriteToFile(filename, Encrypted) - if err != nil { - logger.Warning("saving failed ...") + err = utils.WriteToFile("data.tmp", Decrypted) + if err != nil { + logger.Critical(err.Error()) + } - Decrypted, decErr := crypt.Decrypt(Encrypted, Key) - if decErr != nil { + err = cli.OpenFileInEditor("data.tmp") + if err != nil { logger.Critical(err.Error()) } - logger.Output(string(Decrypted)) + updatedData, err := utils.ReadFromFile("data.tmp") + if err != nil { + logger.Critical(err.Error()) + } - logger.Critical(err.Error()) - } + defer os.Remove("data.tmp") - logger.Info(filename + " saved!") + encrypted, err := crypt.Encrypt(updatedData, Key) + + if err != nil { + logger.Critical(err.Error()) + } + + err = utils.WriteToFile(filename, encrypted) + if err != nil { + logger.Critical(err.Error()) + } + logger.Info(filename + " saved!") + } else { + cliInput, err := cli.CaptureInputFromEditor() + if err != nil { + logger.Critical(err.Error()) + } + + Key := DefaultKey + if options[2] != "" { + Key = options[2] + } + + Encrypted, err := crypt.Encrypt(cliInput, Key) + if err != nil { + logger.Critical(err.Error()) + } + + err = utils.WriteToFile(filename, Encrypted) + if err != nil { + logger.Warning("saving failed ...") + + Decrypted, decErr := crypt.Decrypt(Encrypted, Key) + if decErr != nil { + logger.Critical(err.Error()) + } + + logger.Output(string(Decrypted)) + + logger.Critical(err.Error()) + } + + logger.Info(filename + " saved!") + } } else if options[0] == "-r" { // read file functions filename := options[1] diff --git a/utils/utils.go b/utils/utils.go index b880522..7c78ba1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io/ioutil" + "os" "os/exec" "strings" ) @@ -90,3 +91,13 @@ func ArgParse(args []string) ([]string, error) { return []string{"", "", ""}, errors.New("invalid option, use -h to see available options ") } } + +// checks if the file name provided exists or not +func FileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + diff --git a/version b/version index 0408c30..8b3a022 100644 --- a/version +++ b/version @@ -1 +1 @@ -v1.2.0 \ No newline at end of file +v1.3.0 \ No newline at end of file