Skip to content

Commit e1f78df

Browse files
author
Chris Gilmer
committed
Setup a version flag for command to print before flag parsing errors. Remove flag.PrintErrors which was redundant for how we log errors from the output. Include more comments on the code
1 parent ef79c5a commit e1f78df

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cmd/main.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
const maxNumAccessKeys = 2
2626
const maxMFATokenPromptAttempts = 5
2727

28+
// version is the published version of the utility
29+
var version string
30+
2831
var validate *validator.Validate
2932

3033
// MFATokenPair holds two MFA tokens for enabling virtual
@@ -41,6 +44,7 @@ type cliOptions struct {
4144
IAMUser string `required:"true" long:"iam-user" description:"The IAM user name"`
4245
Role string `required:"true" long:"role" description:"The user role type"`
4346
Output string `long:"output" default:"json" description:"The AWS CLI output format"`
47+
Version bool `long:"version" description:"Print the version and exit"`
4448
}
4549

4650
// User holds information for the AWS user being configured by this script
@@ -485,13 +489,29 @@ func getPartition(region string) (string, error) {
485489
func main() {
486490
// parse command line flags
487491
var options cliOptions
488-
parser := flags.NewParser(&options, flags.Default)
492+
parser := flags.NewParser(&options, flags.HelpFlag|flags.PassDoubleDash)
489493

490-
_, err := parser.Parse()
491-
if err != nil {
492-
log.Fatal(err)
494+
// Parse the command line flags
495+
_, errParse := parser.Parse()
496+
497+
// Print the version and exit
498+
if options.Version {
499+
// Disable output not related to version before printing
500+
log.SetFlags(0)
501+
if len(version) == 0 {
502+
log.Println("development")
503+
} else {
504+
log.Println(version)
505+
}
506+
os.Exit(0)
507+
}
508+
509+
// Manage parse errors after checking for version
510+
if errParse != nil {
511+
log.Fatal(errParse)
493512
}
494513

514+
// Validator used to validate input options for MFA
495515
validate = validator.New()
496516

497517
// initialize things

0 commit comments

Comments
 (0)