| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -214,11 +214,16 @@ func downloadcmd(opt Options) error { | |||
| 214 | 214 | return fmt.Errorf("github did not respond with 200 OK but with %v", resp.Status) | |
| 215 | 215 | } | |
| 216 | 216 | ||
| 217 | - out, err := os.Create(name) | ||
| 218 | - if err != nil { | ||
| 219 | - return fmt.Errorf("could not create file %s", name) | ||
| 217 | + out := os.Stdout // Pipe the asset to stdout by default. | ||
| 218 | + if isCharDevice(out) { | ||
| 219 | + // If stdout is a char device, assume it's a TTY (terminal). In this | ||
| 220 | + // case, don't pipe th easset to stdout, but create it as a file in | ||
| 221 | + // the current working folder. | ||
| 222 | + if out, err = os.Create(name); err != nil { | ||
| 223 | + return fmt.Errorf("could not create file %s", name) | ||
| 224 | + } | ||
| 225 | + defer out.Close() | ||
| 220 | 226 | } | |
| 221 | - defer out.Close() | ||
| 222 | 227 | ||
| 223 | 228 | n, err := io.Copy(out, resp.Body) | |
| 224 | 229 | if n != contentLength { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ package main | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "fmt" | |
| 5 | + "os" | ||
| 5 | 6 | "time" | |
| 6 | 7 | ) | |
| 7 | 8 | ||
@@ -18,15 +19,15 @@ func nvls(xs ...string) string { | |||
| 18 | 19 | ||
| 19 | 20 | func vprintln(a ...interface{}) (int, error) { | |
| 20 | 21 | if VERBOSITY > 0 { | |
| 21 | - return fmt.Println(a...) | ||
| 22 | + return fmt.Fprintln(os.Stderr, a...) | ||
| 22 | 23 | } | |
| 23 | 24 | ||
| 24 | 25 | return 0, nil | |
| 25 | 26 | } | |
| 26 | 27 | ||
| 27 | 28 | func vprintf(format string, a ...interface{}) (int, error) { | |
| 28 | 29 | if VERBOSITY > 0 { | |
| 29 | - return fmt.Printf(format, a...) | ||
| 30 | + return fmt.Fprintf(os.Stderr, format, a...) | ||
| 30 | 31 | } | |
| 31 | 32 | ||
| 32 | 33 | return 0, nil | |
@@ -39,3 +40,13 @@ func timeFmtOr(t *time.Time, fmt, def string) string { | |||
| 39 | 40 | } | |
| 40 | 41 | return t.Format(fmt) | |
| 41 | 42 | } | |
| 43 | + | ||
| 44 | + // isCharDevice returns true if f is a character device (panics if f can't | ||
| 45 | + // be stat'ed). | ||
| 46 | + func isCharDevice(f *os.File) bool { | ||
| 47 | + stat, err := f.Stat() | ||
| 48 | + if err != nil { | ||
| 49 | + panic(err) | ||
| 50 | + } | ||
| 51 | + return (stat.Mode() & os.ModeCharDevice) != 0 | ||
| 52 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments