-
Notifications
You must be signed in to change notification settings - Fork 151
/
utils.go
61 lines (49 loc) · 1.55 KB
/
utils.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
package interactive
import (
"fmt"
"io"
"os"
"github.com/chzyer/readline"
isatty "github.com/mattn/go-isatty"
"github.com/scaleway/scaleway-sdk-go/validation"
)
type ValidateFunc func(string) error
var (
// IsInteractive must be set to print anything with Printer functions (Print, Printf,...).
IsInteractive = isInteractive()
// outputWriter is the writer used by Printer functions (Print, Printf,...).
outputWriter io.Writer
// defaultValidateFunc is used by readline to validate user input.
defaultValidateFunc ValidateFunc = func(string) error { return nil }
)
// SetOutputWriter set the output writer that will be used by both Printer functions (Print, Printf,...) and
// readline prompter. This should be called once from the bootstrap function.
func SetOutputWriter(w io.Writer) {
outputWriter = w
readline.Stdout = newWriteCloser(w)
}
// we should expect both Stdin and Stderr to enable interactive mode
func isInteractive() bool {
return isatty.IsTerminal(os.Stdin.Fd()) && isatty.IsTerminal(os.Stderr.Fd()) ||
isatty.IsCygwinTerminal(os.Stdin.Fd()) && isatty.IsCygwinTerminal(os.Stderr.Fd()) // windows cygwin terminal
}
func ValidateOrganizationID() ValidateFunc {
return func(s string) error {
if !validation.IsOrganizationID(s) {
return fmt.Errorf("invalid organization-id")
}
return nil
}
}
func newWriteCloser(w io.Writer) io.WriteCloser {
return &writeCloser{w}
}
type writeCloser struct {
io.Writer
}
func (wc *writeCloser) Close() error {
if closer, ok := wc.Writer.(io.Closer); ok {
return closer.Close()
}
return nil
}