...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package shared
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23
24 "github.com/sassoftware/relic/config"
25 "github.com/spf13/cobra"
26 )
27
28 var (
29 ArgConfig string
30 ArgDebug uint32
31 CurrentConfig *config.Config
32
33 argVersion bool
34 )
35
36 var lateHooks []func()
37
38 var RootCmd = &cobra.Command{
39 Use: "relic",
40 PersistentPreRun: showVersion,
41 RunE: bailUnlessVersion,
42 }
43
44 func init() {
45 RootCmd.PersistentFlags().StringVarP(&ArgConfig, "config", "c", "", "Configuration file")
46 RootCmd.PersistentFlags().BoolVar(&argVersion, "version", false, "Show version and exit")
47 RootCmd.PersistentFlags().Uint32VarP(&ArgDebug, "debug", "d", 0, "Log additional diagnostic data. (0-9)")
48 }
49
50 func showVersion(cmd *cobra.Command, args []string) {
51 if argVersion {
52 fmt.Printf("relic version %s (%s)\n", config.Version, config.Commit)
53 os.Exit(0)
54 }
55 }
56
57 func bailUnlessVersion(cmd *cobra.Command, args []string) error {
58 if !argVersion {
59 return errors.New("Expected a command")
60 }
61 return nil
62 }
63
64 func AddLateHook(f func()) {
65 lateHooks = append(lateHooks, f)
66 }
67
68 func Main() {
69 for _, f := range lateHooks {
70 f()
71 }
72 if err := RootCmd.Execute(); err != nil {
73 fmt.Fprintln(os.Stderr, err)
74 os.Exit(1)
75 }
76 }
77
View as plain text