-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
53 lines (46 loc) · 1.18 KB
/
root.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
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/whitewater-guide/gorge/version"
)
var (
// Used for flags.
endpointURL string
vers bool
rootCmd = &cobra.Command{
Use: fmt.Sprintf("%s [command] [arguments]", filepath.Base(os.Args[0])),
Short: "command line interface to gorge server",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if !strings.HasSuffix(endpointURL, "/") {
endpointURL = endpointURL + "/"
}
},
Run: func(cmd *cobra.Command, args []string) {
if vers {
fmt.Printf("gorge-cli version %s\n", version.Version)
vServer := map[string]string{}
err := Client.GetTo("version", &vServer)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
} else {
fmt.Printf("endpoint version %s\n", vServer["version"])
}
} else {
cmd.Help() //nolint:errcheck
}
},
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
rootCmd.PersistentFlags().StringVar(&endpointURL, "url", "http://localhost:7080", "endpoint url")
rootCmd.Flags().BoolVar(&vers, "version", false, "prints cli and server version")
}