From fb34fbc23a8f9c48475f97bb216979d033e4e043 Mon Sep 17 00:00:00 2001 From: Mark Wolfe Date: Tue, 9 Aug 2016 09:16:00 +1000 Subject: [PATCH] Added option to print secret with no newline fixes #41. --- cmd/unicreds/main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/unicreds/main.go b/cmd/unicreds/main.go index b20dbf0..dfc3e1c 100644 --- a/cmd/unicreds/main.go +++ b/cmd/unicreds/main.go @@ -31,8 +31,9 @@ var ( cmdSetupRead = cmdSetup.Flag("read", "Dynamo read capacity.").Default("4").Int64() cmdSetupWrite = cmdSetup.Flag("write", "Dynamo write capacity.").Default("4").Int64() - cmdGet = app.Command("get", "Get a credential from the store.") - cmdGetName = cmdGet.Arg("credential", "The name of the credential to get.").Required().String() + cmdGet = app.Command("get", "Get a credential from the store.") + cmdGetName = cmdGet.Arg("credential", "The name of the credential to get.").Required().String() + cmdGetNoLine = cmdGet.Flag("noline", "Leave off the newline when emitting secret").Short('n').Bool() cmdGetAll = app.Command("getall", "Get latest credentials from the store.") cmdGetAllVersions = cmdGetAll.Flag("all", "List all versions").Bool() @@ -93,7 +94,7 @@ func main() { log.WithFields(log.Fields{"name": *cmdGetName, "secret": cred.Secret, "status": "success"}).Info(cred.Secret) } else { // Or just print, out of backwards compatibility - fmt.Println(cred.Secret) + printSecret(cred.Secret, *cmdGetNoLine) } case cmdPut.FullCommand(): @@ -179,6 +180,15 @@ func printFatalError(err error) { os.Exit(1) } +func printSecret(secret string, noline bool) { + log.WithField("noline", noline).Debug("print secret") + if noline { + fmt.Printf(secret) + } else { + fmt.Println(secret) + } +} + func printEncryptionContext(encContext *unicreds.EncryptionContextValue) { if encContext == nil || len(*encContext) == 0 { return