-
Notifications
You must be signed in to change notification settings - Fork 2
/
magda-cli.go
61 lines (51 loc) · 1.37 KB
/
magda-cli.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
// Program to create, update & delete aspect schemas in Magda
package main
import (
"fmt"
"os"
"github.com/maxott/magda-cli/cmd"
"github.com/maxott/magda-cli/pkg/adapter"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/alecthomas/kingpin.v2"
)
var Version string
func main() {
app := cmd.App()
app.HelpFlag.Short('h')
setLogger(zapcore.ErrorLevel)
app.Flag("verbose", "Be chatty [MAGDA_VERBOSE]").Short('v').Envar("MAGDA_VERBOSE").Action(setVerbose).Bool()
app.Flag("debug", "Be very chatty [MAGDA_DEBUG]").Short('d').Envar("MAGDA_DEBUG").Action(setDebug).Bool()
app.Flag("version", "Print out version").Action(printVersion).Bool()
// app.PreAction(configLogger)
_, err := app.Parse(os.Args[1:])
if err != nil {
//var e interface{} = err
if aerr, ok := err.(adapter.IAdapterError); ok {
fmt.Printf("ERROR: %s\n", aerr.Error())
}
}
cmd.Logger().Sync()
}
func setLogger(logLevel zapcore.Level) {
cfg := zap.NewDevelopmentConfig()
cfg.Level = zap.NewAtomicLevelAt(logLevel)
logger, err := cfg.Build()
if err != nil {
panic(err)
}
cmd.SetLogger(logger)
}
func setVerbose(c *kingpin.ParseContext) error {
setLogger(zapcore.InfoLevel)
return nil
}
func setDebug(c *kingpin.ParseContext) error {
setLogger(zapcore.DebugLevel)
return nil
}
func printVersion(c *kingpin.ParseContext) error {
fmt.Printf("Version: %s\n", Version)
os.Exit(0)
return nil
}