Skip to content

Commit

Permalink
Formatting: Colorize stderr output.
Browse files Browse the repository at this point in the history
  • Loading branch information
Theotime Leveque committed Mar 20, 2017
1 parent b99c13a commit 820116d
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 64 deletions.
18 changes: 9 additions & 9 deletions backend/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}
Expand Down
21 changes: 12 additions & 9 deletions backend/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os/exec"
"os/user"

"github.com/fatih/color"
"github.com/thylong/ian/backend/command"
)

Expand All @@ -34,15 +35,15 @@ 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()

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
}

Expand All @@ -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"))
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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:"))
}
}

Expand Down
5 changes: 3 additions & 2 deletions backend/env/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
12 changes: 8 additions & 4 deletions backend/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}

Expand All @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions backend/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/exec"

"github.com/fatih/color"
"github.com/thylong/ian/backend/command"
"github.com/thylong/ian/backend/config"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
12 changes: 8 additions & 4 deletions backend/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"mime/multipart"
"net/http"
"os"

"github.com/fatih/color"
)

// Upload to transfer.sh
Expand All @@ -20,15 +22,15 @@ 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
}

var fh io.Reader
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 {
Expand Down Expand Up @@ -68,15 +70,17 @@ 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
// include it at the beginning of the ciphertext.
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)
Expand Down
Loading

0 comments on commit 820116d

Please sign in to comment.