/
plugins.go
85 lines (72 loc) · 2.02 KB
/
plugins.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
package main
import (
"fmt"
"os"
"os/exec"
"path"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/plugins"
"github.com/manifoldco/manifold-cli/prompts"
)
func init() {
pluginsCmd := cli.Command{
Name: "plugins",
Usage: "Manage installed plugins",
Category: "UTILITY",
Subcommands: []cli.Command{
{
Name: "install",
Usage: "Install a new plugin",
ArgsUsage: "[repository]",
Action: install,
},
},
}
cmds = append(cmds, pluginsCmd)
}
func install(cliCtx *cli.Context) error {
pluginsDir, err := plugins.Path()
if err != nil {
return cli.NewExitError("Failed to install plugin: "+err.Error(), -1)
}
args := cliCtx.Args()
if len(args) < 1 {
return errs.NewUsageExitError(cliCtx, cli.NewExitError("Missing repository", -1))
}
// Identify the name of the plugin being installed
name := plugins.DeriveName(args[0])
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
spin := prompts.NewSpinner(fmt.Sprintf("Installing plugin `%s`", name))
spin.Start()
defer spin.Stop()
// Abort if already exists
destinationDir := path.Join(pluginsDir, name)
if _, err := os.Stat(destinationDir); !os.IsNotExist(err) {
return cli.NewExitError("Plugin already installed.", -1)
}
// Clone to its destination
pluginURL := plugins.NormalizeURL(args[0])
cmd := exec.Command("git", "clone", pluginURL, destinationDir)
err = cmd.Run()
if err != nil {
return cli.NewExitError("Failed to clone plugin: "+err.Error(), -1)
}
// Initialize the config file
newFile, err := os.Create(path.Join(destinationDir, ".config.json"))
if err != nil {
return cli.NewExitError("Failed to create config file: "+err.Error(), -1)
}
newFile.Close()
spin.Stop()
// Finally output the Help text
exe := plugins.Shortname(name)
fmt.Printf("Plugin installed. Use `manifold %s` to execute.\n\n", exe)
err = plugins.Help(name)
if err != nil {
return cli.NewExitError("Failed to display plugin help output: "+err.Error(), -1)
}
return nil
}