From 52bf777e67f68497bd42a7cb62f811d5697d0b57 Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Wed, 14 Nov 2018 12:14:41 +0000 Subject: [PATCH] Print errors on stderr This makes a start on fixing https://github.com/Jguer/yay/issues/715 It is by no means a complete solution, but instead a start --- callbacks.go | 8 ++++---- clean.go | 6 +++--- cmd.go | 4 ++-- config.go | 18 +++++++++--------- depCheck.go | 29 +++++++++++++++-------------- download.go | 8 ++++---- exec.go | 2 +- install.go | 8 ++++---- main.go | 6 +++--- print.go | 14 +++++++------- query.go | 7 ++++--- 11 files changed, 56 insertions(+), 54 deletions(-) diff --git a/callbacks.go b/callbacks.go index b54aa4c3..6b18a665 100644 --- a/callbacks.go +++ b/callbacks.go @@ -62,12 +62,12 @@ func questionCallback(question alpm.QuestionAny) { numberBuf, overflow, err := reader.ReadLine() if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) break } if overflow { - fmt.Println("Input too long") + fmt.Fprintln(os.Stderr, "Input too long") continue } @@ -77,12 +77,12 @@ func questionCallback(question alpm.QuestionAny) { num, err := strconv.Atoi(string(numberBuf)) if err != nil { - fmt.Printf("%s invalid number: %s\n", red("error:"), string(numberBuf)) + fmt.Fprintf(os.Stderr, "%s invalid number: %s\n", red("error:"), string(numberBuf)) continue } if num < 1 || num > size { - fmt.Printf("%s invalid value: %d is not between %d and %d\n", red("error:"), num, 1, size) + fmt.Fprintf(os.Stderr, "%s invalid value: %d is not between %d and %d\n", red("error:"), num, 1, size) continue } diff --git a/clean.go b/clean.go index 8d1865a3..4fc3dfb6 100644 --- a/clean.go +++ b/clean.go @@ -207,14 +207,14 @@ func cleanAfter(bases []Base) { fmt.Printf(bold(cyan("::")+" Cleaning (%d/%d): %s\n"), i+1, len(bases), cyan(dir)) _, stderr, err := capture(passToGit(dir, "reset", "--hard", "HEAD")) if err != nil { - fmt.Printf("error resetting %s: %s", base.String(), stderr) + fmt.Fprintf(os.Stderr, "error resetting %s: %s", base.String(), stderr) } show(passToGit(dir, "clean", "-fx")) } else { fmt.Printf(bold(cyan("::")+" Deleting (%d/%d): %s\n"), i+1, len(bases), cyan(dir)) if err := os.RemoveAll(dir); err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } } } @@ -225,7 +225,7 @@ func cleanBuilds(bases []Base) { dir := filepath.Join(config.BuildDir, base.Pkgbase()) fmt.Printf(bold(cyan("::")+" Deleting (%d/%d): %s\n"), i+1, len(bases), cyan(dir)) if err := os.RemoveAll(dir); err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } } } diff --git a/cmd.go b/cmd.go index 992662ab..15aa38ca 100644 --- a/cmd.go +++ b/cmd.go @@ -328,8 +328,8 @@ func displayNumberMenu(pkgS []string) (err error) { } if aurErr != nil { - fmt.Printf("Error during AUR search: %s\n", aurErr) - fmt.Println("Showing repo packages only") + fmt.Fprintf(os.Stderr, "Error during AUR search: %s\n", aurErr) + fmt.Fprintln(os.Stderr, "Showing repo packages only") } fmt.Println(bold(green(arrow + " Packages to install (eg: 1 2 3, 1-3 or ^4)"))) diff --git a/config.go b/config.go index 27591810..9ce69767 100644 --- a/config.go +++ b/config.go @@ -214,7 +214,7 @@ func editor() (string, []string) { case config.Editor != "": editor, err := exec.LookPath(config.Editor) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } else { return editor, strings.Fields(config.EditorFlags) } @@ -223,7 +223,7 @@ func editor() (string, []string) { editorArgs := strings.Fields(os.Getenv("EDITOR")) editor, err := exec.LookPath(editorArgs[0]) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } else { return editor, editorArgs[1:] } @@ -232,21 +232,21 @@ func editor() (string, []string) { editorArgs := strings.Fields(os.Getenv("VISUAL")) editor, err := exec.LookPath(editorArgs[0]) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } else { return editor, editorArgs[1:] } fallthrough default: - fmt.Println() - fmt.Println(bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set")) - fmt.Println(bold(red(arrow)) + bold(" Please add ") + bold(cyan("$EDITOR")) + bold(" or ") + bold(cyan("$VISUAL")) + bold(" to your environment variables.")) + fmt.Fprintln(os.Stderr) + fmt.Fprintln(os.Stderr, bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set")) + fmt.Fprintln(os.Stderr, bold(red(arrow))+bold(" Please add ")+bold(cyan("$EDITOR"))+bold(" or ")+bold(cyan("$VISUAL"))+bold(" to your environment variables.")) for { fmt.Print(green(bold(arrow + " Edit PKGBUILD with: "))) editorInput, err := getInput("") if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) continue } @@ -254,7 +254,7 @@ func editor() (string, []string) { editor, err := exec.LookPath(editorArgs[0]) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) continue } return editor, editorArgs[1:] @@ -318,7 +318,7 @@ func (config Configuration) String() string { enc := json.NewEncoder(&buf) enc.SetIndent("", "\t") if err := enc.Encode(config); err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } return buf.String() } diff --git a/depCheck.go b/depCheck.go index 169e4446..08548a81 100644 --- a/depCheck.go +++ b/depCheck.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "strings" "sync" @@ -74,23 +75,23 @@ func (ds *depSolver) CheckMissing() error { return nil } - fmt.Println(bold(red(arrow+" Error: ")) + "Could not find all required packages:") + fmt.Fprintln(os.Stderr, bold(red(arrow+" Error: "))+"Could not find all required packages:") for dep, trees := range missing.Missing { for _, tree := range trees { - fmt.Print(" ", cyan(dep)) + fmt.Fprint(os.Stderr, " ", cyan(dep)) if len(tree) == 0 { - fmt.Print(" (Target") + fmt.Fprint(os.Stderr, " (Target") } else { - fmt.Print(" (Wanted by: ") + fmt.Fprint(os.Stderr, " (Wanted by: ") for n := 0; n < len(tree)-1; n++ { - fmt.Print(cyan(tree[n]), " -> ") + fmt.Fprint(os.Stderr, cyan(tree[n]), " -> ") } - fmt.Print(cyan(tree[len(tree)-1])) + fmt.Fprint(os.Stderr, cyan(tree[len(tree)-1])) } - fmt.Println(")") + fmt.Fprintln(os.Stderr, ")") } } @@ -263,11 +264,11 @@ func (ds *depSolver) CheckConflicts() (mapStringSet, error) { formatConflicts := func(conflicts mapStringSet, inner bool, s string) { if len(conflicts) != 0 { - fmt.Println() + fmt.Fprintln(os.Stderr) if inner { - fmt.Println(bold(red(arrow)), bold("Inner conflicts found:")) + fmt.Fprintln(os.Stderr, bold(red(arrow)), bold("Inner conflicts found:")) } else { - fmt.Println(bold(red(arrow)), bold("Package conflicts found:")) + fmt.Fprintln(os.Stderr, bold(red(arrow)), bold("Package conflicts found:")) } printConflict := func(name string, pkgs stringSet) { @@ -277,7 +278,7 @@ func (ds *depSolver) CheckConflicts() (mapStringSet, error) { } str = strings.TrimSuffix(str, ",") - fmt.Println(str) + fmt.Fprintln(os.Stderr, str) } for _, pkg := range ds.Repo { @@ -318,9 +319,9 @@ func (ds *depSolver) CheckConflicts() (mapStringSet, error) { return nil, fmt.Errorf("Package conflicts can not be resolved with noconfirm, aborting") } - fmt.Println() - fmt.Println(bold(red(arrow)), bold("Conflicting packages will have to be confirmed manually")) - fmt.Println() + fmt.Fprintln(os.Stderr) + fmt.Fprintln(os.Stderr, bold(red(arrow)), bold("Conflicting packages will have to be confirmed manually")) + fmt.Fprintln(os.Stderr) } } diff --git a/download.go b/download.go index 32e6516f..6ae1b914 100644 --- a/download.go +++ b/download.go @@ -165,11 +165,11 @@ func getPkgbuilds(pkgs []string) error { name := base.Pkgbase() _, err = os.Stat(filepath.Join(wd, name)) if err != nil && !os.IsNotExist(err) { - fmt.Println(bold(red(smallArrow)), err) + fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err) continue } else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") || shouldUseGit(filepath.Join(wd, name)) { if err = os.RemoveAll(filepath.Join(wd, name)); err != nil { - fmt.Println(bold(red(smallArrow)), err) + fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err) continue } } else { @@ -249,11 +249,11 @@ func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) { _, err = os.Stat(filepath.Join(path, name)) if err != nil && !os.IsNotExist(err) { - fmt.Println(bold(red(smallArrow)), err) + fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err) continue } else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") { if err = os.RemoveAll(filepath.Join(path, name)); err != nil { - fmt.Println(bold(red(smallArrow)), err) + fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err) continue } } else { diff --git a/exec.go b/exec.go index 52b6cfe1..3b643aac 100644 --- a/exec.go +++ b/exec.go @@ -47,7 +47,7 @@ func updateSudo() { for { err := show(exec.Command("sudo", "-v")) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } else { break } diff --git a/install.go b/install.go index 67a46287..f062115d 100644 --- a/install.go +++ b/install.go @@ -434,9 +434,9 @@ nextpkg: if len(incompatible) > 0 { fmt.Println() - fmt.Print(bold(yellow(arrow)) + " The following packages are not compatible with your architecture:") + fmt.Print(bold(yellow(arrow))+" The following packages are not compatible with your architecture:") for pkg := range incompatible { - fmt.Print(" " + cyan((basesMap[pkg].String()))) + fmt.Print(" "+cyan(basesMap[pkg].String())) } fmt.Println() @@ -763,7 +763,7 @@ func parseSrcinfoFiles(bases []Base, errIsFatal bool) (map[string]*gosrc.Srcinfo pkgbuild, err := gosrc.ParseFile(filepath.Join(dir, ".SRCINFO")) if err != nil { if !errIsFatal { - fmt.Printf("failed to parse %s -- skipping: %s\n", base.String(), err) + fmt.Fprintf(os.Stderr, "failed to parse %s -- skipping: %s\n", base.String(), err) continue } return nil, fmt.Errorf("failed to parse %s: %s", base.String(), err) @@ -1057,7 +1057,7 @@ func buildInstallPkgbuilds(ds *depSolver, srcinfos map[string]*gosrc.Srcinfo, pa err = saveVCSInfo() if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } if len(depArguments.targets) > 0 { diff --git a/main.go b/main.go index 90ad17c2..fd42d825 100644 --- a/main.go +++ b/main.go @@ -184,7 +184,7 @@ func initAlpmHandle() error { func exitOnError(err error) { if err != nil { if str := err.Error(); str != "" { - fmt.Println(str) + fmt.Fprintln(os.Stderr, str) } cleanup() os.Exit(1) @@ -194,7 +194,7 @@ func exitOnError(err error) { func cleanup() int { if alpmHandle != nil { if err := alpmHandle.Release(); err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) return 1 } } @@ -204,7 +204,7 @@ func cleanup() int { func main() { if 0 == os.Geteuid() { - fmt.Println("Please avoid running yay as root/sudo.") + fmt.Fprintln(os.Stderr, "Please avoid running yay as root/sudo.") } exitOnError(setPaths()) diff --git a/print.go b/print.go index 76a5dd8f..650dc5d6 100644 --- a/print.go +++ b/print.go @@ -371,7 +371,7 @@ outer: } } - fmt.Println(red(bold("error:")), "package '"+pkg+"' was not found") + fmt.Fprintln(os.Stderr, red(bold("error:")), "package '"+pkg+"' was not found") missing = true } @@ -395,7 +395,7 @@ func (item item) print(buildTime time.Time) { date, err := time.Parse(time.RFC1123Z, item.PubDate) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } else { fd = formatTime(int(date.Unix())) if _, double, _ := cmdArgs.getArg("news", "w"); !double && !buildTime.IsZero() { @@ -546,7 +546,7 @@ func providerMenu(dep string, providers providers) *rpc.Pkg { size++ } - fmt.Println(str) + fmt.Fprintln(os.Stderr, str) for { fmt.Print("\nEnter a number (default=1): ") @@ -560,12 +560,12 @@ func providerMenu(dep string, providers providers) *rpc.Pkg { numberBuf, overflow, err := reader.ReadLine() if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) break } if overflow { - fmt.Println("Input too long") + fmt.Fprintln(os.Stderr, "Input too long") continue } @@ -575,12 +575,12 @@ func providerMenu(dep string, providers providers) *rpc.Pkg { num, err := strconv.Atoi(string(numberBuf)) if err != nil { - fmt.Printf("%s invalid number: %s\n", red("error:"), string(numberBuf)) + fmt.Fprintf(os.Stderr, "%s invalid number: %s\n", red("error:"), string(numberBuf)) continue } if num < 1 || num > size { - fmt.Printf("%s invalid value: %d is not between %d and %d\n", red("error:"), num, 1, size) + fmt.Fprintf(os.Stderr, "%s invalid value: %d is not between %d and %d\n", red("error:"), num, 1, size) continue } diff --git a/query.go b/query.go index ad58c0a5..a6abd1ce 100644 --- a/query.go +++ b/query.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "sort" "strings" "sync" @@ -192,8 +193,8 @@ func syncSearch(pkgS []string) (err error) { } if aurErr != nil { - fmt.Printf("Error during AUR search: %s\n", aurErr) - fmt.Println("Showing Repo packages only") + fmt.Fprintf(os.Stderr, "Error during AUR search: %s\n", aurErr) + fmt.Fprintln(os.Stderr, "Showing Repo packages only") } return nil @@ -220,7 +221,7 @@ func syncInfo(pkgS []string) (err error) { info, err = aurInfoPrint(noDb) if err != nil { missing = true - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) } }