forked from privateerproj/privateer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
105 lines (94 loc) · 2.87 KB
/
list.go
File metadata and controls
105 lines (94 loc) · 2.87 KB
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
package cmd
import (
"fmt"
"log"
"path"
"strings"
hcplugin "github.com/hashicorp/go-plugin"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cmdName = "list"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: cmdName,
Short: "Consult the Charts! List all plugins that have been installed.",
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("all") {
_, _ = fmt.Fprintln(writer, "| Plugin \t | Available \t| Requested \t|")
for _, pluginPkg := range GetPlugins() {
_, _ = fmt.Fprintf(writer, "| %s \t | %t \t| %t \t|\n", pluginPkg.Name, pluginPkg.Available, pluginPkg.Requested)
}
} else {
// list only the available plugins
_, _ = fmt.Fprintln(writer, "| Plugin \t | Requested \t|")
for _, pluginPkg := range GetPlugins() {
if pluginPkg.Available {
_, _ = fmt.Fprintf(writer, "| %s \t | %t \t|\n", pluginPkg.Name, pluginPkg.Requested)
}
}
}
err := writer.Flush()
if err != nil {
log.Printf("Error flushing writer: %v", err)
}
},
}
func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().BoolP("all", "a", false, "Review the Fleet! List all plugins that have been installed or requested in the current config")
_ = viper.BindPFlag("all", listCmd.PersistentFlags().Lookup("all"))
}
// GetRequestedPlugins returns a list of plugin names requested in the config
func getRequestedPlugins() (requestedPluginPackages []*PluginPkg) {
services := viper.GetStringMap("services")
for serviceName := range services {
pluginName := viper.GetString("services." + serviceName + ".plugin")
pluginPkg := NewPluginPkg(pluginName, serviceName)
pluginPkg.Requested = true
requestedPluginPackages = append(requestedPluginPackages, pluginPkg)
}
return requestedPluginPackages
}
// GetAvailablePlugins returns a list of plugins found in the binaries path
func getAvailablePlugins() (availablePluginPackages []*PluginPkg) {
pluginPaths, _ := hcplugin.Discover("*", viper.GetString("binaries-path"))
for _, pluginPath := range pluginPaths {
pluginPkg := NewPluginPkg(path.Base(pluginPath), "")
pluginPkg.Available = true
if strings.Contains(pluginPkg.Name, "privateer") {
continue
}
availablePluginPackages = append(availablePluginPackages, pluginPkg)
}
return availablePluginPackages
}
var allPlugins []*PluginPkg
func GetPlugins() []*PluginPkg {
if allPlugins != nil {
return allPlugins
}
output := make([]*PluginPkg, 0)
for _, plugin := range getRequestedPlugins() {
if Contains(getAvailablePlugins(), plugin.Name) {
plugin.Available = true
}
output = append(output, plugin)
}
for _, plugin := range getAvailablePlugins() {
if !Contains(output, plugin.Name) {
output = append(output, plugin)
}
}
return output
}
func Contains(slice []*PluginPkg, search string) bool {
for _, plugin := range slice {
if plugin.Name == search {
return true
}
}
return false
}