From 819a3f4efca13db8e6d3783d08034d66815abeca Mon Sep 17 00:00:00 2001 From: lavenderguitar Date: Mon, 28 Aug 2023 17:49:18 -0400 Subject: [PATCH] Add help menu and set defaults for profile and region --- go/instances.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/go/instances.go b/go/instances.go index 7adae5f..0d6ab51 100644 --- a/go/instances.go +++ b/go/instances.go @@ -256,16 +256,30 @@ func handleEC2OperationResult(err error, successMsg string, list *tview.List, de } func main() { - // Require profile and region from the user. - flag.StringVar(&profile, "profile", "", "AWS Profile") - flag.StringVar(&profile, "p", "", "AWS Profile (shorthand)") - flag.StringVar(®ion, "region", "", "AWS Region") - flag.StringVar(®ion, "r", "", "AWS Region (shorthand)") + // Allow for profile and region from the user. Defaults provided. + // Custom usage function for the flag package. Create a CLI help flag. + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) + fmt.Println(` + This script is an AWS EC2 Instance Viewer and Manager. + View instance details, as well as stop, start, and reboot instances. + + Available options are: + + `) + flag.PrintDefaults() + } + + // AWS Profile and Region flags. Defaults provided. + flag.StringVar(&profile, "profile", "default", "AWS Profile") + flag.StringVar(&profile, "p", "default", "AWS Profile (shorthand)") + flag.StringVar(®ion, "region", "us-east-1", "AWS Region") + flag.StringVar(®ion, "r", "us-east-1", "AWS Region (shorthand)") flag.Parse() - if profile == "" || region == "" { - fmt.Println("Both --profile (-p) and --region (-r) are required.") - os.Exit(1) + // If "help" flag passed, provide usage information above and exit. + if flag.Lookup("help") != nil && flag.Lookup("help").Value.String() == "true" { + os.Exit(0) } // Establish AWS connection.