Add passwd --stdin
This commit is contained in:
parent
c8227b405d
commit
ff943704c4
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -16,9 +17,12 @@ var passwdCmd = &cobra.Command{
|
|||||||
|
|
||||||
Access through the command line should be controlled by standard filesystem
|
Access through the command line should be controlled by standard filesystem
|
||||||
permissions.
|
permissions.
|
||||||
|
|
||||||
|
passwd expects to be run from a tty. If you want to pipe the password in, use
|
||||||
|
the --stdin flag.
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
passwd(boolArg(cmd, "verify"))
|
passwd(boolArg(cmd, "verify"), boolArg(cmd, "stdin"))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,19 +30,33 @@ func init() {
|
|||||||
rootCmd.AddCommand(passwdCmd)
|
rootCmd.AddCommand(passwdCmd)
|
||||||
|
|
||||||
passwdCmd.Flags().Bool("verify", false, "Compare the input password instead of setting it.")
|
passwdCmd.Flags().Bool("verify", false, "Compare the input password instead of setting it.")
|
||||||
|
passwdCmd.Flags().Bool("stdin", false, "Read the password from stdin. Terminal whitespace is stripped.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func passwd(verify bool) {
|
func passwd(verify bool, stdin bool) {
|
||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
|
var password string
|
||||||
|
if stdin {
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
password = scanner.Text()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if scanner.Err() != nil {
|
||||||
|
fmt.Printf("error: failed to read password: %v", scanner.Err())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
fmt.Print("Enter your password: ")
|
fmt.Print("Enter your password: ")
|
||||||
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: failed to read password: %v\n", err)
|
fmt.Printf("error: failed to read password: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
password := string(bytes)
|
password = string(bytes)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
if verify {
|
if verify {
|
||||||
match, err := core.CheckPassword(db, password)
|
match, err := core.CheckPassword(db, password)
|
||||||
@ -55,7 +73,7 @@ func passwd(verify bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = core.SetPassword(db, password)
|
err := core.SetPassword(db, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to set password: %v\n", err)
|
fmt.Printf("failed to set password: %v\n", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user