From 61b73ceecea92f53589f092c3629c30568126052 Mon Sep 17 00:00:00 2001 From: Shteryana Shopova Date: Sun, 21 Jul 2019 00:22:50 +0300 Subject: [PATCH] Don't dump the keys to stdout by default Include a flag to do so though, and ignore file system errors if dumping keys is enabled --- access/fetch-keys/main.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/access/fetch-keys/main.go b/access/fetch-keys/main.go index a7c141a..ea6ca35 100644 --- a/access/fetch-keys/main.go +++ b/access/fetch-keys/main.go @@ -29,6 +29,7 @@ func main() { fetchPgp := parser.Flag("p", "pgp-keys", &argparse.Options{Required: false, Help: "Fetch configured PGP key ids", Default: false}) quiet := parser.Flag("q", "quiet", &argparse.Options{Required: false, Help: "Skip output to stdout", Default: false}) ghTeam := parser.String("t", "team", &argparse.Options{Required: false, Help: "Github Team name, 'all' for all members of the organization", Default: "NOC"}) + verbose := parser.Flag("v", "verbose", &argparse.Options{Required: false, Help: "Verbose output: print keys to stdout", Default: false}) // Parse input err := parser.Parse(os.Args) @@ -37,14 +38,16 @@ func main() { os.Exit(1) } - fi, err := os.Lstat(*keysDir) - if err != nil { - fmt.Println(*keysDir, ": target directory error :", err) - os.Exit(1) - } else { - if fi.Mode().IsDir() == false { - fmt.Println(*keysDir, ": target directory error : not a directory - ", fi.Mode()) + if *verbose == false { + fi, err := os.Lstat(*keysDir) + if err != nil { + fmt.Println(*keysDir, ": target directory error :", err) os.Exit(1) + } else { + if fi.Mode().IsDir() == false { + fmt.Println(*keysDir, ": target directory error : not a directory - ", fi.Mode()) + os.Exit(1) + } } } @@ -59,7 +62,7 @@ func main() { teamMembers := fetchUsers(client, ghOrganization, ghTeam) for _, user := range teamMembers { if *quiet == false { - fmt.Println("Fetching keys for ", *user) + fmt.Println("Fetching keys for", *user) } var sshKeys bytes.Buffer var pgpKeys bytes.Buffer @@ -80,7 +83,7 @@ func main() { } for _, key := range keys { - if *quiet == false { + if *verbose == true { fmt.Println(*key.Key) } sshKeys.WriteString(*key.Key) @@ -93,11 +96,11 @@ func main() { nextPage = rsp.NextPage } - if *quiet == false { + if *quiet == false && *verbose == false { fmt.Println("Writing to", *keysDir+"/"+*user+".key") } err := ioutil.WriteFile(*keysDir+"/"+*user+".key", sshKeys.Bytes(), 0444) - if err != nil { + if err != nil && *verbose == false { fmt.Println(*user+".key error ", err) } @@ -118,7 +121,7 @@ func main() { } for _, key := range keys { - if *quiet == false { + if *verbose == true { fmt.Println(*key.KeyID) } pgpKeys.WriteString(*key.KeyID) @@ -130,11 +133,11 @@ func main() { } nextPage = rsp.NextPage } - if *quiet == false { + if *quiet == false && *verbose == false { fmt.Println("Writing to", *keysDir+"/"+*user+".gpg") } err = ioutil.WriteFile(*keysDir+"/"+*user+".gpg", pgpKeys.Bytes(), 0444) - if err != nil { + if err != nil && *verbose == false { fmt.Println(*user+".gpg error ", err) } }