-
Notifications
You must be signed in to change notification settings - Fork 199
/
mixins.go
198 lines (167 loc) · 6.86 KB
/
mixins.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"get.porter.sh/porter/pkg/mixin"
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/pkg/pkgmgmt/feed"
"get.porter.sh/porter/pkg/porter"
"github.com/spf13/cobra"
)
func buildMixinCommands(p *porter.Porter) *cobra.Command {
cmd := &cobra.Command{
Use: "mixins",
Aliases: []string{"mixin"},
Short: "Mixin commands. Mixins assist with authoring bundles.",
Annotations: map[string]string{
"group": "resource",
},
}
cmd.AddCommand(buildMixinsListCommand(p))
cmd.AddCommand(buildMixinsSearchCommand(p))
cmd.AddCommand(BuildMixinInstallCommand(p))
cmd.AddCommand(BuildMixinUninstallCommand(p))
cmd.AddCommand(buildMixinsFeedCommand(p))
return cmd
}
func buildMixinsListCommand(p *porter.Porter) *cobra.Command {
opts := porter.PrintMixinsOptions{}
cmd := &cobra.Command{
Use: "list",
Short: "List installed mixins",
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.ParseFormat()
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.PrintMixins(opts)
},
}
cmd.Flags().StringVarP(&opts.RawFormat, "output", "o", "table",
"Output format, allowed values are: table, json, yaml")
return cmd
}
func buildMixinsSearchCommand(p *porter.Porter) *cobra.Command {
opts := porter.SearchOptions{
Type: "mixin",
}
cmd := &cobra.Command{
Use: "search [QUERY]",
Short: "Search available mixins",
Long: `Search available mixins. You can specify an optional mixin name query, where the results are filtered by mixins whose name contains the query term.
By default the community mixin index at https://cdn.porter.sh/mixins/index.json is searched. To search from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`,
Example: ` porter mixin search
porter mixin search helm
porter mixin search -o json`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.SearchPackages(opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.RawFormat, "output", "o", "table",
"Output format, allowed values are: table, json, yaml")
flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror,
"Mirror of official Porter assets")
return cmd
}
func BuildMixinInstallCommand(p *porter.Porter) *cobra.Command {
opts := mixin.InstallOptions{}
cmd := &cobra.Command{
Use: "install NAME",
Short: "Install a mixin",
Long: `Install a mixin.
By default mixins are downloaded from the official Porter mixin feed at https://cdn.porter.sh/mixins/atom.xml. To download from a mirror, set the environment variable PORTER_MIRROR, or mirror in the Porter config file, with the value to replace https://cdn.porter.sh with.`,
Example: ` porter mixin install helm3 --feed-url https://mchorfa.github.io/porter-helm3/atom.xml
porter mixin install azure --version v0.4.0-ralpha.1+dubonnet --url https://cdn.porter.sh/mixins/azure
porter mixin install kubernetes --version canary --url https://cdn.porter.sh/mixins/kubernetes`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.InstallMixin(opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.Version, "version", "v", "latest",
"The mixin version. This can either be a version number, or a tagged release like 'latest' or 'canary'")
flags.StringVar(&opts.URL, "url", "",
"URL from where the mixin can be downloaded, for example https://github.com/org/proj/releases/downloads")
flags.StringVar(&opts.FeedURL, "feed-url", "",
"URL of an atom feed where the mixin can be downloaded. Defaults to the official Porter mixin feed.")
flags.StringVar(&opts.Mirror, "mirror", pkgmgmt.DefaultPackageMirror,
"Mirror of official Porter assets")
return cmd
}
func BuildMixinUninstallCommand(p *porter.Porter) *cobra.Command {
opts := pkgmgmt.UninstallOptions{}
cmd := &cobra.Command{
Use: "uninstall NAME",
Short: "Uninstall a mixin",
Example: ` porter mixin uninstall helm`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.UninstallMixin(opts)
},
}
return cmd
}
func buildMixinsFeedCommand(p *porter.Porter) *cobra.Command {
cmd := &cobra.Command{
Use: "feed",
Aliases: []string{"feeds"},
Short: "Feed commands",
Annotations: map[string]string{
"group": "resource",
},
}
cmd.AddCommand(BuildMixinFeedGenerateCommand(p))
cmd.AddCommand(BuildMixinFeedTemplateCommand(p))
return cmd
}
func BuildMixinFeedGenerateCommand(p *porter.Porter) *cobra.Command {
opts := feed.GenerateOptions{}
cmd := &cobra.Command{
Use: "generate",
Short: "Generate an atom feed from the mixins in a directory",
Long: `Generate an atom feed from the mixins in a directory.
A template is required, providing values for text properties such as the author name, base URLs and other values that cannot be inferred from the mixin file names. You can make a default template by running 'porter mixins feed template'.
The file names of the mixins must follow the naming conventions required of published mixins:
VERSION/MIXIN-GOOS-GOARCH[FILE_EXT]
More than one mixin may be present in the directory, and the directories may be nested a few levels deep, as long as the file path ends with the above naming convention, porter will find and match it. Below is an example directory structure that porter can list to generate a feed:
bin/
└── v1.2.3/
├── mymixin-darwin-amd64
├── mymixin-linux-amd64
└── mymixin-windows-amd64.exe
See https://porter.sh/mixin-dev-guide/distribution more details.
`,
Example: ` porter mixin feed generate
porter mixin feed generate --dir bin --file bin/atom.xml --template porter-atom-template.xml`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.GenerateMixinFeed(opts)
},
}
cmd.Flags().StringVarP(&opts.SearchDirectory, "dir", "d", "",
"The directory to search for mixin versions to publish in the feed. Defaults to the current directory.")
cmd.Flags().StringVarP(&opts.AtomFile, "file", "f", "atom.xml",
"The path of the atom feed output by this command.")
cmd.Flags().StringVarP(&opts.TemplateFile, "template", "t", "atom-template.xml",
"The template atom file used to populate the text fields in the generated feed.")
return cmd
}
func BuildMixinFeedTemplateCommand(p *porter.Porter) *cobra.Command {
cmd := &cobra.Command{
Use: "template",
Short: "Create an atom feed template",
Long: "Create an atom feed template in the current directory",
RunE: func(cmd *cobra.Command, args []string) error {
return p.CreateMixinFeedTemplate()
},
}
return cmd
}