From 443cfa99194a2a8a48cf3089dcde30ca36d11f9f Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 10 Sep 2019 12:27:38 -0400 Subject: [PATCH] Refactor platform-dependent code. --- .gitignore | 1 + impl_darwin.go | 86 -------------------------------------------------- impl_linux.go | 16 ++++++++++ impl_shared.go | 76 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 86 deletions(-) create mode 100644 impl_linux.go create mode 100644 impl_shared.go diff --git a/.gitignore b/.gitignore index 003da32..2925ed9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ cmd/passgo/passgo cmd/passgo-gui/passgo-gui nohup.out +*.apk diff --git a/impl_darwin.go b/impl_darwin.go index db3e260..0ac1cf1 100644 --- a/impl_darwin.go +++ b/impl_darwin.go @@ -13,76 +13,12 @@ import ( "path" "strconv" "strings" - "sync" "golang.org/x/crypto/openpgp" - "github.com/fsnotify/fsnotify" "github.com/jcmdev0/gpgagent" ) -func (s *Store) nativeDecrypt(name string) (string, error) { - fmt.Println("calling gpg -d") - file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, "")) - if _, err := os.Stat(file); os.IsNotExist(err) { - return "", fmt.Errorf("Not in password store.") - } - output, err := exec.Command("gpg", "-d", file).Output() - if err != nil { - return "", fmt.Errorf("Error running gpg: %s", err) - } - return string(output[:len(output)-1]), nil -} - -func (s *Store) nativeEncrypt(pw string) ([]byte, error) { - if s.Id == "" { - return nil, fmt.Errorf("No ID") - } - fmt.Printf("Calling gpg -e -r %s\n", s.Id) - cmd := exec.Command("gpg", "-e", "-r", s.Id) - cmd.Stdin = strings.NewReader(pw + "\n") - output, err := cmd.Output() - if err != nil { - fmt.Printf("Error running GPG: %s\n", err) - return nil, err - } - fmt.Println("success") - return output, nil -} - -var ( - idMux sync.Mutex - cachedIdentities []string -) - -func nativeIdentities() ([]string, error) { - idMux.Lock() - defer idMux.Unlock() - if cachedIdentities != nil { - ret := make([]string, len(cachedIdentities)) - copy(ret, cachedIdentities) - return ret, nil - } - ret := make([]string, 0) - fmt.Println("calling gpg") - output, err := exec.Command("gpg", "--list-secret-keys", "--with-colons").Output() - if err != nil { - return nil, fmt.Errorf("Error running gpg: %s", err) - } - scanner := bufio.NewScanner(bytes.NewBuffer(output)) - for scanner.Scan() { - fs := strings.Split(scanner.Text(), ":") - if fs[0] == "uid" { - fmt.Printf("%s: %s\n", fs[0], fs[9]) - ret = append(ret, fs[9]) - } - } - if len(ret) > 0 { - cachedIdentities = ret - } - return ret, nil -} - func setAgentInfo() { // get UID of current user usr, err := user.Current() @@ -180,28 +116,6 @@ func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) { func init() { setAgentInfo() - go func() { - watcher, err := fsnotify.NewWatcher() - if err != nil { - fmt.Printf("Cannot create filesystem watcher: %s\n", err) - return - } - u, err := user.Current() - if err != nil { - fmt.Printf("Cannot get current user: %s\n", err) - return - } - watcher.Add(path.Join(u.HomeDir, ".gnupg")) - fmt.Println("Watching ", path.Join(u.HomeDir, ".gnupg")) - for { - select { - case <-watcher.Events: - idMux.Lock() - cachedIdentities = nil - idMux.Unlock() - } - } - }() } //Clip copies a string to the clipboard diff --git a/impl_linux.go b/impl_linux.go new file mode 100644 index 0000000..a78ea00 --- /dev/null +++ b/impl_linux.go @@ -0,0 +1,16 @@ +//+build linux + +package passgo + +import ( + "bytes" + "os/exec" +) + +//Clip copies a string to the clipboard +func Clip(x string) { + b := bytes.NewBuffer([]byte(x)) + cmd := exec.Command("xclip") + cmd.Stdin = b + cmd.Run() +} diff --git a/impl_shared.go b/impl_shared.go new file mode 100644 index 0000000..4643519 --- /dev/null +++ b/impl_shared.go @@ -0,0 +1,76 @@ +//+build darwin linux + +package passgo + +import ( + "bufio" + "bytes" + "fmt" + "os" + "os/exec" + "path" + "strings" + "sync" +) + +func (s *Store) nativeDecrypt(name string) (string, error) { + fmt.Println("calling gpg -d") + file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, "")) + if _, err := os.Stat(file); os.IsNotExist(err) { + return "", fmt.Errorf("Not in password store.") + } + output, err := exec.Command("gpg", "-d", file).Output() + if err != nil { + return "", fmt.Errorf("Error running gpg: %s", err) + } + return string(output[:len(output)-1]), nil +} + +func (s *Store) nativeEncrypt(pw string) ([]byte, error) { + if s.Id == "" { + return nil, fmt.Errorf("No ID") + } + fmt.Printf("Calling gpg -e -r %s\n", s.Id) + cmd := exec.Command("gpg", "-e", "-r", s.Id) + cmd.Stdin = strings.NewReader(pw + "\n") + output, err := cmd.Output() + if err != nil { + fmt.Printf("Error running GPG: %s\n", err) + return nil, err + } + fmt.Println("success") + return output, nil +} + +var ( + idMux sync.Mutex + cachedIdentities []string +) + +func nativeIdentities() ([]string, error) { + idMux.Lock() + defer idMux.Unlock() + if cachedIdentities != nil { + ret := make([]string, len(cachedIdentities)) + copy(ret, cachedIdentities) + return ret, nil + } + ret := make([]string, 0) + fmt.Println("calling gpg") + output, err := exec.Command("gpg", "--list-secret-keys", "--with-colons").Output() + if err != nil { + return nil, fmt.Errorf("Error running gpg: %s", err) + } + scanner := bufio.NewScanner(bytes.NewBuffer(output)) + for scanner.Scan() { + fs := strings.Split(scanner.Text(), ":") + if fs[0] == "uid" { + fmt.Printf("%s: %s\n", fs[0], fs[9]) + ret = append(ret, fs[9]) + } + } + if len(ret) > 0 { + cachedIdentities = ret + } + return ret, nil +}