From 820116d1665383176a870f05810ce60d77486606 Mon Sep 17 00:00:00 2001 From: Theotime Leveque Date: Mon, 20 Mar 2017 12:26:32 -0400 Subject: [PATCH] Formatting: Colorize stderr output. --- backend/command/command.go | 18 +++++++++--------- backend/config/config.go | 23 ++++++++++++----------- backend/env/env.go | 21 ++++++++++++--------- backend/env/setup.go | 5 +++-- backend/projects/projects.go | 12 ++++++++---- backend/repo/repo.go | 7 ++++--- backend/share/share.go | 12 ++++++++---- cmd/projects.go | 17 ++++++----------- cmd/root.go | 3 ++- cmd/setup.go | 3 ++- cmd/share.go | 17 +++++++++-------- main.go | 3 ++- 12 files changed, 77 insertions(+), 64 deletions(-) diff --git a/backend/command/command.go b/backend/command/command.go index d5ce485..7983bb2 100644 --- a/backend/command/command.go +++ b/backend/command/command.go @@ -27,11 +27,11 @@ import ( func ExecuteCommand(subCmd *exec.Cmd) (err error) { cmdOutReader, err := subCmd.StdoutPipe() if err != nil { - return fmt.Errorf("Error creating StdoutPipe for Cmd: %v", err) + return fmt.Errorf("Impossible to create StdoutPipe for Cmd: %v", err) } cmdErrReader, err := subCmd.StderrPipe() if err != nil { - return fmt.Errorf("Error creating StderrPipe for Cmd: %v", err) + return fmt.Errorf("Impossible to create StderrPipe for Cmd: %v", err) } for _, cmdReader := range []io.ReadCloser{cmdOutReader, cmdErrReader} { @@ -45,12 +45,12 @@ func ExecuteCommand(subCmd *exec.Cmd) (err error) { err = subCmd.Start() if err != nil { - return fmt.Errorf("Error starting Cmd: %v", err) + return fmt.Errorf("Impossible to start Cmd: %v", err) } err = subCmd.Wait() if err != nil { - return fmt.Errorf("Error waiting for Cmd: %v", err) + return fmt.Errorf("Impossible to wait for Cmd: %v", err) } return nil } @@ -60,11 +60,11 @@ func ExecuteCommand(subCmd *exec.Cmd) (err error) { func MustExecuteCommand(subCmd *exec.Cmd) (err error) { cmdOutReader, err := subCmd.StdoutPipe() if err != nil { - return fmt.Errorf("Error creating StdoutPipe for Cmd: %v", err) + return fmt.Errorf("Impossible to create StdoutPipe for Cmd: %v", err) } cmdErrReader, err := subCmd.StderrPipe() if err != nil { - return fmt.Errorf("Error creating StderrPipe for Cmd: %v", err) + return fmt.Errorf("Impossible to create StderrPipe for Cmd: %v", err) } scannerOut := bufio.NewScanner(cmdOutReader) @@ -87,16 +87,16 @@ func MustExecuteCommand(subCmd *exec.Cmd) (err error) { err = subCmd.Start() if err != nil { - return fmt.Errorf("Error starting Cmd: %v StdErr: %v", err, os.Stderr) + return fmt.Errorf("Impossible to start Cmd: %v StdErr: %v", err, os.Stderr) } err = subCmd.Wait() if err != nil { - return fmt.Errorf("Error waiting for Cmd: %v StdErr: %v", err, os.Stderr) + return fmt.Errorf("Impossible to wait for Cmd: %v StdErr: %v", err, os.Stderr) } if <-failure == true { - return errors.New("Command failed to complete without any error") + return errors.New("Failed to complete without error") } return nil } diff --git a/backend/config/config.go b/backend/config/config.go index 41d5daf..1c643e1 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -26,6 +26,7 @@ import ( yaml "gopkg.in/yaml.v2" + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/thylong/ian/backend/command" @@ -62,7 +63,7 @@ var ConfigMap YamlConfigMap func init() { usr, err := user.Current() if err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s", color.RedString("Error:"), err) os.Exit(1) } @@ -124,13 +125,13 @@ func initViper(viperName string, soft bool) (viperInstance *viper.Viper) { err := viperInstance.ReadInConfig() if err != nil { - fmt.Fprintf(os.Stderr, "Problem with config file: %s Error: %s", viperName, err.Error()) + fmt.Fprintf(os.Stderr, "%v Problem with config file: %s: %s", color.RedString("Error:"), viperName, err.Error()) os.Exit(1) } else { configContent, _ := ioutil.ReadFile(ConfigFilesPathes[viperName]) err = yaml.Unmarshal(configContent, &ConfigMap) if err != nil { - fmt.Println("Unable to parse config file.") + fmt.Fprintf(os.Stderr, "%v Unable to parse config file.", color.RedString("Error:")) os.Exit(1) } } @@ -155,7 +156,7 @@ func SetupConfigFile(ConfigFileName string) { fmt.Printf("Creating %s\n", ConfigFileName) if err := ioutil.WriteFile(ConfigFilePath, configContent, 0766); err != nil { - fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } return @@ -176,13 +177,13 @@ func AppendToConfig(lines string, confFilename string) { confPath := ConfigFilesPathes[confFilename] f, err := os.OpenFile(confPath, os.O_APPEND|os.O_WRONLY, 0600) if err != nil { - fmt.Println(err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } defer f.Close() if _, err = f.WriteString(lines); err != nil { - fmt.Println(err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } } @@ -202,11 +203,11 @@ func GetPreset(presetName string) []byte { func UpdateYamlFile(fileFullPath string, fileContent map[string]interface{}) { out, err := yaml.Marshal(&fileContent) if err != nil { - fmt.Fprintf(os.Stderr, "Failed to update %s.\n", fileFullPath) + fmt.Fprintf(os.Stderr, "%v Failed to update %s.\n", color.RedString("Error:"), fileFullPath) os.Exit(1) } if err := ioutil.WriteFile(fileFullPath, out, 0766); err != nil { - fmt.Fprintf(os.Stderr, "Failed to update %s.\n", fileFullPath) + fmt.Fprintf(os.Stderr, "%v Failed to update %s.\n", color.RedString("Error:"), fileFullPath) os.Exit(1) } } @@ -235,7 +236,7 @@ func SetupEnvFileWithPreset(preset string) { var Envcontent string switch preset { default: - fmt.Fprint(os.Stderr, "Cannot find preset.") + fmt.Fprintf(os.Stderr, "%v Cannot find preset.", color.RedString("Error:")) return case "1": Envcontent = GetSoftwareEngineerPreset() @@ -250,13 +251,13 @@ func SetupEnvFileWithPreset(preset string) { confPath := ConfigFilesPathes["env"] f, err := os.OpenFile(confPath, os.O_CREATE|os.O_WRONLY, 0655) if err != nil { - fmt.Println(err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } defer f.Close() if _, err = f.WriteString(Envcontent); err != nil { - fmt.Println(err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } } diff --git a/backend/env/env.go b/backend/env/env.go index 0ae7fe8..b5aa763 100644 --- a/backend/env/env.go +++ b/backend/env/env.go @@ -25,6 +25,7 @@ import ( "os/exec" "os/user" + "github.com/fatih/color" "github.com/thylong/ian/backend/command" ) @@ -34,7 +35,7 @@ func Describe() { resp, err := http.Get(IPCheckerURL) if err != nil { - fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) } content, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() @@ -42,7 +43,7 @@ func Describe() { var jsonContent map[string]string err = json.Unmarshal(content, &jsonContent) if err != nil { - fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) return } @@ -65,7 +66,7 @@ func EnsureDotfilesDir(dotfilesDirPath string) { if _, err := os.Stat(dotfilesDirPath); err != nil { err = os.Mkdir(dotfilesDirPath, 0766) if err != nil { - fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) } command.ExecuteCommand(exec.Command("git", "init")) @@ -83,11 +84,11 @@ func ImportIntoDotfilesDir(dotfilesToSave []string, dotfilesDirPath string) { dst := fmt.Sprintf("%s/%s", dotfilesDirPath, dotfileToSave) if err := MoveFile(src, dst); err != nil { - fmt.Fprintf(os.Stderr, "couldn't move %s !", src) + fmt.Fprintf(os.Stderr, "%v couldn't move %s !", color.RedString("Error:"), src) os.Exit(1) } if err := os.Symlink(dst, src); err != nil { - fmt.Fprintf(os.Stderr, "couldn't symlink %s !", src) + fmt.Fprintf(os.Stderr, "%v couldn't symlink %s !", color.RedString("Error:"), src) os.Exit(1) } } @@ -104,7 +105,7 @@ func EnsureDotfilesRepository(dotfilesRepository string, dotfilesDirPath string) termCmd.Dir = dotfilesDirPath if err := command.MustExecuteCommand(termCmd); err != nil { - fmt.Fprintf(os.Stderr, "%s repository doesn't exists or is not reachable.", repositoryURL) + fmt.Fprintf(os.Stderr, "%v %s repository doesn't exists or is not reachable.", color.RedString("Error:"), repositoryURL) os.Exit(1) } } @@ -119,19 +120,21 @@ func PushDotfiles(message string, dotfilesDirPath string) { addCmd := exec.Command("git", "add", "-A") addCmd.Dir = dotfilesDirPath if err = command.MustExecuteCommand(addCmd); err != nil { - fmt.Fprint(os.Stderr, "Cannot interact with Git") + fmt.Fprintf(os.Stderr, "%v Cannot interact with Git.\n", color.RedString("Error:")) + return } commitCmd := exec.Command("git", "commit", "-m", message) commitCmd.Dir = dotfilesDirPath if err = command.MustExecuteCommand(commitCmd); err != nil { - fmt.Fprint(os.Stderr, "Cannot create a commit") + fmt.Fprintf(os.Stderr, "%v Cannot create a commit.\n", color.RedString("Error:")) + return } termCmd := exec.Command("git", "push", "--force", "origin", "master") termCmd.Dir = dotfilesDirPath if err = command.MustExecuteCommand(termCmd); err != nil { - fmt.Fprint(os.Stderr, "Cannot push to repository") + fmt.Fprintf(os.Stderr, "%v Cannot push to repository.\n", color.RedString("Error:")) } } diff --git a/backend/env/setup.go b/backend/env/setup.go index 0875844..27eedae 100644 --- a/backend/env/setup.go +++ b/backend/env/setup.go @@ -22,6 +22,7 @@ import ( "os/user" "regexp" + "github.com/fatih/color" "github.com/thylong/ian/backend/command" pm "github.com/thylong/ian/backend/package-managers" ) @@ -44,7 +45,7 @@ func SetupDotFiles(dotfilesRepository string, dotfilesDirPath string) { if _, err := os.Stat(usr.HomeDir + "/" + f.Name()); err != nil { err := os.Symlink(usr.HomeDir+"/.dotfiles/"+f.Name(), usr.HomeDir+"/"+f.Name()) if err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) } } } @@ -64,7 +65,7 @@ func SetupPackages(PackageManager pm.PackageManager, packages []string) { for _, packageToInstall := range packages { if err := PackageManager.Install(packageToInstall); err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) } } } diff --git a/backend/projects/projects.go b/backend/projects/projects.go index 1317d8c..d927f81 100644 --- a/backend/projects/projects.go +++ b/backend/projects/projects.go @@ -20,6 +20,8 @@ import ( "io/ioutil" "net/http" "os" + + "github.com/fatih/color" ) // Status makes a GET HTTP query and returns OK if response status is 200 @@ -28,14 +30,16 @@ func Status(project string, baseURL string, healthEndpoint string) { url := baseURL + healthEndpoint resp, err := http.Get(url) if err != nil { - fmt.Fprint(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) } defer resp.Body.Close() if statusCode := resp.StatusCode; statusCode == 200 { - fmt.Printf("%s : OK", project) + fmt.Printf("%s : ", project) + color.Green("OK") } else { - fmt.Printf("%s : ERROR", project) + fmt.Printf("%s : ", project) + color.Red("ERROR") } } @@ -50,7 +54,7 @@ func Stats(project string, repositoryURL string) { var jsonContent map[string]interface{} if err = json.Unmarshal(content, &jsonContent); err != nil { - fmt.Printf("Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) return } diff --git a/backend/repo/repo.go b/backend/repo/repo.go index 085e5df..0abb9c8 100644 --- a/backend/repo/repo.go +++ b/backend/repo/repo.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" + "github.com/fatih/color" "github.com/thylong/ian/backend/command" "github.com/thylong/ian/backend/config" ) @@ -55,7 +56,7 @@ func Clean(repository string) error { func UpdateAll() { files, err := ioutil.ReadDir(config.Vipers["config"].GetString("repositories_path")) if err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } for _, file := range files { @@ -77,7 +78,7 @@ func UpdateOne(repository string) error { func UpgradeAll() { files, err := ioutil.ReadDir(config.Vipers["config"].GetString("repositories_path")) if err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } for _, file := range files { @@ -98,7 +99,7 @@ func UpgradeOne(repository string) error { // Remove local repository func Remove(repository string) error { if repository == "/*" || repository == "/" { - fmt.Fprint(os.Stderr, "Cmon man, don't do that...") + fmt.Fprintf(os.Stderr, "%v", color.RedString("Error: Cmon man, don't do that...")) } termCmd := execCommand("rm", "-rf", repository) termCmd.Dir = config.Vipers["config"].GetString("repositories_path") diff --git a/backend/share/share.go b/backend/share/share.go index 404c391..2a5dbea 100644 --- a/backend/share/share.go +++ b/backend/share/share.go @@ -11,6 +11,8 @@ import ( "mime/multipart" "net/http" "os" + + "github.com/fatih/color" ) // Upload to transfer.sh @@ -20,7 +22,7 @@ func Upload(filename string, targetURL string, key string) (string, error) { fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename) if err != nil { - fmt.Println("error writing to buffer") + fmt.Fprintf(os.Stderr, "%v Failed to write to buffer.", color.RedString("Error:")) return "", err } @@ -28,7 +30,7 @@ func Upload(filename string, targetURL string, key string) (string, error) { if key == "" { fh, err = os.Open(filename) if err != nil { - fmt.Println("error opening file") + fmt.Fprintf(os.Stderr, "%v Failed to open file.", color.RedString("Error:")) return "", err } } else { @@ -68,7 +70,8 @@ func EncryptFile(filename string, key []byte) io.Reader { block, err := aes.NewCipher(key) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "%v %s", color.RedString("Error:"), err) + os.Exit(1) } // The IV needs to be unique, but not secure. Therefore it's common to @@ -76,7 +79,8 @@ func EncryptFile(filename string, key []byte) io.Reader { ciphertext := make([]byte, aes.BlockSize+len(fileContent)) iv := ciphertext[:aes.BlockSize] if _, err := io.ReadFull(rand.Reader, iv); err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "%v %s", color.RedString("Error:"), err) + os.Exit(1) } stream := cipher.NewCFBEncrypter(block, iv) diff --git a/cmd/projects.go b/cmd/projects.go index 1e823b8..d6b4083 100644 --- a/cmd/projects.go +++ b/cmd/projects.go @@ -21,6 +21,7 @@ import ( "os/exec" "strings" + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/thylong/ian/backend/command" "github.com/thylong/ian/backend/config" @@ -216,18 +217,15 @@ func setProjectCmd() *cobra.Command { return &cobra.Command{ Use: "set", Short: "Define a subcommand", - Long: `Define a subcommand. - - Usage: - ian project set `, + Long: `Define a subcommand.`, Run: func(cmd *cobra.Command, args []string) { if len(args) < 2 { - fmt.Fprint(os.Stderr, "Not enough argument.\n\n") + fmt.Fprintf(os.Stderr, "%v Not enough argument.\n\n", color.RedString("Error:")) cmd.Usage() os.Exit(1) } if 5 < len(customCmdDescription) && len(customCmdDescription) < 40 { - fmt.Fprint(os.Stderr, "Description must be between 5 and 40 alphanumeric characters.\n\n") + fmt.Fprintf(os.Stderr, "%v Description must be between 5 and 40 alphanumeric characters.\n\n", color.RedString("Error:")) cmd.Usage() os.Exit(1) } @@ -265,13 +263,10 @@ func unsetProjectCmd() *cobra.Command { return &cobra.Command{ Use: "unset", Short: "Remove a subcommand", - Long: `Remove a subcommand. - - Usage: - ian project remove `, + Long: `Remove a subcommand.`, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { - fmt.Fprint(os.Stderr, "Not enough argument.\n\n") + fmt.Fprintf(os.Stderr, "%v Not enough argument.\n\n", color.RedString("Error:")) cmd.Usage() os.Exit(1) } diff --git a/cmd/root.go b/cmd/root.go index 6a64803..7d4a4c2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ import ( "fmt" "os" + "github.com/fatih/color" "github.com/spf13/cobra" pm "github.com/thylong/ian/backend/package-managers" ) @@ -28,7 +29,7 @@ var OSPackageManager pm.PackageManager func init() { var err error if OSPackageManager, err = pm.GetOSPackageManager(); err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(1) } RootCmd.AddCommand(versionCmd) diff --git a/cmd/setup.go b/cmd/setup.go index a989613..23cdf71 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -18,6 +18,7 @@ import ( "fmt" "os" + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/thylong/ian/backend/config" "github.com/thylong/ian/backend/env" @@ -41,7 +42,7 @@ var setupCmd = &cobra.Command{ fmt.Println("Installing OS package manager...") err = OSPackageManager.Setup() if err != nil { - fmt.Fprintln(os.Stderr, "Missing OS package manager !") + fmt.Fprintf(os.Stderr, "%v Missing OS package manager !.", color.RedString("Error:")) os.Exit(1) } } diff --git a/cmd/share.go b/cmd/share.go index 0647109..85377a8 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -22,6 +22,7 @@ import ( "os" "strings" + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/thylong/ian/backend/config" "github.com/thylong/ian/backend/share" @@ -67,7 +68,7 @@ var shareConfigCmd = &cobra.Command{ } link, err := share.Upload(config.ConfigFilesPathes[cmd.Use], "https://transfer.sh/", key) if err != nil { - fmt.Fprint(os.Stderr, "Sorry, it looks like I cannot upload configuration file... :(") + fmt.Fprintf(os.Stderr, "%v It looks like I cannot upload configuration file... :(.", color.RedString("Error:")) return } fmt.Println(link) @@ -84,7 +85,7 @@ var shareProjectsCmd = &cobra.Command{ } link, err := share.Upload(config.ConfigFilesPathes[cmd.Use], "https://transfer.sh/", key) if err != nil { - fmt.Fprint(os.Stderr, "Sorry, it looks like I cannot upload configuration file... :(") + fmt.Fprintf(os.Stderr, "%v It looks like I cannot upload configuration file... :(.", color.RedString("Error:")) return } fmt.Println(link) @@ -101,7 +102,7 @@ var shareEnvCmd = &cobra.Command{ } link, err := share.Upload(config.ConfigFilesPathes[cmd.Use], "https://transfer.sh/", key) if err != nil { - fmt.Fprint(os.Stderr, "Sorry, it looks like I cannot upload configuration file... :(") + fmt.Fprintf(os.Stderr, "%v It looks like I cannot upload configuration file... :(.", color.RedString("Error:")) return } fmt.Println(link) @@ -114,19 +115,19 @@ var shareSetFromLinkCmd = &cobra.Command{ Long: `Set config from config file link.`, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { - fmt.Fprint(os.Stderr, "Not enough argument.\n\n") + fmt.Fprintf(os.Stderr, "%v Not enough argument.\n\n", color.RedString("Error:")) cmd.Usage() os.Exit(1) } _, err := url.ParseRequestURI(args[0]) if err != nil { - fmt.Fprint(os.Stderr, "Sorry, it looks like the link you provided is invalid.") + fmt.Fprintf(os.Stderr, "%v Sorry, The link you provided is invalid.", color.RedString("Error:")) } resp, err := http.Get(args[0]) if err != nil { - fmt.Fprint(os.Stderr, "Sorry, the link is unreachable.") + fmt.Fprintf(os.Stderr, "%v Sorry, The link you provided is unreachable.", color.RedString("Error:")) } defer resp.Body.Close() @@ -145,12 +146,12 @@ var shareSetFromLinkCmd = &cobra.Command{ defer f.Close() if _, err := io.Copy(f, resp.Body); err != nil { - fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) + fmt.Fprintf(os.Stderr, "%v %s", color.RedString("Error:"), err) os.Exit(1) } return } - fmt.Fprint(os.Stderr, "Sorry, something went wrong.") + fmt.Fprintf(os.Stderr, "%v Sorry, something went wrong.", color.RedString("Error:")) }, } diff --git a/main.go b/main.go index c9a8f80..6e5630b 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,13 @@ import ( "fmt" "os" + "github.com/fatih/color" "github.com/thylong/ian/cmd" ) func main() { if err := cmd.RootCmd.Execute(); err != nil { - fmt.Fprint(os.Stderr, err) + fmt.Fprintf(os.Stderr, "%v %s.", color.RedString("Error:"), err) os.Exit(-1) } }