-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
59 lines (46 loc) · 1.9 KB
/
command.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
package update
import (
"errors"
"fmt"
"io"
"strings"
"github.com/alecthomas/kong"
"github.com/gideaworx/terraform-exporter/registry"
"github.com/gideaworx/terraform-exporter/runner"
)
var ErrPluginNewer = errors.New("installed plugin is newer than the candidate")
type Command struct {
LocalFile bool `short:"f" help:"If set, treat the plugin-name arg as the path to a local file"`
Registry string `short:"r" default:"default" help:"The name of the registry to install the plugin from"`
PluginVersion string `help:"The version to install from the registry. Ignored if --local-file is set"`
AllowDowngrades bool `default:"false" help:"If set, allow an upgrade even if the new version is lower than the installed version"`
Install bool `default:"false" help:"If set, install the plugin if the plugin isn't already installed"`
PluginName string `arg:"" help:"The name of the plugin to install if --registry is true, or the path to the executable plugin if --local-file is set"`
pluginHomeDir string `kong:"-"`
ctx *kong.Context `kong:"-"`
in io.Reader `kong:"-"`
r *registry.PluginRegistries `kong:"-"`
}
func (i *Command) BeforeApply(ctx *kong.Context) error {
i.ctx = ctx
i.in = strings.NewReader("")
var err error
i.r, err = registry.LoadFromDisk()
return err
}
func (i *Command) Run() error {
pluginHome, err := runner.PluginHome()
if err != nil {
return err
}
i.pluginHomeDir = pluginHome
version := "the latest version"
if i.PluginVersion != "" {
version = fmt.Sprintf("version %s", i.PluginVersion)
}
fmt.Fprintf(i.ctx.Stdout, "Updating %s to %s\n\n", i.PluginName, version)
if i.LocalFile {
return i.localUpdate()
}
return i.registryUpdate()
}