2018-03-07 15:36:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-08 10:24:33 +00:00
|
|
|
"flag"
|
2018-03-07 15:36:24 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2018-03-09 06:31:23 +00:00
|
|
|
"github.com/otakukaze/go-pgp-tool/pgpcrypt"
|
2018-03-08 10:24:33 +00:00
|
|
|
|
2018-03-09 06:31:23 +00:00
|
|
|
"github.com/otakukaze/go-pgp-tool/libs"
|
|
|
|
"github.com/otakukaze/go-pgp-tool/tools"
|
2018-03-08 10:24:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flags *libs.Flags
|
2018-03-07 15:36:24 +00:00
|
|
|
)
|
|
|
|
|
2018-03-08 10:24:33 +00:00
|
|
|
func init() {
|
|
|
|
flags = new(libs.Flags)
|
|
|
|
libs.RegFlag(flags)
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:36:24 +00:00
|
|
|
// args [0] is this
|
|
|
|
func main() {
|
2018-03-08 10:24:33 +00:00
|
|
|
// check flags value
|
|
|
|
if !flags.Encrypt && !flags.Decrypt {
|
|
|
|
showUsage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if flags.Decrypt && flags.Encrypt {
|
|
|
|
showUsage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(flags.KeyFile) == 0 {
|
|
|
|
log.Fatal("please input KeyFile path")
|
|
|
|
}
|
|
|
|
if len(flags.SrcFile) == 0 {
|
|
|
|
log.Fatal("please input SrcFile path")
|
|
|
|
}
|
|
|
|
if len(flags.DstFile) == 0 {
|
|
|
|
log.Fatal("please input DstFile path")
|
2018-03-07 15:36:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 10:24:33 +00:00
|
|
|
// check file exists
|
|
|
|
if !tools.CheckExists(flags.KeyFile, false) {
|
|
|
|
log.Fatal("KeyFile not exists")
|
|
|
|
}
|
|
|
|
if !tools.CheckExists(flags.SrcFile, false) {
|
|
|
|
log.Fatal("SrcFile not exists")
|
|
|
|
}
|
|
|
|
dir := path.Dir(flags.DstFile)
|
|
|
|
if !tools.CheckExists(dir, true) {
|
|
|
|
log.Fatal("DstFile parent directory not exists")
|
|
|
|
}
|
|
|
|
if !flags.Override && tools.CheckExists(flags.DstFile, false) {
|
|
|
|
log.Fatal("DstFile has Exists if override add flag -y ")
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:04:27 +00:00
|
|
|
flags.KeyFile = tools.ParsePath(flags.KeyFile)
|
|
|
|
flags.SrcFile = tools.ParsePath(flags.SrcFile)
|
|
|
|
flags.DstFile = tools.ParsePath(flags.DstFile)
|
|
|
|
|
2018-03-08 10:24:33 +00:00
|
|
|
if flags.Encrypt {
|
2018-03-12 09:51:59 +00:00
|
|
|
// encrypt()
|
|
|
|
keyIO, err := os.Open(flags.KeyFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
srcIO, err := os.Open(flags.SrcFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
dstIO, err := os.Create(flags.DstFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
err = pgpcrypt.Encrypt2(keyIO, srcIO, dstIO)
|
|
|
|
|
|
|
|
// err := pgpcrypt.Encrypt(flags.KeyFile, flags.SrcFile, flags.DstFile)
|
|
|
|
handleError(err)
|
2018-03-08 10:24:33 +00:00
|
|
|
}
|
2018-03-09 06:04:27 +00:00
|
|
|
if flags.Decrypt {
|
2018-03-12 09:51:59 +00:00
|
|
|
// decrypt()
|
|
|
|
keyIO, err := os.Open(flags.KeyFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
srcIO, err := os.Open(flags.SrcFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
dstIO, err := os.Create(flags.DstFile)
|
|
|
|
handleError(err)
|
|
|
|
|
|
|
|
err = pgpcrypt.Decrypt2(keyIO, srcIO, dstIO, flags.Password)
|
|
|
|
// err := pgpcrypt.Decrypt(flags.KeyFile, flags.SrcFile, flags.DstFile, flags.Password)
|
|
|
|
handleError(err)
|
2018-03-08 10:24:33 +00:00
|
|
|
}
|
2018-03-09 06:04:27 +00:00
|
|
|
}
|
2018-03-08 10:24:33 +00:00
|
|
|
|
2018-03-09 06:04:27 +00:00
|
|
|
func handleError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-03-07 15:36:24 +00:00
|
|
|
}
|
2018-03-09 06:04:27 +00:00
|
|
|
}
|
2018-03-07 15:36:24 +00:00
|
|
|
|
2018-03-09 06:04:27 +00:00
|
|
|
func showUsage() {
|
|
|
|
flag.Usage()
|
2018-03-08 10:24:33 +00:00
|
|
|
}
|