encvim/main.go
main.goRaw
1package main
2
3import (
4 "fmt"
5 "io/ioutil"
6 "log"
7 "os"
8 "os/exec"
9 "strings"
10
11 "github.com/fatih/color"
12)
13
14func main() {
15 if len(os.Args) <= 1 {
16 color.Red("not enough arguments")
17 os.Exit(0)
18 }
19
20 filename := os.Args[1]
21
22 _, err := readFile(filename)
23 var out []byte
24 if err == nil {
25 //file exists
26 //open the current existing file, decrypts it, and save it to tempfile
27 //decrypt file
28 out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -d -a -in "+filename+" -out tempfile").Output()
29 if err != nil {
30 cmd := exec.Command("bash", "-c", "rm tempfile")
31 cmd.Run()
32 log.Fatal(err)
33 }
34 fmt.Println(out)
35 }
36 //open or create a tempfile to edit
37 cmd := exec.Command("bash", "-c", "vim tempfile")
38 cmd.Stdout = os.Stdout
39 cmd.Stdin = os.Stdin
40 cmd.Stderr = os.Stderr
41 cmd.Run()
42
43 if !strings.Contains(filename, ".encvim") {
44 filename = filename + ".encvim"
45 }
46 //encrypt the file
47 out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -a -salt -in tempfile -out "+filename).Output()
48 if err != nil {
49 log.Fatal(err)
50 }
51 fmt.Println(out)
52
53 //delete tempfile
54 cmd = exec.Command("bash", "-c", "rm tempfile")
55 cmd.Run()
56
57 fmt.Println(filename)
58
59}
60func readFile(path string) (string, error) {
61 b, err := ioutil.ReadFile(path)
62 return string(b), err
63}