This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
89 lines (74 loc) · 1.71 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
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"os"
"github.com/alexflint/go-arg"
"github.com/rs/zerolog"
cicero "github.com/input-output-hk/cicero/src"
"github.com/input-output-hk/cicero/src/config"
"github.com/input-output-hk/cicero/src/domain"
)
var buildVersion = "dev"
var buildCommit = "dirty"
func main() {
args := &CLI{}
parser, err := parseArgs(args)
abort(parser, err)
level, err := zerolog.ParseLevel(args.LogLevel)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
logger := config.ConfigureLogger(level)
domain.Build.Version = buildVersion
domain.Build.Commit = buildCommit
abort(parser, Run(parser, args, logger))
}
type CLI struct {
LogLevel string `arg:"--log-level" default:"info"`
Start *cicero.StartCmd `arg:"subcommand:start"`
}
func Version() string {
return fmt.Sprintf("%s (%s)", buildVersion, buildCommit)
}
func (CLI) Version() string {
return fmt.Sprintf("cicero %s", Version())
}
func abort(parser *arg.Parser, err error) {
switch err {
case nil:
return
case arg.ErrHelp:
parser.WriteHelp(os.Stderr)
os.Exit(0)
case arg.ErrVersion:
fmt.Fprintln(os.Stdout, Version())
os.Exit(0)
default:
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func parseArgs(args *CLI) (parser *arg.Parser, err error) {
parser, err = arg.NewParser(arg.Config{}, args)
if err != nil {
return
}
err = parser.Parse(os.Args[1:])
return
}
func Run(parser *arg.Parser, args *CLI, logger *zerolog.Logger) error {
switch {
case args.Start != nil:
if instance, err := cicero.NewInstance(args.Start, logger); err != nil {
return err
} else {
defer instance.Close()
return instance.Run(context.Background())
}
default:
parser.WriteHelp(os.Stderr)
}
return nil
}