-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathutil.go
58 lines (50 loc) · 1.41 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
func pageln(i ...interface{}) {
fPageln(strings.NewReader(fmt.Sprint(i...)))
}
// fPageln invokes the systems pager with the supplied data
// falls back to fmt.Println() when paging fails or non-interactive
func fPageln(r io.Reader) {
// get system pager, fallback to `less`
pager := os.Getenv("PAGER")
var args []string
if pager == "" || pager == "less" {
// --RAW-CONTROL-CHARS Honors colors from diff. Must be in all caps, otherwise display issues occur.
// --quit-if-one-screen Closer to the git experience.
// --no-init Don't clear the screen when exiting.
pager = "less"
args = []string{"--RAW-CONTROL-CHARS", "--quit-if-one-screen", "--no-init"}
}
// invoke pager
cmd := exec.Command(pager, args...)
cmd.Stdin = r
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// if this fails, just print it
if err := cmd.Run(); err != nil {
if _, err = io.Copy(os.Stdout, r); err != nil {
log.Fatalln("Writing to Stdout:", err)
}
}
}
// writeJSON writes the given object to the path as a JSON file
func writeJSON(i interface{}, path string) error {
out, err := json.MarshalIndent(i, "", " ")
if err != nil {
return fmt.Errorf("marshalling: %s", err)
}
if err := ioutil.WriteFile(path, append(out, '\n'), 0644); err != nil {
return fmt.Errorf("writing %s: %s", path, err)
}
return nil
}