Refactor platform-dependent code.

This commit is contained in:
Greg 2019-09-10 12:27:38 -04:00
parent 12b57d263f
commit 443cfa9919
4 changed files with 93 additions and 86 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
cmd/passgo/passgo cmd/passgo/passgo
cmd/passgo-gui/passgo-gui cmd/passgo-gui/passgo-gui
nohup.out nohup.out
*.apk

View File

@ -13,76 +13,12 @@ import (
"path" "path"
"strconv" "strconv"
"strings" "strings"
"sync"
"golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp"
"github.com/fsnotify/fsnotify"
"github.com/jcmdev0/gpgagent" "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() { func setAgentInfo() {
// get UID of current user // get UID of current user
usr, err := user.Current() usr, err := user.Current()
@ -180,28 +116,6 @@ func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) {
func init() { func init() {
setAgentInfo() 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 //Clip copies a string to the clipboard

16
impl_linux.go Normal file
View File

@ -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()
}

76
impl_shared.go Normal file
View File

@ -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
}