This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
ensure.go
184 lines (152 loc) · 4.19 KB
/
ensure.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"os"
pluginbase "k8s.io/helm/pkg/plugin"
"github.com/Azure/draft/pkg/draft/pack/repo"
"github.com/Azure/draft/pkg/plugin"
)
// ensureDirectories checks to see if $DRAFT_HOME exists
//
// If $DRAFT_HOME does not exist, this function will create it.
func (i *initCmd) ensureDirectories() error {
configDirectories := []string{
i.home.String(),
i.home.Plugins(),
i.home.Packs(),
i.home.Logs(),
}
for _, p := range configDirectories {
if fi, err := os.Stat(p); err != nil {
fmt.Fprintf(i.out, "Creating %s \n", p)
if err := os.MkdirAll(p, 0755); err != nil {
return fmt.Errorf("Could not create %s: %s", p, err)
}
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", p)
}
}
return nil
}
// ensureConfig checks to see if $DRAFT_HOME/config.toml exists
//
// If it does not exist, this function will create it.
func (i *initCmd) ensureConfig() error {
fi, err := os.Stat(i.home.Config())
if err != nil {
fmt.Fprintf(i.out, "Creating %s \n", i.home.Config())
f, err := os.Create(i.home.Config())
if err != nil {
return fmt.Errorf("Could not create %s: %s", i.home.Config(), err)
}
defer f.Close()
} else if fi.IsDir() {
return fmt.Errorf("%s must not be a directory", i.home.Config())
}
return nil
}
// ensurePacks checks to see if the default packs exist.
//
// If the pack does not exist, this function will create it.
func (i *initCmd) ensurePacks() error {
existingRepos := repo.FindRepositories(i.home.Packs())
fmt.Fprintln(i.out, "Installing default pack repositories...")
for _, builtin := range repo.Builtins() {
if err := i.ensurePack(builtin, existingRepos); err != nil {
return err
}
}
fmt.Fprintln(i.out, "Installation of default pack repositories complete")
return nil
}
func (i *initCmd) ensurePack(builtin *repo.Builtin, existingRepos []repo.Repository) error {
for _, repo := range existingRepos {
if builtin.Name == repo.Name {
return nil
}
}
addArgs := []string{
"add",
builtin.URL,
}
addFlags := []string{
"--version",
builtin.Version,
"--home",
string(i.home),
fmt.Sprintf("--debug=%v", flagDebug),
}
packRepoCmd, _, err := rootCmd.Find([]string{"pack-repo"})
if err != nil {
return err
}
if err := packRepoCmd.ParseFlags(addFlags); err != nil {
return err
}
if err := packRepoCmd.RunE(packRepoCmd, addArgs); err != nil {
return err
}
debug("Successfully installed pack repo: %v %v", builtin.URL, builtin.Version)
return nil
}
// ensurePlugins checks to see if the default plugins exist.
//
// If the plugin does not exist, this function will add it.
func (i *initCmd) ensurePlugins() error {
existingPlugins, err := findPlugins(pluginDirPath(i.home))
if err != nil {
return err
}
fmt.Fprintln(i.out, "Installing default plugins...")
for _, builtin := range plugin.Builtins() {
if err := i.ensurePlugin(builtin, existingPlugins); err != nil {
return err
}
}
fmt.Fprintln(i.out, "Installation of default plugins complete")
return nil
}
func (i *initCmd) ensurePlugin(builtin *plugin.Builtin, existingPlugins []*pluginbase.Plugin) error {
for _, pl := range existingPlugins {
if builtin.Name == pl.Metadata.Name {
if builtin.Version == pl.Metadata.Version {
return nil
}
debug("Currently have %v version %v. Removing to install %v",
pl.Metadata.Name, pl.Metadata.Version, builtin.Version)
if err := removePlugin(pl); err != nil {
return err
}
debug("Successfully removed %v %v",
pl.Metadata.Name, pl.Metadata.Version)
}
}
installArgs := []string{
builtin.URL,
}
installFlags := []string{
"--version",
builtin.Version,
"--home",
string(i.home),
fmt.Sprintf("--debug=%v", flagDebug),
}
plugInstallCmd, _, err := rootCmd.Find([]string{"plugin", "install"})
if err != nil {
return err
}
if err := plugInstallCmd.ParseFlags(installFlags); err != nil {
return err
}
if err := plugInstallCmd.PreRunE(plugInstallCmd, installArgs); err != nil {
return err
}
if err := plugInstallCmd.RunE(plugInstallCmd, installArgs); err != nil {
return err
}
// reload plugins
loadPlugins(rootCmd, i.home, i.out, i.in)
debug("Successfully installed %v %v from %v",
builtin.Name, builtin.Version, builtin.URL)
return nil
}