This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
78 lines (67 loc) · 1.38 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
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"io"
"log"
"os"
"runtime"
"github.com/b4b4r07/stein/pkg/logging"
"github.com/mitchellh/cli"
)
const (
// AppName is the application name
AppName = "stein"
Version = "unset"
Revision = "unset"
envEnvPrefix = "STEIN_"
)
// CLI represents the command-line interface
type CLI struct {
Stdout io.Writer
Stderr io.Writer
}
func main() {
logWriter, err := logging.LogOutput()
if err != nil {
panic(err)
}
log.SetOutput(logWriter)
log.Printf("[INFO] Stein version: %s (%s)", Version, Revision)
log.Printf("[INFO] Go runtime version: %s", runtime.Version())
log.Printf("[INFO] CLI args: %#v", os.Args)
stein := CLI{
Stdout: os.Stdout,
Stderr: os.Stderr,
}
app := cli.NewCLI(AppName, Version)
app.Args = os.Args[1:]
app.Commands = map[string]cli.CommandFactory{
"apply": func() (cli.Command, error) {
return &ApplyCommand{CLI: stein}, nil
},
"fmt": func() (cli.Command, error) {
return &FmtCommand{CLI: stein}, nil
},
}
exitStatus, err := app.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] %s: %v\n", AppName, err)
}
os.Exit(exitStatus)
}
func (c CLI) exit(msg interface{}) int {
switch m := msg.(type) {
case int:
return m
case nil:
return 0
case string:
fmt.Fprintf(c.Stdout, "%s\n", m)
return 0
case error:
fmt.Fprintf(c.Stderr, "[ERROR] %s: %s\n", AppName, m.Error())
return 1
default:
panic(msg)
}
}