Skip to content

Commit

Permalink
CLI interface setup and main/root command
Browse files Browse the repository at this point in the history
The root command `snowsaw` represents the main entry point of the
application that

- registers all subcommands and global flags.
- initializes the application logging/printer (GH-59) verbosity level.
- checks for existing application-wide configuration files to load and
  merges them with the given flag parameters.

The `info` subcommand prints more detailed application information
while the `--version` flag can be used to obtain the version number in a
parsable format.

The `main` function in the `main` package has been placed in the
`main.go` in the repository root. It calls `Run()` of the `snowsaw`
command to start the main application flow.

Epic GH-33
Depends on GH-58
Resolves GH-61
  • Loading branch information
arcticicestudio committed Jun 26, 2019
1 parent 5aa483e commit b1ed2cc
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 1 deletion.
44 changes: 44 additions & 0 deletions cmd/snowsaw/info/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2017-present Arctic Ice Studio <development@arcticicestudio.com>
// Copyright (C) 2017-present Sven Greb <development@svengreb.de>
//
// Project: snowsaw
// Repository: https://github.com/arcticicestudio/snowsaw
// License: MIT

// Author: Arctic Ice Studio <development@arcticicestudio.com>
// Author: Sven Greb <development@svengreb.de>
// Since: 0.4.0

// Package info provides the info command to print more detailed application information.
package info

import (
"fmt"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/arcticicestudio/snowsaw/pkg/config"
)

// NewInfoCmd creates and configures a new `info` command.
func NewInfoCmd() *cobra.Command {
infoCmd := &cobra.Command{
Use: "info",
Short: "Prints more detailed application information",
Run: func(cmd *cobra.Command, args []string) {
if config.BuildDateTime != "" {
fmt.Println(fmt.Sprintf("%s %s (build %s)",
color.CyanString(config.ProjectName),
color.BlueString(config.Version),
color.GreenString(config.BuildDateTime)))
} else {
fmt.Println(fmt.Sprintf("%s %s",
color.CyanString(config.ProjectName),
color.BlueString(config.Version)))
}
},
}

return infoCmd
}
115 changes: 115 additions & 0 deletions cmd/snowsaw/snowsaw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (C) 2017-present Arctic Ice Studio <development@arcticicestudio.com>
// Copyright (C) 2017-present Sven Greb <development@svengreb.de>
//
// Project: snowsaw
// Repository: https://github.com/arcticicestudio/snowsaw
// License: MIT

// Author: Arctic Ice Studio <development@arcticicestudio.com>
// Author: Sven Greb <development@svengreb.de>
// Since: 0.4.0

// Package snowsaw provides the root command of the application and bootstraps the startup.
package snowsaw

import (
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/arcticicestudio/snowsaw/cmd/snowsaw/info"
"github.com/arcticicestudio/snowsaw/pkg/config"
"github.com/arcticicestudio/snowsaw/pkg/config/builder"
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
"github.com/arcticicestudio/snowsaw/pkg/prt"
)

var (
// debug indicates if the `debug` flag has been set to enable configure the logging for the debug scope.
debug bool
// explicitConfigFilePath stores the path to the application configuration file when the `config` flag is specified.
explicitConfigFilePath string
)

// rootCmd is the root command of the application.
var rootCmd = &cobra.Command{
Use: config.ProjectName,
Short: "A lightweight, plugin-driven and dynamic dotfiles bootstrapper.",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
prt.Errorf("Failed to run %s: %v", config.ProjectName, err)
os.Exit(1)
}
},
}

// Run is the main application function that adds all child commands to the root command and sets flags appropriately.
// This is called by `main.main()` and only needs to be run once for the root command.
func Run() {
// Disable verbose errors to provide custom formatted CLI output via application-wide printer.
rootCmd.SilenceErrors = true

// Run the application with the given commands, flags and arguments and exit on any (downstream) error.
if err := rootCmd.Execute(); err != nil {
prt.Errorf(err.Error())
os.Exit(1)
}
}

func init() {
// Specify the functions to be run before each command gets executed.
cobra.OnInitialize(initDebugScope, initConfig, initPrinter)

// Define global application flags.
rootCmd.PersistentFlags().StringVar(&explicitConfigFilePath, "config", "", "set the configuration file")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug information output")

// Set the app version information for the automatically generated `version` flag.
rootCmd.Version = color.CyanString(config.Version)
rootCmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)

// Create and register all subcommands.
rootCmd.AddCommand(info.NewInfoCmd())
}

// initConfig searches and loads either the default application configuration file paths or the explicit file at the
// given path specified through the global `config` flag.
func initConfig() {
if explicitConfigFilePath != "" {
if err := builder.Load(file.NewFile(explicitConfigFilePath)).Into(&config.AppConfig); err != nil {
prt.Errorf("while loading custom application configuration file:\n%v", err)
os.Exit(1)
}
} else {
b := builder.Load(config.AppConfigPaths...)
if len(b.Files) == 0 {
prt.Debugf("No configuration files found, using default application configuration.")
}
if err := b.Into(&config.AppConfig); err != nil {
prt.Errorf("while loading application configuration files:\n%v", err)
os.Exit(1)
}
}
}

// initDebugScope configures the application when run with debug scope.
func initDebugScope() {
if debug {
prt.SetVerbosityLevel(prt.DebugVerbosity)
}
}

// setPrinterVerbosityLevel configures the global CLI printer like the verbosity level.
func initPrinter() {
lvl, err := prt.ParseVerbosityLevel(strings.ToUpper(config.AppConfig.LogLevel))
if err != nil {
prt.Debugf("Error while parsing log level from configuration: %v", err)
prt.Debugf("Using default INFO level as fallback")
prt.SetVerbosityLevel(prt.InfoVerbosity)
} else {
prt.Debugf("Using configured logger level: %s", strings.ToUpper(config.AppConfig.LogLevel))
prt.SetVerbosityLevel(lvl)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ require (
github.com/imdario/mergo v0.3.7
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mitchellh/go-homedir v1.1.0
gopkg.in/yaml.v2 v2.2.2 // indirect
github.com/spf13/cobra v0.0.5
)
30 changes: 30 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2017-present Arctic Ice Studio <development@arcticicestudio.com>
// Copyright (C) 2017-present Sven Greb <development@svengreb.de>
//
// Project: snowsaw
// Repository: https://github.com/arcticicestudio/snowsaw
// License: MIT

// Author: Arctic Ice Studio <development@arcticicestudio.com>
// Author: Sven Greb <development@svengreb.de>
// Since: 0.4.0

// A lightweight, plugin-driven and dynamic dotfiles bootstrapper.
package main

import (
"github.com/arcticicestudio/snowsaw/cmd/snowsaw"
)

func main() {
snowsaw.Run()
}

0 comments on commit b1ed2cc

Please sign in to comment.