Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
unify style of error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 13, 2020
1 parent bb69517 commit d490f7e
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 22 deletions.
4 changes: 3 additions & 1 deletion internal/commands/configcmd/cat.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package configcmd

import (
"errors"
"fmt"
"io/ioutil"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)
Expand All @@ -21,7 +23,7 @@ var catCmd = &cobra.Command{
content, err := ioutil.ReadFile(filepath)

if err != nil {
fmt.Printf("%sError: Could not read config file!%s\n", chalk.Red, chalk.ResetColor)
utils.ErrorAndExit(errors.New("Could not read config file"))
} else {
fmt.Printf("%sHere comes the dockma configuration file:%s\n", chalk.Cyan, chalk.ResetColor)

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/downcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var DownCommand = &cobra.Command{

utils.Error(err)
if err != nil {
fmt.Println(output)
fmt.Print(string(output))

os.Exit(0)
}
Expand Down
14 changes: 5 additions & 9 deletions internal/commands/envcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var initCmd = &cobra.Command{
if len(args) == 1 {
path = args[0]

if err := os.Chdir(path); err != nil {
fmt.Printf("%sError: Could not find directory: %s%s\n", chalk.Red, path, chalk.ResetColor)
err := os.Chdir(path)

os.Exit(0)
if err != nil {
utils.ErrorAndExit(fmt.Errorf("Could not find directory: %s", path))
}
}

Expand All @@ -43,9 +43,7 @@ var initCmd = &cobra.Command{
workingDir, err := os.Getwd()

if err != nil {
fmt.Printf("%sError. Could not read current working directory%s\n", chalk.Red, chalk.ResetColor)

os.Exit(0)
utils.ErrorAndExit(errors.New("Could not read current working directory"))
}

pull := "off"
Expand All @@ -69,9 +67,7 @@ var initCmd = &cobra.Command{
viper.Set("active", env)

if err := viper.WriteConfig(); err != nil {
fmt.Printf("%sError on initializing environment: %s (old: %s)%s\n", chalk.Red, env, oldEnv, chalk.ResetColor)

os.Exit(1)
utils.ErrorAndExit(fmt.Errorf("Initializing environment failed: %s", env))
}

fmt.Printf("%sSet active environment: %s%s\n", chalk.Cyan, env, chalk.ResetColor)
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/envcmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var removeCmd = &cobra.Command{
viper.Set("envs", envs)

if err := viper.WriteConfig(); err != nil {
fmt.Printf("%sError removing environment: %s%s\n", chalk.Red, env, chalk.ResetColor)
utils.ErrorAndExit(fmt.Errorf("Removing environment failed: %s", env))
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/envcmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var setCmd = &cobra.Command{
viper.Set("active", env)

if err := viper.WriteConfig(); err != nil {
fmt.Printf("%sError setting active environment: %s%s\n", chalk.Red, env, chalk.ResetColor)
utils.ErrorAndExit(fmt.Errorf("Setting active environment failed: %s", env))
}
},
}
Expand Down
8 changes: 2 additions & 6 deletions internal/commands/initcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ func initCommandHandler(cmd *cobra.Command, args []string) {
home := viper.GetString("home")

if err := os.MkdirAll(home, os.FileMode(0755)); err != nil {
fmt.Printf("%sCould not create config dir: %s%s\n", chalk.Red, err, chalk.ResetColor)

os.Exit(1)
utils.ErrorAndExit(fmt.Errorf("Could not create config dir: %s", err))
}

filepath := path.Join(home, "config.json")

if err := viper.WriteConfigAs(filepath); err != nil {
fmt.Printf("%sCould not save config.json at: %s%s\n", chalk.Red, home, chalk.ResetColor)

os.Exit(1)
utils.ErrorAndExit(fmt.Errorf("Could not save config.json at: %s", home))
}
}
3 changes: 2 additions & 1 deletion internal/commands/inspectcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
Expand All @@ -27,7 +28,7 @@ var InspectCommand = &cobra.Command{
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("%sNothing to output yet. Sry, %s!%s\n", chalk.Cyan, viper.GetString("username"), chalk.ResetColor)
} else {
fmt.Printf("%sError: %s%s\n", chalk.Red, viper.GetString("username"), chalk.ResetColor)
utils.ErrorAndExit(fmt.Errorf("Could not read logfile: %s", viper.GetString("logfile")))
}
} else {
fmt.Printf("%sHere come the logs:%s\n", chalk.Cyan, chalk.ResetColor)
Expand Down
3 changes: 2 additions & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/martinnirtl/dockma/internal/commands/pscmd"
"github.com/martinnirtl/dockma/internal/commands/upcmd"
"github.com/martinnirtl/dockma/internal/commands/versioncmd"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -56,7 +57,7 @@ func init() {
if homeDir := viper.GetString("home"); homeDir == "" {
userHome, err := homedir.Dir()
if err != nil {
fmt.Printf("%sCould not detect home dir: %s%s\n", chalk.Red, err, chalk.ResetColor)
utils.Error(fmt.Errorf("Could not detect home dir: %s", err))
}

viper.SetDefault("home", path.Join(userHome, ".dockma"))
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/upcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var UpCommand = &cobra.Command{

utils.Error(err)
if err != nil {
fmt.Println(string(output))
fmt.Print(string(output))

os.Exit(0)
}
Expand Down

0 comments on commit d490f7e

Please sign in to comment.