Skip to content

Commit

Permalink
Add FatalOnError for explicitly exit on error
Browse files Browse the repository at this point in the history
Add Infof and Debug for wrapping Printf

Info now simply wraps Print

Hooked up diagnostic and root commands to new logger
  • Loading branch information
Zachary Scott committed Jun 5, 2018
1 parent 3a4d5d1 commit 4a7489c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
16 changes: 5 additions & 11 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -17,18 +15,14 @@ func diagnostic(cmd *cobra.Command, args []string) {
host := viper.GetString("host")
token := viper.GetString("token")

fmt.Printf("\n---\nCircleCI CLI Diagnostics\n---\n\n")
fmt.Printf("Config found: `%v`\n", viper.ConfigFileUsed())
Logger.Info("\n---\nCircleCI CLI Diagnostics\n---\n\n")
Logger.Infof("Config found: `%v`\n", viper.ConfigFileUsed())

if host == "host" || host == "" {
fmt.Println("Please set a host!")
} else {
fmt.Printf("Host is: %s\n", host)
}
Logger.Infof("Host is: %s\n", host)

if token == "token" || token == "" {
fmt.Println("Please set a token!")
Logger.Info("Please set a token!")
} else {
fmt.Println("OK, got a token.")
Logger.Info("OK, got a token.")
}
}
18 changes: 9 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func init() {
rootCmd.PersistentFlags().StringP("host", "H", "https://circleci.com", "the host of your CircleCI install")
rootCmd.PersistentFlags().StringP("token", "t", "", "your token for using CircleCI")

Logger.Error("Error binding host flag", viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host")))
Logger.Error("Error binding token flag", viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")))
Logger.FatalOnError("Error binding host flag", viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host")))
Logger.FatalOnError("Error binding token flag", viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")))
}

// TODO: move config stuff to it's own package
Expand All @@ -58,10 +58,10 @@ func initConfig() {
return
}

Logger.Error("Error creating a new config file", createConfig())
Logger.FatalOnError("Error creating a new config file", createConfig())

cfgFile = cfgPathDefault
Logger.Error(
Logger.FatalOnError(
"Failed to re-read config after creating a new file",
readConfig(), // reload config after creating it
)
Expand Down Expand Up @@ -96,12 +96,12 @@ func createConfig() (err error) {
path := fmt.Sprintf("%s/.circleci", os.Getenv("HOME"))

if _, err = os.Stat(path); os.IsNotExist(err) {
Logger.Error(
Logger.FatalOnError(
fmt.Sprintf("Error creating directory: '%s'", path),
os.Mkdir(path, 0644),
)
} else {
Logger.Error(fmt.Sprintf("Error accessing '%s'", path), err)
Logger.FatalOnError(fmt.Sprintf("Error accessing '%s'", path), err)
}

// Create default config file
Expand All @@ -112,9 +112,9 @@ func createConfig() (err error) {
// open file with read & write
file, err := os.OpenFile(cfgPathDefault, os.O_RDWR, 0600)
if err != nil {
Logger.Error("", err)
Logger.FatalOnError("", err)
}
defer Logger.Error("Error closing config file", file.Close())
defer Logger.FatalOnError("Error closing config file", file.Close())

// read flag values
host := viper.GetString("host")
Expand All @@ -137,7 +137,7 @@ func createConfig() (err error) {

// write new config values to file
if _, err = file.WriteString(configValues); err != nil {
Logger.Error("", err)
Logger.FatalOnError("", err)
}

Logger.Info("Your configuration has been created in `%v`.\n", cfgPathDefault)
Expand Down
20 changes: 13 additions & 7 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ func NewLogger(verbose bool) *Logger {
}
}

func (l *Logger) Debug(msg string, args ...interface{}) {
func (l *Logger) Debug(format string, args ...interface{}) {
if l.verbose {
l.debug.Printf(msg, args...)
l.debug.Printf(format, args...)
}
}

func (l *Logger) Info(msg string, args ...interface{}) {
l.info.Printf(msg, args...)
func (l *Logger) Info(args ...interface{}) {
l.info.Print(args...)
}

func (l *Logger) Infof(format string, args ...interface{}) {
l.info.Printf(format, args...)
}

func (l *Logger) Error(msg string, err error) {
if err == nil {
l.error.Println(msg)
} else {
l.error.Print(msg, err.Error())
}

func (l *Logger) FatalOnError(msg string, err error) {
if err != nil {
l.error.Fatalln(msg, err.Error())
}
}

0 comments on commit 4a7489c

Please sign in to comment.