-
Notifications
You must be signed in to change notification settings - Fork 398
/
plugin.go
139 lines (113 loc) · 3.04 KB
/
plugin.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
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/pluginhost"
"github.com/spf13/cobra"
)
// Plugin creates the plugin commands hierarchy.
func Plugin() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "plugin",
Short: "plugin commands",
Long: "plugin is used to access plugin commands",
Aliases: []string{"p"},
},
}
CmdBuilder(cmd, RunPluginList, "list", "list plugins", Writer,
aliasOpt("ls"))
CmdBuilder(cmd, RunPluginRun, "run", "run plugin", Writer)
return cmd
}
// RunPluginRun is a command for running a plugin.
func RunPluginRun(c *CmdConfig) error {
if len(c.Args) == 0 {
return fmt.Errorf("missing plugin name")
}
plugs, err := searchPlugins()
if err != nil {
return err
}
var selectedPlugin *displayers.PlugDesc
for i, p := range plugs {
if p.Name == c.Args[0] {
selectedPlugin = &plugs[i]
}
}
if selectedPlugin == nil {
return fmt.Errorf("unknown plugin %q", c.Args[0])
}
var pluginArgs []string
if len(c.Args) > 1 {
pluginArgs = c.Args[1:]
}
host, err := pluginhost.NewHost(selectedPlugin.Path)
if err != nil {
return err
}
var method string
var methodArgs []string
switch l := len(pluginArgs); {
case l == 0:
method = "Default"
case l == 1:
method = pluginArgs[0]
default:
method = pluginArgs[0]
methodArgs = pluginArgs[1:]
}
if len(pluginArgs) > 1 {
methodArgs = pluginArgs[1:]
}
results, err := host.Call(selectedPlugin.Name+"."+strings.Title(method), methodArgs...)
if err != nil {
return err
}
fmt.Fprintln(c.Out, results)
return nil
}
// RunPluginList is a command for listing available plugins.
func RunPluginList(c *CmdConfig) error {
plugs, err := searchPlugins()
if err != nil {
return err
}
item := &displayers.Plugin{Plugins: plugs}
return c.Display(item)
}
func searchPlugins() ([]displayers.PlugDesc, error) {
envPath := os.Getenv("PATH")
paths := strings.Split(envPath, string(os.PathListSeparator))
var plugs []displayers.PlugDesc
for _, p := range paths {
matches, err := filepath.Glob(filepath.Join(p, "doit-provider-*"))
if err != nil {
return nil, err
}
for _, pluginPath := range matches {
name := pluginName(pluginPath)
plugs = append(plugs, displayers.PlugDesc{Path: pluginPath, Name: name})
}
}
return plugs, nil
}
func pluginName(p string) string {
base := filepath.Base(p)
return strings.TrimPrefix(base, "doit-provider-")
}