-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
72 lines (63 loc) · 1.42 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/tj/go-spin"
"github.com/urfave/cli"
)
// https://developers.google.com/admin-sdk/directory/v1/reference/groups/list
// As an account administrator, you can also use the my_customer alias to represent your account's customerId.
const myAccoutsCustomerID = "my_customer"
func ifZero(i, j int) int {
if i == 0 {
return j
}
return i
}
func showSpinnerWhile(c *cli.Context) func() {
// no spinner while verbose logging
if c.GlobalBool("v") {
return func() {}
}
spinner := spin.New()
spinner.Set(spin.Box1)
done := make(chan bool)
go func() {
for {
select {
case <-done:
default:
// reprint new spinner state
fmt.Fprintf(os.Stderr, "\r%s", spinner.Next())
time.Sleep(100 * time.Millisecond)
}
}
}()
return func() {
done <- true
// remove spinner
fmt.Fprintf(os.Stderr, "\033[%dD", 1)
}
}
func optionJSON(c *cli.Context, v interface{}) bool {
wantsJSON := c.Bool("json")
if wantsJSON {
data, _ := json.MarshalIndent(v, "", "\t")
fmt.Println(string(data))
}
return wantsJSON
}
func promptForYes(c *cli.Context, message string) bool {
// Don't prompt for confirmation if the quiet flag is enabled
if c.GlobalBool("quiet") {
return true
}
reader := bufio.NewReader(os.Stdin)
fmt.Print(message)
yn, _ := reader.ReadString('\n')
return strings.HasPrefix(yn, "Y") || strings.HasPrefix(yn, "y")
}