-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
67 lines (58 loc) · 1.36 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/DavidGamba/go-getoptions"
)
var Logger = log.New(io.Discard, "", log.LstdFlags)
func main() {
os.Exit(program(os.Args))
}
func program(args []string) int {
opt := getoptions.New()
opt.SetUnknownMode(getoptions.Pass)
opt.Bool("debug", false)
opt.SetCommandFn(Run)
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if opt.Called("debug") {
Logger.SetOutput(os.Stderr)
}
Logger.Println(remaining)
ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()
err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
if errors.Is(err, getoptions.ErrorParsing) {
fmt.Fprintf(os.Stderr, "\n"+opt.Help())
}
return 1
}
return 0
}
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Printf("Running")
for _, e := range os.Environ() {
parts := strings.Split(e, "=")
key := strings.ToLower(parts[0])
if strings.Contains(key, "key") || strings.Contains(key, "password") || strings.Contains(key, "token") {
fmt.Printf("%s=%s\n", parts[0], "***")
} else {
fmt.Println(e)
}
}
return nil
}