This tool is based on an original command line interface I wrote for interfacing with AWS utilities within a proprietary project. It has been recreated as a shell so I may reuse it again in other projects.
The tool's primary purpose was to allow for an ease of switching between AWS roles. The codebase grew to support extensions to interface with other AWS services. Two samples have been included based on previous ideas, an SSH utility and an ElasticSearch utility.
The CLI is runnable as an executable binary. It can also be run from soure code using the "go run" command. Note that the binary must be built for the specific OS in which it is intended to be run against.
To use the above command you need to init the cli as a Go module named "clitool". Use the following command to do so:
go mod init clitool
Simply write the command and its flag arguments after the executable to execute the command
./clitool kssh -t green -a Frontend -e Dev
go run clitool kssh -t green -a Frontend -e Dev
clitool> kssh -t green -a Frontend -e Dev
Pass in "help" as an argument to see the list of commands.
go run clitool.go help
To run the tool in interactive mode, pass in the "-i" flag.
clitool -i
Each utility is defined as a cmd inside the "cmd" directory. A command can be any Go code and the intention of the command is left up to the implementer and use case.
The CLI has been written using a Command registry pattern so that the CLI may be easily extended. To create a custom command:
- Create a directory for your command in the cmd directory.
- Implement the Cmd struct from CmdRegistry where name is the desired Name of your command and RunCmd is the main entry point to your logic.
- Creating a FlagSet for your command is not required, but highly recommended to work well with the CLI and be involved during the help function. The Usage(), Name(), and PrintDefaults() functions will be used by the main help command.
- In the init function of your command, call the RegisterCmd and RegisterFlagSet from the CmdRegistry. This will be used by the CLIs main code to identify your command when called and run its main code as you've specified it.
- Finally, import your command within clitool.go into the unused variable. When the CLI is run, it will call the init function of your command, thus registering it with the CmdRegistry, and allow the CLI to execute its functionality as described above.
Note: Go Plugins could have more easily been used to replicate the above behavior but at the time of this writing, plugins are not supported on Windows.
Create proper help printout from command.Improve help printout behavior to iterate over every commands in the commandss directory and use the flagset within each commands to print help information.- Create a proper test functionality and test cases.
Properly process '-h' and '-help' flags for commandss.Figure out how to use "usage" command for each commands.Improve use of 'Usage' for each commands.Work on other commands.Update readme to include examples.Update readme to include information on writing new commands.Create non-interactive commands processing.Create utils for common callsAdd general error handling for errors not appropriately handled in command.- Print something cool when the CLI starts in interactive mode.
Consider a builder pattern for creating commands. Using the builder will reduce the neccessity for code or patterns to be repeated across different commands.- Appropriately configure a linter to ensure code conforms to pattern on every commit
- Setup git hooks when building the CLI binary to use linter
- Figure out cleaner way to print help info for each command (using the FlagSet for each command from Main was not working)
- Refactor Assume to set env variables and use the default credentials to assume the role (with the option of passing in a profile optional)
- Move elasticsearch cluster list to external config
- Clean up elastic code a bit
- For KSSH, make system user (i.e. ubuntu) a user-input variable with the default as Ubuntu
- Pull AWS Region (in utils.go) from config file or profile