boilerplate-golang-cli

command module
v0.0.0-...-24e03d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2021 License: MIT Imports: 4 Imported by: 0

README

boilerplate-golang-cli

The goal of this code is to give a basic framework for a golang CLI that deals with the scaffolding of configuration, logging and state.

Configuration is managed parameters from command-line, config file and environment variables. This is done using cobra viper and XDG for standard directories configuration. If the same parameters are provided through different routes then command-line parameters take precedence, then environment variables, then config file values, then finally any flag defaults specified in the code. Config parameters are then accessed through viper.Get methods.

Environment variables are automatically bound to Viper config strings with naming system APPNAME_PARAMETERNAME (e.g. EXAMPLEAPP_FLAGPARAM).

Also includes an example implementation of a Cobra flag with restricted valid values (Enum-like). This is just included so I don't have to work out how to do it again.

This framework will create persistent filesystem objects. An empty config file and directory structure are automatically created in a location specified by the --config flag, which defaults to $XDG_CONFIG_HOME/appName/config.yaml with $XDG_CONFIG_HOME defaulting to ~/.config.

Commands and subcommands are managed through cobra. Instead of the default cobra approach of package global variables, this builds a set of command methods. Building the command and subcommand tree is left as a manual exercise but is fairly straightforward, examples are given. Do not try using the cobra CLI!

Logging is provided through logrus. This can easily be changed if desired.

Application logic is separated from the CLI, allowing for multiple clients to the same application logic (e.g. HTTP client as well as CLI).

The framework includes a SQLite database for locally persisted application state. It uses appdb, a small library that adds some integrity checks to opening and creating application databases. This can easily be removed if the specific application doesn't require one. The assumption is that the user will manually run a one-off "init" command to initialize the database and any other application state (such as config choices), but this could be automated if desired.

Probably thousands of these on github, but this one is mine.

Examples:
EXAMPLEAPP_FLAGPARAM=42 ./exampleapp example subcommand foo
./exampleapp init --loglevel info

Documentation

Overview

Copyright © 2021 Andrew Mobbs <andrew.mobbs@gmail.com>

Permission is hereby granted, free of charge, any person obtaining a copy of this software and associated documentation files (the "Software"), deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL