add 'store add' set of commands for content adding

This commit is contained in:
Josh Wolf
2021-11-01 15:29:08 -06:00
parent 786e63f2ef
commit cde59cea74
2 changed files with 205 additions and 0 deletions

View File

@@ -21,6 +21,9 @@ func addStore(parent *cobra.Command) {
addStoreLoad(),
addStoreSave(),
addStoreServe(),
// TODO: Remove this in favor of sync only
addStoreAdd(),
)
parent.AddCommand(cmd)
@@ -139,3 +142,101 @@ func addStoreSave() *cobra.Command {
return cmd
}
func addStoreAdd() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add content to store",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(
addStoreAddFile(),
addStoreAddImage(),
addStoreAddChart(),
)
return cmd
}
func addStoreAddFile() *cobra.Command {
o := &store.AddFileOpts{}
cmd := &cobra.Command{
Use: "file",
Short: "Add a file to the content store",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
s, err := ro.getStore(ctx)
if err != nil {
return err
}
ref := args[0]
return store.AddFileCmd(ctx, o, s, ref)
},
}
o.AddFlags(cmd)
return cmd
}
func addStoreAddImage() *cobra.Command {
o := &store.AddImageOpts{}
cmd := &cobra.Command{
Use: "image",
Short: "Add an image to the content store",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
s, err := ro.getStore(ctx)
if err != nil {
return err
}
ref := args[0]
return store.AddImageCmd(ctx, o, s, ref)
},
}
o.AddFlags(cmd)
return cmd
}
func addStoreAddChart() *cobra.Command {
o := &store.AddChartOpts{}
cmd := &cobra.Command{
Use: "chart",
Short: "Add a chart to the content store",
Example: `
# add a chart
hauler store add longhorn --repo "https://charts.longhorn.io"
# add a specific version of a chart
hauler store add chart rancher --repo "https://releases.rancher.com/server-charts/latest" --version "2.6.2"
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
s, err := ro.getStore(ctx)
if err != nil {
return err
}
return store.AddChartCmd(ctx, o, s, args[0])
},
}
o.AddFlags(cmd)
return cmd
}

104
cmd/hauler/cli/store/add.go Normal file
View File

@@ -0,0 +1,104 @@
package store
import (
"context"
"github.com/spf13/cobra"
"github.com/rancherfederal/hauler/pkg/apis/hauler.cattle.io/v1alpha1"
"github.com/rancherfederal/hauler/pkg/content/chart"
"github.com/rancherfederal/hauler/pkg/content/file"
"github.com/rancherfederal/hauler/pkg/content/image"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/store"
)
type AddFileOpts struct {
Name string
}
func (o *AddFileOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&o.Name, "name", "n", "", "(Optional) Name to assign to file in store")
}
func AddFileCmd(ctx context.Context, o *AddFileOpts, s *store.Store, ref string) error {
l := log.FromContext(ctx)
l.Debugf("running cli command `hauler store add`")
s.Open()
defer s.Close()
cfg := v1alpha1.File{
Ref: ref,
Name: o.Name,
}
f := file.NewFile(cfg)
return s.Add(ctx, f)
}
type AddImageOpts struct {
Name string
}
func (o *AddImageOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()
_ = f
}
func AddImageCmd(ctx context.Context, o *AddImageOpts, s *store.Store, ref string) error {
l := log.FromContext(ctx)
l.Debugf("running cli command `hauler store add image`")
s.Open()
defer s.Close()
cfg := v1alpha1.Image{
Ref: ref,
}
i := image.NewImage(cfg)
return s.Add(ctx, i)
}
type AddChartOpts struct {
Name string
Version string
RepoURL string
// TODO: Support helm auth
Username string
Password string
PassCredentialsAll bool
CertFile string
KeyFile string
CaFile string
InsecureSkipTLSverify bool
RepositoryConfig string
RepositoryCache string
}
func (o *AddChartOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&o.RepoURL, "repo", "r", "", "Chart repository URL")
f.StringVar(&o.Version, "version", "", "(Optional) Version of the chart to download, defaults to latest if not specified")
}
func AddChartCmd(ctx context.Context, o *AddChartOpts, s *store.Store, name string) error {
l := log.FromContext(ctx)
l.Debugf("running cli command `hauler store add chart`")
s.Open()
defer s.Close()
cfg := v1alpha1.Chart{
Name: name,
RepoURL: o.RepoURL,
Version: o.Version,
}
c := chart.NewChart(cfg)
return s.Add(ctx, c)
}