clibase

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

clibase

Clibase provides the basic features and functionality common to all of my Cobra Command based Golang projects.

Features

  • Generate and return a standard root command
  • Add standard flags and subcommands to an existing root command
  • Functions for setting Cobra Command flag values based on environment variables
  • A version subcommand that will print out all the imported packages, including overrides specified in go.mod (at the time of compilation)
  • Logrus configuration and top level logging related flags (like log-format and log-level)

Installation

go get -u github.com/SkyMack/clibase

Usage

package main

import (
	"github.com/SkyMack/clibase"
	log "github.com/sirupsen/logrus"
)

const (
	appName        = "thumbnailer"
	appDescription = "Generates sequentially numbered thumbnail images based on the specified image and text settings."
)

func main() {
	// Create a new, standard root command
	rootCmd := clibase.New(appName, appDescription)

	// Add application specific subcommands
	AddCmdGeneratePng(rootCmd)

	// Execute command
	if err := rootCmd.Execute(); err != nil {
		log.WithFields(
			log.Fields{
				"app.name": appName,
				"error":    err.Error(),
			},
		).Fatal("application exited with an error")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrorLogInitFailure is the error logged when the initial log configuration setup fails
	ErrorLogInitFailure = fmt.Errorf("failure during logging init")
	// ErrorLogLevelParse is the error logged when the specified log level cannot be parsed
	ErrorLogLevelParse = fmt.Errorf("unable to parse specified log level")
	// ErrorLogUnknownFormat is the error logged when an unrecognized log format is specified
	ErrorLogUnknownFormat = fmt.Errorf("unknown log format specified")
)
View Source
var (
	// ErrorFlagCannotRetrieve is the error logged when attempting to retrieve the value of a flag fails
	ErrorFlagCannotRetrieve = fmt.Errorf("cannot retrieve flag value")
)

Functions

func AddTopLevelFlags

func AddTopLevelFlags(flags *pflag.FlagSet)

AddTopLevelFlags takes a pointer to an existing pflag.FlagSet and adds the default top level flags to it

func EnvNameForFlag

func EnvNameForFlag(prefix string, flag *pflag.Flag) string

EnvNameForFlag generates an expected environment variable name, given the provided prefix and flag

func InitLogging added in v0.0.2

func InitLogging()

func LogFlagError

func LogFlagError(flagName string, err error)

LogFlagError generates a log entry for an error related to retrieving a flag value

func New

func New(cmdName, cmdDescription string) *cobra.Command

New returns a new Cobra root command with all the defaults

func NewUsingCmd

func NewUsingCmd(rootCmd *cobra.Command) *cobra.Command

NewUsingCmd takes an existing Cobra command and adds in the default flags, subcommands, and Init/Run entries

func SetFlagsFromEnv

func SetFlagsFromEnv(prefix string, flags *pflag.FlagSet)

SetFlagsFromEnv calls SetFlagsFromEnvWithOverrides and passes an empty overrides map

func SetFlagsFromEnvWithOverrides

func SetFlagsFromEnvWithOverrides(prefix string, flagSet *pflag.FlagSet, overrides map[string]string)

SetFlagsFromEnvWithOverrides sets the default value for each flag in the flagset based on a matching environment variable. The expected env var name will be the flag's name, all uppercase, with hyphens replaced by underscores (i.e. flag "foo-bar" will be matched with env var "FOO_BAR") If set, prefix will be appended to the expected name of each env variable (i.e. prefix of baz and flag "foobar" will be marched with env var "BAZ_FOOBAR") If overrides has a key that matches the flag name, the value of that key will be used as the expected env var name (i.e. override entry of "foobar=MY_SPECIAL_FLAG" will result in the flag "foobar" being matched with the env var "MY_SPECIAL_FLAG") Override is for when you want all the flags to have a prefix, but need some specific flags to not use that prefix (or have different env vars entirely). For example, having the application recognize a standard env var like "REDIS_URL" while prefacing most app specific flags with APPNAME_

Types

This section is empty.

Jump to

Keyboard shortcuts

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