forked from Azure/draft-classic
/
plugin_install.go
77 lines (64 loc) · 1.7 KB
/
plugin_install.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
package main
import (
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/plugin"
"github.com/Azure/draft/pkg/plugin/installer"
)
type pluginInstallCmd struct {
source string
version string
home draftpath.Home
out io.Writer
args []string
}
func newPluginInstallCmd(out io.Writer) *cobra.Command {
pcmd := &pluginInstallCmd{
out: out,
args: []string{"plugin"},
}
cmd := &cobra.Command{
Use: "install [options] <path|url>...",
Short: "install one or more Draft plugins",
PreRunE: func(cmd *cobra.Command, args []string) error {
return pcmd.complete(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return pcmd.run()
},
}
cmd.Flags().StringVar(&pcmd.version, "version", "", "specify a version constraint. If this is not specified, the latest version is installed")
return cmd
}
func (pcmd *pluginInstallCmd) complete(args []string) error {
if err := validateArgs(args, pcmd.args); err != nil {
return err
}
pcmd.source = args[0]
pcmd.home = draftpath.Home(homePath())
return nil
}
func (pcmd *pluginInstallCmd) run() error {
installer.Debug = flagDebug
i, err := installer.New(pcmd.source, pcmd.version, pcmd.home)
if err != nil {
return err
}
debug("installing plugin from %s", pcmd.source)
if err := installer.Install(i); err != nil {
return err
}
debug("loading plugin from %s", i.Path())
p, err := plugin.LoadDir(i.Path())
if err != nil {
return err
}
debug("running any install instructions for plugin: %s", p.Metadata.Name)
if err := runHook(p, plugin.Install); err != nil {
return err
}
fmt.Fprintf(pcmd.out, "Installed plugin: %s\n", p.Metadata.Name)
return nil
}