/
setList.go
117 lines (99 loc) · 2.88 KB
/
setList.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package hossted
import (
"errors"
"fmt"
"regexp"
"sort"
"strings"
)
// ListCommands lists the available services and general commands on the virtual machine.
// The available application is defined under the file /opt/linoovate/run/uuid.txt.
// Also it would depends on whether the "action" is predefined as available of the certain app.
func ListCommands() error {
// Get available apps
config, err := GetConfig()
if err != nil {
return err
}
apps := config.Applications
// Applications available on the vm
vmAppsMap := make(map[string]bool) // e.g. map["prometheus"]true
for _, app := range apps {
vmAppsMap[app.AppName] = true
}
// Get all available apps/commands
m, err := getCommandsMap(GAVAILABLE, AVAILABLE)
if err != nil {
return err
}
// Print general commands
var generalCommands []Command
for k, v := range m { // k: app.command, v: Command{}
if strings.Contains(k, "general") {
generalCommands = append(generalCommands, v)
}
}
// Check matching commands
// TODO: Check if this is still vaild after moving almost all commands to General Available (#39)
var validCommands []Command
for k, v := range m { // k: app.command, v: Command{}
app := getAppNameFromKey(k)
if _, ok := vmAppsMap[app]; ok { // append to validCommands only if its on vm
validCommands = append(validCommands, v)
}
}
// Sort
sort.Slice(validCommands, func(i, j int) bool {
return validCommands[i].App < validCommands[j].App
})
// Check
allCommands := append(generalCommands, validCommands...)
if len(allCommands) == 0 {
return errors.New("No available commands. Please check with administrator.")
}
// Print the commands if available
if len(generalCommands) > 0 {
_ = printCommands(generalCommands)
}
if len(validCommands) > 0 {
_ = printCommands(validCommands)
}
return nil
}
func getAppNameFromKey(key string) string {
var app string
s := strings.Split(key, ".")
if len(s) > 0 {
app = s[0] // Get the app/first part from the key
}
return app
}
// printCommands prints the list of available commands (general, app, etc,..) to the console
// to provide additional information to users on the available commands on the vm
func printCommands(commands []Command) error {
if len(commands) == 0 {
return errors.New("No available commands. Please check with administrator.")
}
// List the available commands (vm + predefined)
var prev string // For formatting only. Group same apps together.
for _, c := range commands {
app := c.App
if prev != app {
prev = app
fmt.Println("")
fmt.Println(app)
fmt.Println("------------")
}
// Handle general commands
if c.App == "general" {
c.App = ""
}
msg := fmt.Sprintf("hossted %s %s %s %s\n", c.CommandGroup, c.Command, c.App, c.Value)
// Replace multiple spaces to one
space := regexp.MustCompile(`\s+`)
s := space.ReplaceAllString(msg, " ")
fmt.Println(s)
}
fmt.Println("")
return nil
}