Debian Codegraph @

cmdline/shared/main.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package shared
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23
24 "github.com/spf13/cobra"
25
26 "github.com/sassoftware/relic/v7/config"
27 )
28
29 var (
30 ArgConfig string
31 ArgDebug uint32
32 CurrentConfig *config.Config
33
34 argVersion bool
35 )
36
37 var lateHooks []func()
38
39 var RootCmd = &cobra.Command{
40 Use: "relic",
41 PersistentPreRun: showVersion,
42 RunE: bailUnlessVersion,
43 }
44
45 func init() {
46 RootCmd.PersistentFlags().StringVarP(&ArgConfig, "config", "c", "", "Configuration file")
47 RootCmd.PersistentFlags().BoolVar(&argVersion, "version", false, "Show version and exit")
48 RootCmd.PersistentFlags().Uint32VarP(&ArgDebug, "debug", "d", 0, "Log additional diagnostic data. (0-9)")
49 }
50
51 func showVersion(cmd *cobra.Command, args []string) {
52 if argVersion {
53 fmt.Printf("relic version %s (%s)\n", config.Version, config.Commit)
54 os.Exit(0)
55 }
56 }
57
58 func bailUnlessVersion(cmd *cobra.Command, args []string) error {
59 if !argVersion {
60 return errors.New("Expected a command")
61 }
62 return nil
63 }
64
65 func AddLateHook(f func()) {
66 lateHooks = append(lateHooks, f)
67 }
68
69 func Main() {
70 for _, f := range lateHooks {
71 f()
72 }
73 if err := RootCmd.Execute(); err != nil {
74 fmt.Fprintln(os.Stderr, err)
75 os.Exit(1)
76 }
77 }