-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
91 lines (74 loc) · 1.57 KB
/
flags.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Package flags add Mode and Config flags to git-in-sync.
package flags
import (
"flag"
"fmt"
"os"
"os/exec"
"github.com/jychri/tilde"
)
// Flags records values for Mode and Config.
type Flags struct {
Mode string
Config string
}
// Init returns validated user input as Flags.
func Init() (f Flags) {
var c, m string
flag.StringVar(&m, "m", "verify", "mode")
flag.StringVar(&c, "c", "~/.gisrc.json", "configuration")
flag.Parse()
switch m {
case "login", "logout", "verify", "oneline", "testing":
default:
m = "verify"
}
if env := os.Getenv("MODE"); env == "TESTING" {
m = "testing"
}
if env := os.Getenv("GISRC"); env != "" {
c = env
}
c = tilde.Abs(c)
return Flags{Mode: m, Config: c}
}
// Testing returns a Flags instance with Mode == "testing".
func Testing(c string) Flags {
return Flags{Mode: "testing", Config: c}
}
// ClearScreen clears the screen.
func (f Flags) ClearScreen() {
switch f.Mode {
case "oneline":
case "testing":
default:
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
}
// Printv calls prints to standard output if not running in 'oneline' or 'testing' mode.
func Printv(f Flags, s string, z ...interface{}) string {
out := fmt.Sprintf(s, z...)
switch f.Mode {
case "oneline":
case "testing":
default:
fmt.Println(out)
}
return out
}
// Login returns true if f.Mode == "login".
func (f Flags) Login() bool {
if f.Mode == "login" {
return true
}
return false
}
// Logout returns true if f.Mode == "logout".
func (f Flags) Logout() bool {
if f.Mode == "logout" {
return true
}
return false
}