-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI interface setup and main/root command
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
1 parent
5aa483e
commit b1ed2cc
Showing
5 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |