forked from whyrusleeping/gx
/
ui.go
71 lines (61 loc) · 1.32 KB
/
ui.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
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
gx "github.com/whyrusleeping/gx/gxutil"
. "github.com/whyrusleeping/stump"
)
func promptUser(query string) string {
fmt.Printf("%s ", query)
scan := bufio.NewScanner(os.Stdin)
scan.Scan()
return scan.Text()
}
func yesNoPrompt(prompt string, def bool) bool {
opts := "[y/N]"
if def {
opts = "[Y/n]"
}
fmt.Printf("%s %s ", prompt, opts)
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
val := strings.ToLower(scan.Text())
switch val {
case "":
return def
case "y":
return true
case "n":
return false
default:
fmt.Println("please type 'y' or 'n'")
}
}
panic("unexpected termination of stdin")
}
func jsonPrint(i interface{}) {
out, _ := json.MarshalIndent(i, "", " ")
outs, err := strconv.Unquote(string(out)) // for printing out raw strings
if err != nil {
outs = string(out)
}
Log(outs)
}
func printDepsTree(pm *gx.PM, pkg *gx.Package, quiet bool, indent int) error {
return pkg.ForEachDep(func(dep *gx.Dependency, dpkg *gx.Package) error {
label := dep.Hash
if !quiet {
label = fmt.Sprintf("%s %s %s", dep.Name, dep.Hash, dep.Version)
}
Log("%s%s", strings.Repeat(" ", indent), label)
err := printDepsTree(pm, dpkg, quiet, indent+1)
if err != nil {
return err
}
return nil
})
}