forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_config.go
158 lines (133 loc) · 4.5 KB
/
plugins_config.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package configv3
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"code.cloudfoundry.org/cli/util"
)
// PluginsConfig represents the plugin configuration
type PluginsConfig struct {
Plugins map[string]Plugin `json:"Plugins"`
}
// Plugin represents the plugin as a whole, not be confused with PluginCommand
type Plugin struct {
Name string
Location string `json:"Location"`
Version PluginVersion `json:"Version"`
Commands []PluginCommand `json:"Commands"`
}
// CalculateSHA1 returns the sha1 value of the plugin executable. If an error
// is encountered calculating sha1, N/A is returned
func (p Plugin) CalculateSHA1() string {
fileSHA, err := util.NewSha1Checksum(p.Location).ComputeFileSha1()
if err != nil {
return "N/A"
}
return fmt.Sprintf("%x", fileSHA)
}
// PluginCommands returns the plugin's commands sorted by command name.
func (p Plugin) PluginCommands() []PluginCommand {
sort.Slice(p.Commands, func(i, j int) bool {
return strings.ToLower(p.Commands[i].Name) < strings.ToLower(p.Commands[j].Name)
})
return p.Commands
}
// PluginVersion is the plugin version information
type PluginVersion struct {
Major int `json:"Major"`
Minor int `json:"Minor"`
Build int `json:"Build"`
}
// String returns the plugin's version in the format x.y.z.
func (v PluginVersion) String() string {
if v.Major == 0 && v.Minor == 0 && v.Build == 0 {
return "N/A"
}
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Build)
}
// PluginCommand represents an individual command inside a plugin
type PluginCommand struct {
Name string `json:"Name"`
Alias string `json:"Alias"`
HelpText string `json:"HelpText"`
UsageDetails PluginUsageDetails `json:"UsageDetails"`
}
// CommandName returns the name of the plugin. The name is concatenated with
// alias if alias is specified.
func (c PluginCommand) CommandName() string {
if c.Name != "" && c.Alias != "" {
return fmt.Sprintf("%s, %s", c.Name, c.Alias)
}
return c.Name
}
// PluginUsageDetails contains the usage metadata provided by the plugin
type PluginUsageDetails struct {
Usage string `json:"Usage"`
Options map[string]string `json:"Options"`
}
// AddPlugin adds the specified plugin to PluginsConfig
func (config *Config) AddPlugin(plugin Plugin) {
config.pluginsConfig.Plugins[plugin.Name] = plugin
}
func (config *Config) CreatePluginHome() error {
return os.MkdirAll(config.PluginHome(), 0700)
}
// GetPlugin returns the requested plugin and true if it exists.
func (config *Config) GetPlugin(pluginName string) (Plugin, bool) {
plugin, exists := config.pluginsConfig.Plugins[pluginName]
return plugin, exists
}
// GetPluginCaseInsensitive finds the first matching plugin name case
// insensitive and returns true if it exists.
func (config *Config) GetPluginCaseInsensitive(pluginName string) (Plugin, bool) {
for name, plugin := range config.pluginsConfig.Plugins {
if strings.ToLower(name) == strings.ToLower(pluginName) {
return plugin, true
}
}
return Plugin{}, false
}
// PluginHome returns the plugin configuration directory to:
// 1. The $CF_PLUGIN_HOME/.cf/plugins environment variable if set
// 2. Defaults to the home directory (outlined in LoadConfig)/.cf/plugins
func (config *Config) PluginHome() string {
if config.ENV.CFPluginHome != "" {
return filepath.Join(config.ENV.CFPluginHome, ".cf", "plugins")
}
return filepath.Join(homeDirectory(), ".cf", "plugins")
}
// Plugins returns installed plugins from the config sorted by name (case-insensitive).
func (config *Config) Plugins() []Plugin {
plugins := []Plugin{}
for _, plugin := range config.pluginsConfig.Plugins {
plugins = append(plugins, plugin)
}
sort.Slice(plugins, func(i, j int) bool {
return strings.ToLower(plugins[i].Name) < strings.ToLower(plugins[j].Name)
})
return plugins
}
// RemovePlugin removes the specified plugin from PluginsConfig idempotently
func (config *Config) RemovePlugin(pluginName string) {
delete(config.pluginsConfig.Plugins, pluginName)
}
// WritePluginConfig writes the plugin config to config.json in the plugin home
// directory.
func (config *Config) WritePluginConfig() error {
// Marshal JSON
rawConfig, err := json.MarshalIndent(config.pluginsConfig, "", " ")
if err != nil {
return err
}
pluginFileDir := filepath.Join(config.PluginHome())
err = os.MkdirAll(pluginFileDir, 0700)
if err != nil {
return err
}
// Write to file
return ioutil.WriteFile(filepath.Join(pluginFileDir, "config.json"), rawConfig, 0600)
}