-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.go
156 lines (123 loc) · 3.1 KB
/
system.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cli
import (
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
// System is passed to commands as an argument when the command is run. It
// provides an IO interface for the command to use that can be easily attached
// to STDIN/STDOUT or to bytes.Buffer for testing
type System interface {
Environ() []string
Getenv(string) string
Args() []string
Print(...interface{}) (int, error)
Printf(string, ...interface{}) (int, error)
Println(...interface{}) (int, error)
Scan(...interface{}) (int, error)
Scanf(string, ...interface{}) (int, error)
Log(...interface{})
Logf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
Fatalln(...interface{})
Exit(int)
ReadPassword() (string, error)
}
type BaseSystem struct {
In io.Reader
Out io.Writer
Logger *log.Logger
Environment map[string]string
Arguments []string
}
type ExitStatus int
func (s *BaseSystem) Environ() []string {
environ := make([]string, len(s.Environment))
i := 0
for k, v := range s.Environment {
environ[i] = fmt.Sprintf("%s=%s", k, v)
i += 1
}
sort.Strings(environ)
return environ
}
func (s *BaseSystem) Getenv(k string) string {
if v, ok := s.Environment[k]; ok {
return v
}
return ""
}
func (s *BaseSystem) Args() []string {
return s.Arguments
}
func (s *BaseSystem) Print(a ...interface{}) (int, error) {
return fmt.Fprint(s.Out, a...)
}
func (s *BaseSystem) Printf(format string, a ...interface{}) (int, error) {
return fmt.Fprintf(s.Out, format, a...)
}
func (s *BaseSystem) Println(a ...interface{}) (int, error) {
return fmt.Fprintln(s.Out, a...)
}
func (s *BaseSystem) Scan(a ...interface{}) (int, error) {
return fmt.Fscan(s.In, a...)
}
func (s *BaseSystem) Scanf(format string, a ...interface{}) (int, error) {
return fmt.Fscanf(s.In, format, a...)
}
func (s *BaseSystem) Scanln(a ...interface{}) (int, error) {
return fmt.Fscanln(s.In, a...)
}
func (s *BaseSystem) Log(a ...interface{}) {
s.Logger.Println(a...)
}
func (s *BaseSystem) Logf(format string, a ...interface{}) {
s.Logger.Printf(format, a...)
}
func (s *BaseSystem) Fatal(v ...interface{}) {
s.Logger.Print(v...)
panic(ExitStatus(1))
}
func (s *BaseSystem) Fatalf(format string, v ...interface{}) {
s.Logger.Printf(format, v...)
panic(ExitStatus(1))
}
func (s *BaseSystem) Fatalln(v ...interface{}) {
s.Logger.Println(v...)
panic(ExitStatus(1))
}
func (s *BaseSystem) Exit(code int) {
panic(ExitStatus(code))
}
type UnixSystem struct {
*BaseSystem
}
func NewUnixSystem() *UnixSystem {
env := os.Environ()
environment := make(map[string]string, len(env))
for _, e := range env {
split := strings.Split(e, "=")
environment[split[0]] = split[1]
}
arguments := os.Args
return &UnixSystem{&BaseSystem{
In: os.Stdin,
Out: os.Stdout,
Logger: log.New(os.Stderr, "", log.LstdFlags),
Environment: environment,
Arguments: arguments,
}}
}
func (s *UnixSystem) ReadPassword() (string, error) {
cloaked, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", nil
}
return string(cloaked), nil
}