-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
102 lines (85 loc) · 2.53 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
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"text/tabwriter"
hcplugin "github.com/hashicorp/go-plugin"
"github.com/probr/probr/internal/config"
"github.com/probr/probr/internal/flags"
"github.com/probr/probr/run"
)
var (
// See Makefile for more on how this package is built
// Version is the main version number that is being run at the moment
Version = "0.1.0"
// VersionPostfix is a marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc", etc.
VersionPostfix = "dev"
// GitCommitHash references the commit id at build time
GitCommitHash = ""
// BuiltAt is the build date
BuiltAt = ""
)
func main() {
var subCommand string
if len(os.Args) > 1 {
subCommand = os.Args[1]
}
switch subCommand {
// Ref: https://gobyexample.com/command-line-subcommands
case "list":
flags.List.Parse(os.Args[2:])
listServicePacks()
case "version":
flags.Version.Parse(os.Args[2:])
printVersion()
default:
flags.Run.Parse(os.Args[1:])
run.CLIContext()
}
}
func printVersion() {
if VersionPostfix != "" {
Version = fmt.Sprintf("%s-%s", Version, VersionPostfix)
}
fmt.Fprintf(os.Stdout, "Probr Version: %s", Version)
if config.Vars.Verbose != nil && *config.Vars.Verbose {
fmt.Fprintln(os.Stdout)
fmt.Fprintf(os.Stdout, "Commit : %s", GitCommitHash)
fmt.Fprintln(os.Stdout)
fmt.Fprintf(os.Stdout, "Built at : %s", BuiltAt)
}
}
// listServicePacks reads all service packs declared in config and checks whether they are installed
func listServicePacks() {
servicePackNames, err := getPackNames()
if err != nil {
log.Fatalf("An error occurred while retrieving service packs from config: %v", err)
}
servicePacks := make(map[string]string)
for _, pack := range servicePackNames {
packName, binErr := run.GetPackBinary(pack)
if binErr != nil {
servicePacks[pack] = fmt.Sprintf("ERROR: %v", binErr)
} else {
servicePacks[filepath.Base(packName)] = "OK"
}
}
// Print output
writer := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintln(writer, "| Service Pack\t | Installed ")
for k, v := range servicePacks {
fmt.Fprintf(writer, "| %s\t | %s\n", k, v)
}
writer.Flush()
}
// getPackNames returns all service packs declared in config file
func getPackNames() (packNames []string, err error) {
if err != nil || (config.Vars.AllPacks != nil && *config.Vars.AllPacks) {
return hcplugin.Discover("*", config.Vars.BinariesPath)
}
return config.Vars.Run, nil
}