Skip to content

Commit

Permalink
Support Windows (#14)
Browse files Browse the repository at this point in the history
* Support Windows
  • Loading branch information
mattn authored and dominikschulz committed Feb 21, 2017
1 parent 2f40be4 commit be6ed5b
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 44 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()
}
2 changes: 1 addition & 1 deletion action/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func (s *Action) GitInit(c *cli.Context) error {
if err := s.Store.GitInit(store, sk); err != nil {
return err
}
fmt.Println(color.GreenString("Git initialized"))
fmt.Fprintln(color.Output, color.GreenString("Git initialized"))
return nil
}
2 changes: 1 addition & 1 deletion action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Action) Init(c *cli.Context) error {
return err
}

fmt.Printf(color.GreenString("Password store initialized for: "))
fmt.Fprint(color.Output, color.GreenString("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
6 changes: 3 additions & 3 deletions action/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *Action) MountRemove(c *cli.Context) error {
return err
}

fmt.Println(color.GreenString("Password Store %s umounted", c.Args()[0]))
color.Green("Password Store %s umounted", c.Args()[0])
return nil
}

Expand All @@ -36,7 +36,7 @@ func (s *Action) MountsPrint(c *cli.Context) error {
fmt.Printf("Failed to add mount: %s\n", err)
}
}
fmt.Println(root.Format())
fmt.Fprintln(color.Output, root.Format())
return nil
}

Expand Down Expand Up @@ -65,6 +65,6 @@ func (s *Action) MountAdd(c *cli.Context) error {
return err
}

fmt.Println(color.GreenString("Mounted %s as %s", c.Args()[0], c.Args()[1]))
color.Green("Mounted %s as %s", c.Args()[0], c.Args()[1])
return nil
}
17 changes: 0 additions & 17 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os/user"
"path/filepath"
"strings"

"golang.org/x/sys/unix"
)

// CleanPath resolves common aliases in a path and cleans it as much as possible
Expand Down Expand Up @@ -62,18 +60,3 @@ func IsFile(path string) bool {

return fi.Mode().IsRegular()
}

// Tempdir returns a temporary directory suiteable for sensitive data. It tries
// /dev/shm but if this isn't working it will return an empty string. Using
// this with ioutil.Tempdir will ensure that we're getting the "best" tempdir.
func Tempdir() string {
shmDir := "/dev/shm"
if fi, err := os.Stat(shmDir); err == nil {
if fi.IsDir() {
if unix.Access(shmDir, unix.W_OK) == nil {
return shmDir
}
}
}
return ""
}
24 changes: 24 additions & 0 deletions fsutil/fsutil_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build !windows

package fsutil

import (
"os"

"golang.org/x/sys/unix"
)

// Tempdir returns a temporary directory suiteable for sensitive data. It tries
// /dev/shm but if this isn't working it will return an empty string. Using
// this with ioutil.Tempdir will ensure that we're getting the "best" tempdir.
func Tempdir() string {
shmDir := "/dev/shm"
if fi, err := os.Stat(shmDir); err == nil {
if fi.IsDir() {
if unix.Access(shmDir, unix.W_OK) == nil {
return shmDir
}
}
}
return ""
}
9 changes: 9 additions & 0 deletions fsutil/fsutil_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build windows

package fsutil

// 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 be6ed5b

Please sign in to comment.