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
This commit is contained in:
Shteryana Shopova 2019-07-21 00:22:50 +03:00
parent c9dda02092
commit 61b73ceece
1 changed files with 17 additions and 14 deletions

View File

@ -29,6 +29,7 @@ func main() {
fetchPgp := parser.Flag("p", "pgp-keys", &argparse.Options{Required: false, Help: "Fetch configured PGP key ids", Default: false}) 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}) 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"}) 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 // Parse input
err := parser.Parse(os.Args) err := parser.Parse(os.Args)
@ -37,14 +38,16 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fi, err := os.Lstat(*keysDir) if *verbose == false {
if err != nil { fi, err := os.Lstat(*keysDir)
fmt.Println(*keysDir, ": target directory error :", err) if err != nil {
os.Exit(1) fmt.Println(*keysDir, ": target directory error :", err)
} else {
if fi.Mode().IsDir() == false {
fmt.Println(*keysDir, ": target directory error : not a directory - ", fi.Mode())
os.Exit(1) 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) teamMembers := fetchUsers(client, ghOrganization, ghTeam)
for _, user := range teamMembers { for _, user := range teamMembers {
if *quiet == false { if *quiet == false {
fmt.Println("Fetching keys for ", *user) fmt.Println("Fetching keys for", *user)
} }
var sshKeys bytes.Buffer var sshKeys bytes.Buffer
var pgpKeys bytes.Buffer var pgpKeys bytes.Buffer
@ -80,7 +83,7 @@ func main() {
} }
for _, key := range keys { for _, key := range keys {
if *quiet == false { if *verbose == true {
fmt.Println(*key.Key) fmt.Println(*key.Key)
} }
sshKeys.WriteString(*key.Key) sshKeys.WriteString(*key.Key)
@ -93,11 +96,11 @@ func main() {
nextPage = rsp.NextPage nextPage = rsp.NextPage
} }
if *quiet == false { if *quiet == false && *verbose == false {
fmt.Println("Writing to", *keysDir+"/"+*user+".key") fmt.Println("Writing to", *keysDir+"/"+*user+".key")
} }
err := ioutil.WriteFile(*keysDir+"/"+*user+".key", sshKeys.Bytes(), 0444) err := ioutil.WriteFile(*keysDir+"/"+*user+".key", sshKeys.Bytes(), 0444)
if err != nil { if err != nil && *verbose == false {
fmt.Println(*user+".key error ", err) fmt.Println(*user+".key error ", err)
} }
@ -118,7 +121,7 @@ func main() {
} }
for _, key := range keys { for _, key := range keys {
if *quiet == false { if *verbose == true {
fmt.Println(*key.KeyID) fmt.Println(*key.KeyID)
} }
pgpKeys.WriteString(*key.KeyID) pgpKeys.WriteString(*key.KeyID)
@ -130,11 +133,11 @@ func main() {
} }
nextPage = rsp.NextPage nextPage = rsp.NextPage
} }
if *quiet == false { if *quiet == false && *verbose == false {
fmt.Println("Writing to", *keysDir+"/"+*user+".gpg") fmt.Println("Writing to", *keysDir+"/"+*user+".gpg")
} }
err = ioutil.WriteFile(*keysDir+"/"+*user+".gpg", pgpKeys.Bytes(), 0444) err = ioutil.WriteFile(*keysDir+"/"+*user+".gpg", pgpKeys.Bytes(), 0444)
if err != nil { if err != nil && *verbose == false {
fmt.Println(*user+".gpg error ", err) fmt.Println(*user+".gpg error ", err)
} }
} }