Skip to content

Commit

Permalink
Added support for upda files using -w command
Browse files Browse the repository at this point in the history
  • Loading branch information
sharma1612harshit committed Jun 9, 2020
1 parent 3a37e0d commit 69b4b39
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
88 changes: 67 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
11 changes: 11 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -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()
}

2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.0
v1.3.0

0 comments on commit 69b4b39

Please sign in to comment.