Skip to content

Commit

Permalink
Merge pull request #71 from tooploox/bdymowski/oya-secrets-for-windows
Browse files Browse the repository at this point in the history
Oya secrets for windows
  • Loading branch information
bart84ek authored Jun 12, 2019
2 parents 64439e3 + 19e2da6 commit 0335ee7
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Oyafile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ lintFix: |
release: |
go get -u github.com/mitchellh/gox
go get -u github.com/tcnksm/ghr
gox -os="linux darwin" -arch="amd64 386" -output="dist/oya_${CIRCLE_TAG}_{{.OS}}_{{.Arch}}" -ldflags="-X main.buildVersion=${CIRCLE_TAG}"
gox -os="linux darwin windows" -arch="amd64 386" -output="dist/oya_${CIRCLE_TAG}_{{.OS}}_{{.Arch}}" -ldflags="-X main.buildVersion=${CIRCLE_TAG}"
gzip dist/*
sha256sum dist/oya_${CIRCLE_TAG}_{darwin,linux}_* | tee dist/oya_${CIRCLE_TAG}_SHA256SUMS
if [ "$CIRCLE_TAG" ]; then
Expand Down
63 changes: 6 additions & 57 deletions cmd/internal/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ package internal

import (
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/kr/pty"
"github.com/pkg/errors"
"github.com/tooploox/oya/pkg/secrets"
"golang.org/x/crypto/ssh/terminal"
)

func SecretsView(path string, stdout, stderr io.Writer) error {
Expand All @@ -26,12 +20,12 @@ func SecretsView(path string, stdout, stderr io.Writer) error {
return nil
}

func SecretsEdit(path string, stdout, stderr io.Writer) error {
viewCmd := secrets.ViewCmd(path)
if err := terminalRun(viewCmd); err != nil {
return err
}
return nil
func SecretsEdit(filename string, stdout, stderr io.Writer) error {
cmd := secrets.ViewCmd(filename)
cmd.Stdin = os.Stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
}

func SecretsEncrypt(path string, stdout, stderr io.Writer) error {
Expand All @@ -40,48 +34,3 @@ func SecretsEncrypt(path string, stdout, stderr io.Writer) error {
}
return nil
}

func terminalRun(cmd *exec.Cmd) error {
// Start the command with a pty.
ptmx, err := pty.Start(cmd)
if err != nil {
return err
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.

// Handle pty size.
ch := make(chan os.Signal, 1)
defer close(ch)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Printf("error resizing pty: %s", err)
}
}
}()
ch <- syscall.SIGWINCH // Initial resize.

// Set stdin in raw mode.
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return err
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

// Copy stdin to the pty and the pty to stdout.
go func() {
_, err = io.Copy(ptmx, os.Stdin)
if err != nil {
log.Printf("error %s", err)
}
}()

_, err = io.Copy(os.Stdout, ptmx)
if err != nil {
return err
}

return nil
}
6 changes: 1 addition & 5 deletions cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ var secretsEditCmd = &cobra.Command{
Short: "Edit secrets file",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
path, err := filepath.Abs(args[0])
if err != nil {
return err
}
return internal.SecretsEdit(path,
return internal.SecretsEdit(args[0],
cmd.OutOrStdout(), cmd.OutOrStderr())
},
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/gobwas/glob v0.2.3
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/google/pprof v0.0.0-20190208070709-b421f19a5c07 // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
github.com/google/uuid v1.1.0 // indirect
github.com/goware/prefixer v0.0.0-20160118172347-395022866408 // indirect
github.com/hashicorp/go-cleanhttp v0.5.0
Expand All @@ -46,6 +46,7 @@ require (
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pty v1.1.3
github.com/lib/pq v1.0.0 // indirect
github.com/magefile/mage v0.0.0-20180920151403-1426fb6b9761
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/pprof v0.0.0-20190208070709-b421f19a5c07 h1:a8gLxYPNyi4nj8mRSyv71dzsQgGDEOo4Fg4nWcyUBto=
github.com/google/pprof v0.0.0-20190208070709-b421f19a5c07/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -107,6 +108,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68U
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand All @@ -118,6 +121,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
4 changes: 2 additions & 2 deletions pkg/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func Encrypt(inputPath, outputPath string) error {
return nil
}

func ViewCmd(path string) *exec.Cmd {
return exec.Command("sops", path)
func ViewCmd(filename string) *exec.Cmd {
return exec.Command("sops", filename)
}

func isSopsFile(path string) (bool, error) {
Expand Down

0 comments on commit 0335ee7

Please sign in to comment.