Skip to content

Commit

Permalink
Support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Feb 3, 2017
1 parent efb0bb2 commit 5784589
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 23 deletions.
19 changes: 0 additions & 19 deletions action/clihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package action

import (
"bufio"
"crypto/sha256"
"fmt"
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"

"github.com/justwatchcom/gopass/gpg"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -51,22 +48,6 @@ func (s *Action) confirmRecipients(name string, recipients []string) ([]string,
}
}

// clearClipboard will spwan a copy of gopass that waits in a detached background
// process group until the timeout is expired. It will then compare the contents
// of the clipboard and erase it if it still contains the data gopass copied
// to it.
func clearClipboard(content []byte, timeout int) error {
hash := fmt.Sprintf("%x", sha256.Sum256(content))

cmd := exec.Command(os.Args[0], "unclip", "--timeout", strconv.Itoa(timeout))
// https://groups.google.com/d/msg/golang-nuts/shST-SDqIp4/za4oxEiVtI0J
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmd.Env = append(os.Environ(), "GOPASS_UNCLIP_CHECKSUM="+hash)
return cmd.Start()
}

// askForConfirmation asks a yes/no question until the user
// replies yes or no
func askForConfirmation(text string) bool {
Expand Down
28 changes: 28 additions & 0 deletions action/clipboard_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build !windows

package action

import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"strconv"
"syscall"
)

// clearClipboard will spwan a copy of gopass that waits in a detached background
// process group until the timeout is expired. It will then compare the contents
// of the clipboard and erase it if it still contains the data gopass copied
// to it.
func clearClipboard(content []byte, timeout int) error {
hash := fmt.Sprintf("%x", sha256.Sum256(content))

cmd := exec.Command(os.Args[0], "unclip", "--timeout", strconv.Itoa(timeout))
// https://groups.google.com/d/msg/golang-nuts/shST-SDqIp4/za4oxEiVtI0J
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmd.Env = append(os.Environ(), "GOPASS_UNCLIP_CHECKSUM="+hash)
return cmd.Start()
}
23 changes: 23 additions & 0 deletions action/clipboard_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build windows

package action

import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"strconv"
)

// clearClipboard will spwan a copy of gopass that waits in a detached background
// process group until the timeout is expired. It will then compare the contents
// of the clipboard and erase it if it still contains the data gopass copied
// to it.
func clearClipboard(content []byte, timeout int) error {
hash := fmt.Sprintf("%x", sha256.Sum256(content))

cmd := exec.Command(os.Args[0], "unclip", "--timeout", strconv.Itoa(timeout))
cmd.Env = append(os.Environ(), "GOPASS_UNCLIP_CHECKSUM="+hash)
return cmd.Start()
}
7 changes: 6 additions & 1 deletion action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (

"github.com/fatih/color"
"github.com/justwatchcom/gopass/gpg"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
)

var (
out = colorable.NewColorableStdout()
)

// Initialized returns an error if the store is not properly
// prepared.
func (s *Action) Initialized(*cli.Context) error {
Expand Down Expand Up @@ -46,7 +51,7 @@ func (s *Action) Init(c *cli.Context) error {
return err
}

fmt.Printf(color.GreenString("Password store initialized for: "))
color.Green("Password store initialized for: ")
for _, recipient := range s.Store.ListRecipients(store) {
r := "0x" + recipient
if kl, err := gpg.ListPublicKeys(recipient); err == nil && len(kl) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions fsutil/fsutil.go → fsutil/fsutil_others.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package fsutil

import (
Expand Down
70 changes: 70 additions & 0 deletions fsutil/fsutil_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// +build windows

package fsutil

import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
)

// CleanPath resolves common aliases in a path and cleans it as much as possible
func CleanPath(path string) string {
// http://stackoverflow.com/questions/17609732/expand-tilde-to-home-directory
if path[:2] == "~/" {
usr, _ := user.Current()
dir := usr.HomeDir
path = strings.Replace(path, "~/", dir+"/", 1)
}
if p, err := filepath.Abs(path); err == nil {
return p
}
return filepath.Clean(path)
}

// IsDir checks if a certain path exists and is a directory
// https://stackoverflow.com/questions/10510691/how-to-check-whether-a-file-or-directory-denoted-by-a-path-exists-in-golang
func IsDir(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
return false
}
fmt.Printf("failed to check dir %s: %s\n", path, err)
return false
}
if fi.Mode()&os.ModeSymlink != 0 {
fmt.Printf("dir %s is a symlink. ignoring", path)
return false
}

return fi.IsDir()
}

// IsFile checks if a certain path is actually a file
func IsFile(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
return false
}
fmt.Printf("failed to check dir %s: %s\n", path, err)
return false
}
if fi.Mode()&os.ModeSymlink != 0 {
fmt.Printf("dir %s is a symlink. ignoring", path)
return false
}

return fi.Mode().IsRegular()
}

// Tempdir returns a temporary directory suiteable for sensitive data. On
// Windows, just return empty string for ioutil.TempFile.
func Tempdir() string {
return ""
}
3 changes: 1 addition & 2 deletions gpg/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"regexp"
"strconv"
"strings"
"syscall"
"time"
)

Expand All @@ -24,7 +23,7 @@ const (
func init() {
// ensure created files don't have group or world perms set
// this setting should be inherited by sub-processes
syscall.Umask(077)
umask(077)
}

var (
Expand Down
9 changes: 9 additions & 0 deletions gpg/umask_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !windows

package gpg

import "syscall"

func umask(mask int) int {
return syscall.Umask(mask)
}
7 changes: 7 additions & 0 deletions gpg/umask_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows

package gpg

func umask(mask int) int {
return -1
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/fatih/color"
"github.com/justwatchcom/gopass/action"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
)

Expand All @@ -36,7 +37,7 @@ func (e errorWriter) Write(p []byte) (int, error) {

func main() {
cli.ErrWriter = errorWriter{
out: os.Stderr,
out: colorable.NewColorableStderr(),
}

cli.VersionPrinter = func(c *cli.Context) {
Expand Down

0 comments on commit 5784589

Please sign in to comment.