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 396
/
init.go
146 lines (120 loc) · 3.2 KB
/
init.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
package main
import (
"errors"
"fmt"
"io"
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/draft/pack/repo"
"github.com/Azure/draft/pkg/plugin"
)
const (
initDesc = `
This command sets up local configuration in $DRAFT_HOME (default ~/.draft/) with default set of packs, plugins, and other directories required to work with Draft
`
)
type initCmd struct {
clientOnly bool
dryRun bool
out io.Writer
in io.Reader
home draftpath.Home
configFile string
}
func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
i := &initCmd{
out: out,
in: in,
}
cmd := &cobra.Command{
Use: "init",
Short: "sets up local environment to work with Draft",
Long: initDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("This command does not accept arguments")
}
i.home = draftpath.Home(homePath())
return i.run()
},
}
f := cmd.Flags()
f.BoolVar(&i.dryRun, "dry-run", false, "go through all the steps without actually installing anything. Mostly used along with --debug for debugging purposes.")
f.StringVarP(&i.configFile, "config", "f", "", "specify default plugins and pack repositories in a TOML file")
return cmd
}
// runInit initializes local config and installs Draft to Kubernetes Cluster
func (i *initCmd) run() error {
pluginOverrides, repoOverrides, err := i.parseConfig()
if err != nil {
return err
}
if !i.dryRun {
if err := i.setupDraftHome(pluginOverrides, repoOverrides); err != nil {
return err
}
}
fmt.Fprintf(i.out, "$DRAFT_HOME has been configured at %s.\nHappy Sailing!\n", draftHome)
return nil
}
func (i *initCmd) parseConfig() ([]plugin.Builtin, []repo.Builtin, error) {
pluginOverrides := []plugin.Builtin{}
repoOverrides := []repo.Builtin{}
if i.configFile != "" {
var err error
pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
if err != nil {
return pluginOverrides, repoOverrides, fmt.Errorf("Could not parse config file: %s", err)
}
}
return pluginOverrides, repoOverrides, nil
}
func (i *initCmd) setupDraftHome(plugins []plugin.Builtin, repos []repo.Builtin) error {
ensureFuncs := []func() error{
i.ensureDirectories,
i.ensureConfig,
}
for _, funct := range ensureFuncs {
if err := funct(); err != nil {
return err
}
}
if err := i.ensurePlugins(plugins); err != nil {
return err
}
if err := i.ensurePacks(repos); err != nil {
return err
}
return nil
}
type obj struct {
Name string `toml:"name"`
URL string `toml:"url"`
Version string `toml:"version"`
}
func parseConfigFile(f string) ([]plugin.Builtin, []repo.Builtin, error) {
var conf map[string][]obj
if _, err := toml.DecodeFile(f, &conf); err != nil {
return nil, nil, err
}
plugins := []plugin.Builtin{}
for _, pl := range conf["plugin"] {
p := plugin.Builtin{
Name: pl.Name,
Version: pl.Version,
URL: pl.URL,
}
plugins = append(plugins, p)
}
repos := []repo.Builtin{}
for _, re := range conf["repo"] {
r := repo.Builtin{
Name: re.Name,
Version: re.Version,
URL: re.URL,
}
repos = append(repos, r)
}
return plugins, repos, nil
}