You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.2 KiB

package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
)
func main() {
if len(os.Args) <= 1 {
color.Red("not enough arguments")
os.Exit(0)
}
filename := os.Args[1]
_, err := readFile(filename)
var out []byte
if err == nil {
//file exists
//open the current existing file, decrypts it, and save it to tempfile
//decrypt file
out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -d -a -in "+filename+" -out tempfile").Output()
if err != nil {
cmd := exec.Command("bash", "-c", "rm tempfile")
cmd.Run()
log.Fatal(err)
}
fmt.Println(out)
}
//open or create a tempfile to edit
cmd := exec.Command("bash", "-c", "vim tempfile")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
if !strings.Contains(filename, ".encvim") {
filename = filename + ".encvim"
}
//encrypt the file
out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -a -salt -in tempfile -out "+filename).Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
//delete tempfile
cmd = exec.Command("bash", "-c", "rm tempfile")
cmd.Run()
fmt.Println(filename)
}
func readFile(path string) (string, error) {
b, err := ioutil.ReadFile(path)
return string(b), err
}