Skip to content

Commit

Permalink
Read password from terminal if required
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 27, 2018
1 parent 4751fcd commit 538d78f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,16 @@ and Windows Credentials Manager (on Windows) via the

Set passwords with:

$ chezmoi keyring set --service=<service> --user=<user> --password=<password>
$ chezmoi keyring set --service=<service> --user=<user>
Password: xxxxxxxx

The password can then be used in templates using the `keyring` function which
takes the service and user as arguments.

For example, save a Github access token in keyring with:

$ chezmoi keyring set --service=github --user=$GITHUB_USERNAME --password=xxxxxxxx
$ chezmoi keyring set --service=github --user=$GITHUB_USERNAME
Password: xxxxxxxx

and then include it in your `~/.gitconfig` file with:

Expand Down
16 changes: 14 additions & 2 deletions cmd/keyring_set.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cmd

import (
"fmt"
"syscall"

"github.com/spf13/cobra"
"github.com/twpayne/go-vfs"
"github.com/zalando/go-keyring"
"golang.org/x/crypto/ssh/terminal"
)

var keyringSetCommand = &cobra.Command{
Expand All @@ -18,9 +22,17 @@ func init() {

persistentFlags := keyringSetCommand.PersistentFlags()
persistentFlags.StringVar(&config.Keyring.Password, "password", "", "password")
keyringSetCommand.MarkPersistentFlagRequired("password")
}

func (c *Config) runKeyringSetCommand(fs vfs.FS, cmd *cobra.Command, args []string) error {
return keyring.Set(c.Keyring.Service, c.Keyring.User, c.Keyring.Password)
passwordString := c.Keyring.Password
if passwordString == "" {
fmt.Print("Password: ")
password, err := terminal.ReadPassword(syscall.Stdin)
if err != nil {
return err
}
passwordString = string(password)
}
return keyring.Set(c.Keyring.Service, c.Keyring.User, passwordString)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ require (
github.com/stretchr/testify v1.2.2 // indirect
github.com/twpayne/go-vfs v0.1.1
github.com/zalando/go-keyring v0.0.0-20180221093347-6d81c293b3fb
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/twpayne/go-vfs v0.1.1 h1:5HFYyah2qxr+b2SDlF6ExZScSgjMvAzhjrnwxKLhI34=
github.com/twpayne/go-vfs v0.1.1/go.mod h1:OIXA6zWkcn7Jk46XT7ceYqBMeIkfzJ8WOBhGJM0W4y8=
github.com/zalando/go-keyring v0.0.0-20180221093347-6d81c293b3fb h1:tXbazu9ZlecQbyCczvA22mWj+lw/36Bdwxapk8v7e7s=
github.com/zalando/go-keyring v0.0.0-20180221093347-6d81c293b3fb/go.mod h1:XlXBIfkGawHNVOHlenOaBW7zlfCh8LovwjOgjamYnkQ=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down

0 comments on commit 538d78f

Please sign in to comment.