This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
installer.go
86 lines (69 loc) · 2.07 KB
/
installer.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
package installer
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/plugin"
)
// special thanks to the Kubernets Helm plugin installer pkg
// Debug enables verbose output.
var Debug bool
// Installer provides an interface for installing client plugins.
type Installer interface {
// Install adds a plugin to a path
Install() error
// Path is the directory of the installed plugin.
Path() string
// Update updates a plugin to $DRAFT_HOME.
Update() error
}
// Install installs a plugin to $DRAFT_HOME
func Install(i Installer) error {
if _, pathErr := os.Stat(path.Dir(i.Path())); os.IsNotExist(pathErr) {
return plugin.ErrHomeMissing
}
if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) {
return plugin.ErrExists
}
return i.Install()
}
// Update updates a plugin in $DRAFT_HOME.
func Update(i Installer) error {
if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) {
return plugin.ErrDoesNotExist
}
return i.Update()
}
// FindSource determines the correct Installer for the given source.
func FindSource(location string, home draftpath.Home) (Installer, error) {
installer, err := existingVCSRepo(location, home)
if err != nil && err.Error() == "Cannot detect VCS" {
return installer, plugin.ErrMissingSource
}
return installer, err
}
// New determines and returns the correct Installer for the given source
func New(source, version string, home draftpath.Home) (Installer, error) {
if isLocalReference(source) {
return NewLocalInstaller(source, home)
}
return NewVCSInstaller(source, version, home)
}
func debug(format string, args ...interface{}) {
if Debug {
format = fmt.Sprintf("[debug] %s\n", format)
fmt.Printf(format, args...)
}
}
// isLocalReference checks if the source exists on the filesystem.
func isLocalReference(source string) bool {
_, err := os.Stat(source)
return err == nil
}
// isPlugin checks if the directory contains a plugin.yaml file.
func isPlugin(dirname string) bool {
_, err := os.Stat(filepath.Join(dirname, "plugin.yaml"))
return err == nil
}