Skip to content

Commit

Permalink
Merge branch 'main' into testing-linting-updates
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Brady <[email protected]>
  • Loading branch information
zackbradys authored Aug 25, 2024
2 parents ffaed43 + 2d930b5 commit 03f48ae
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 295 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ tmp/
bin/
store/
registry/
fileserver/
cmd/hauler/binaries
coverage.out
11 changes: 4 additions & 7 deletions cmd/hauler/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ package cli
import (
"github.com/spf13/cobra"

"github.com/rancherfederal/hauler/internal/flags"
"github.com/rancherfederal/hauler/pkg/log"
)

type rootOpts struct {
logLevel string
}

var ro = &rootOpts{}
var ro = &flags.CliRootOpts{}

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "hauler",
Short: "Airgap Swiss Army Knife",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
l := log.FromContext(cmd.Context())
l.SetLevel(ro.logLevel)
l.SetLevel(ro.LogLevel)
l.Debugf("running cli command [%s]", cmd.CommandPath())
return nil
},
Expand All @@ -28,7 +25,7 @@ func New() *cobra.Command {
}

pf := cmd.PersistentFlags()
pf.StringVarP(&ro.logLevel, "log-level", "l", "info", "")
pf.StringVarP(&ro.LogLevel, "log-level", "l", "info", "")

// Add subcommands
addLogin(cmd)
Expand Down
20 changes: 4 additions & 16 deletions cmd/hauler/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,12 @@ import (
"github.com/spf13/cobra"
"oras.land/oras-go/pkg/content"

"github.com/rancherfederal/hauler/internal/flags"
"github.com/rancherfederal/hauler/pkg/cosign"
)

type Opts struct {
Username string
Password string
PasswordStdin bool
}

func (o *Opts) AddArgs(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&o.Username, "username", "u", "", "Username to use for authentication")
f.StringVarP(&o.Password, "password", "p", "", "Password to use for authentication")
f.BoolVar(&o.PasswordStdin, "password-stdin", false, "Password to use for authentication (from stdin)")
}

func addLogin(parent *cobra.Command) {
o := &Opts{}
o := &flags.LoginOpts{}

cmd := &cobra.Command{
Use: "login",
Expand Down Expand Up @@ -55,12 +43,12 @@ hauler login reg.example.com -u bob -p haulin`,
return login(ctx, o, arg[0])
},
}
o.AddArgs(cmd)
o.AddFlags(cmd)

parent.AddCommand(cmd)
}

func login(ctx context.Context, o *Opts, registry string) error {
func login(ctx context.Context, o *flags.LoginOpts, registry string) error {
ropts := content.RegistryOptions{
Username: o.Username,
Password: o.Password,
Expand Down
35 changes: 18 additions & 17 deletions cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"helm.sh/helm/v3/pkg/action"

"github.com/rancherfederal/hauler/cmd/hauler/cli/store"
"github.com/rancherfederal/hauler/internal/flags"
)

var rootStoreOpts = &store.RootOpts{}
var rootStoreOpts = &flags.StoreRootOpts{}

func addStore(parent *cobra.Command) {
cmd := &cobra.Command{
Expand All @@ -20,7 +21,7 @@ func addStore(parent *cobra.Command) {
return cmd.Help()
},
}
rootStoreOpts.AddArgs(cmd)
rootStoreOpts.AddFlags(cmd)

cmd.AddCommand(
addStoreSync(),
Expand All @@ -39,7 +40,7 @@ func addStore(parent *cobra.Command) {
}

func addStoreExtract() *cobra.Command {
o := &store.ExtractOpts{RootOpts: rootStoreOpts}
o := &flags.ExtractOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "extract",
Expand All @@ -57,13 +58,13 @@ func addStoreExtract() *cobra.Command {
return store.ExtractCmd(ctx, o, s, args[0])
},
}
o.AddArgs(cmd)
o.AddFlags(cmd)

return cmd
}

func addStoreSync() *cobra.Command {
o := &store.SyncOpts{RootOpts: rootStoreOpts}
o := &flags.SyncOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "sync",
Expand All @@ -85,7 +86,7 @@ func addStoreSync() *cobra.Command {
}

func addStoreLoad() *cobra.Command {
o := &store.LoadOpts{RootOpts: rootStoreOpts}
o := &flags.LoadOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "load",
Expand Down Expand Up @@ -126,7 +127,7 @@ func addStoreServe() *cobra.Command {

// RegistryCmd serves the embedded registry
func addStoreServeRegistry() *cobra.Command {
o := &store.ServeRegistryOpts{RootOpts: rootStoreOpts}
o := &flags.ServeRegistryOpts{StoreRootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "registry",
Short: "Serve the embedded registry",
Expand All @@ -149,7 +150,7 @@ func addStoreServeRegistry() *cobra.Command {

// FileServerCmd serves the file server
func addStoreServeFiles() *cobra.Command {
o := &store.ServeFilesOpts{RootOpts: rootStoreOpts}
o := &flags.ServeFilesOpts{StoreRootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "fileserver",
Short: "Serve the file server",
Expand All @@ -171,7 +172,7 @@ func addStoreServeFiles() *cobra.Command {
}

func addStoreSave() *cobra.Command {
o := &store.SaveOpts{RootOpts: rootStoreOpts}
o := &flags.SaveOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "save",
Expand All @@ -189,13 +190,13 @@ func addStoreSave() *cobra.Command {
return store.SaveCmd(ctx, o, o.FileName)
},
}
o.AddArgs(cmd)
o.AddFlags(cmd)

return cmd
}

func addStoreInfo() *cobra.Command {
o := &store.InfoOpts{RootOpts: rootStoreOpts}
o := &flags.InfoOpts{StoreRootOpts: rootStoreOpts}

var allowedValues = []string{"image", "chart", "file", "sigs", "atts", "sbom", "all"}

Expand Down Expand Up @@ -226,7 +227,7 @@ func addStoreInfo() *cobra.Command {
}

func addStoreCopy() *cobra.Command {
o := &store.CopyOpts{RootOpts: rootStoreOpts}
o := &flags.CopyOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "copy",
Expand Down Expand Up @@ -267,7 +268,7 @@ func addStoreAdd() *cobra.Command {
}

func addStoreAddFile() *cobra.Command {
o := &store.AddFileOpts{RootOpts: rootStoreOpts}
o := &flags.AddFileOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "file",
Expand All @@ -290,7 +291,7 @@ func addStoreAddFile() *cobra.Command {
}

func addStoreAddImage() *cobra.Command {
o := &store.AddImageOpts{RootOpts: rootStoreOpts}
o := &flags.AddImageOpts{StoreRootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "image",
Expand All @@ -313,9 +314,9 @@ func addStoreAddImage() *cobra.Command {
}

func addStoreAddChart() *cobra.Command {
o := &store.AddChartOpts{
RootOpts: rootStoreOpts,
ChartOpts: &action.ChartPathOptions{},
o := &flags.AddChartOpts{
StoreRootOpts: rootStoreOpts,
ChartOpts: &action.ChartPathOptions{},
}

cmd := &cobra.Command{
Expand Down
51 changes: 4 additions & 47 deletions cmd/hauler/cli/store/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/google/go-containerregistry/pkg/name"
"github.com/rancherfederal/hauler/pkg/artifacts/file/getter"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action"

"github.com/rancherfederal/hauler/internal/flags"
"github.com/rancherfederal/hauler/pkg/apis/hauler.cattle.io/v1alpha1"
"github.com/rancherfederal/hauler/pkg/artifacts/file"
"github.com/rancherfederal/hauler/pkg/content/chart"
Expand All @@ -17,17 +17,7 @@ import (
"github.com/rancherfederal/hauler/pkg/store"
)

type AddFileOpts struct {
*RootOpts
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.Layout, reference string) error {
func AddFileCmd(ctx context.Context, o *flags.AddFileOpts, s *store.Layout, reference string) error {
cfg := v1alpha1.File{
Path: reference,
}
Expand Down Expand Up @@ -61,20 +51,7 @@ func storeFile(ctx context.Context, s *store.Layout, fi v1alpha1.File) error {
return nil
}

type AddImageOpts struct {
*RootOpts
Name string
Key string
Platform string
}

func (o *AddImageOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&o.Key, "key", "k", "", "(Optional) Path to the key for digital signature verification")
f.StringVarP(&o.Platform, "platform", "p", "", "(Optional) Specific platform to save. i.e. linux/amd64. Defaults to all if flag is omitted.")
}

func AddImageCmd(ctx context.Context, o *AddImageOpts, s *store.Layout, reference string) error {
func AddImageCmd(ctx context.Context, o *flags.AddImageOpts, s *store.Layout, reference string) error {
l := log.FromContext(ctx)
cfg := v1alpha1.Image{
Name: reference,
Expand Down Expand Up @@ -111,27 +88,7 @@ func storeImage(ctx context.Context, s *store.Layout, i v1alpha1.Image, platform
return nil
}

type AddChartOpts struct {
*RootOpts

ChartOpts *action.ChartPathOptions
}

func (o *AddChartOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.StringVar(&o.ChartOpts.RepoURL, "repo", "", "chart repository url where to locate the requested chart")
f.StringVar(&o.ChartOpts.Version, "version", "", "specify a version constraint for the chart version to use. This constraint can be a specific tag (e.g. 1.1.1) or it may reference a valid range (e.g. ^2.0.0). If this is not specified, the latest version is used")
f.BoolVar(&o.ChartOpts.Verify, "verify", false, "verify the package before using it")
f.StringVar(&o.ChartOpts.Username, "username", "", "chart repository username where to locate the requested chart")
f.StringVar(&o.ChartOpts.Password, "password", "", "chart repository password where to locate the requested chart")
f.StringVar(&o.ChartOpts.CertFile, "cert-file", "", "identify HTTPS client using this SSL certificate file")
f.StringVar(&o.ChartOpts.KeyFile, "key-file", "", "identify HTTPS client using this SSL key file")
f.BoolVar(&o.ChartOpts.InsecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the chart download")
f.StringVar(&o.ChartOpts.CaFile, "ca-file", "", "verify certificates of HTTPS-enabled servers using this CA bundle")
}

func AddChartCmd(ctx context.Context, o *AddChartOpts, s *store.Layout, chartName string) error {
func AddChartCmd(ctx context.Context, o *flags.AddChartOpts, s *store.Layout, chartName string) error {
// TODO: Reduce duplicates between api chart and upstream helm opts
cfg := v1alpha1.Chart{
Name: chartName,
Expand Down
22 changes: 2 additions & 20 deletions cmd/hauler/cli/store/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,15 @@ import (
"fmt"
"strings"

"github.com/spf13/cobra"
"oras.land/oras-go/pkg/content"

"github.com/rancherfederal/hauler/internal/flags"
"github.com/rancherfederal/hauler/pkg/cosign"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/store"
)

type CopyOpts struct {
*RootOpts

Username string
Password string
Insecure bool
PlainHTTP bool
}

func (o *CopyOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.StringVarP(&o.Username, "username", "u", "", "Username when copying to an authenticated remote registry")
f.StringVarP(&o.Password, "password", "p", "", "Password when copying to an authenticated remote registry")
f.BoolVar(&o.Insecure, "insecure", false, "Toggle allowing insecure connections when copying to a remote registry")
f.BoolVar(&o.PlainHTTP, "plain-http", false, "Toggle allowing plain http connections when copying to a remote registry")
}

func CopyCmd(ctx context.Context, o *CopyOpts, s *store.Layout, targetRef string) error {
func CopyCmd(ctx context.Context, o *flags.CopyOpts, s *store.Layout, targetRef string) error {
l := log.FromContext(ctx)

components := strings.SplitN(targetRef, "://", 2)
Expand Down
15 changes: 2 additions & 13 deletions cmd/hauler/cli/store/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@ import (
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"

"github.com/rancherfederal/hauler/internal/flags"
"github.com/rancherfederal/hauler/internal/mapper"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/reference"
"github.com/rancherfederal/hauler/pkg/store"
)

type ExtractOpts struct {
*RootOpts
DestinationDir string
}

func (o *ExtractOpts) AddArgs(cmd *cobra.Command) {
f := cmd.Flags()

f.StringVarP(&o.DestinationDir, "output", "o", "", "Directory to save contents to (defaults to current directory)")
}

func ExtractCmd(ctx context.Context, o *ExtractOpts, s *store.Layout, ref string) error {
func ExtractCmd(ctx context.Context, o *flags.ExtractOpts, s *store.Layout, ref string) error {
l := log.FromContext(ctx)

r, err := reference.Parse(ref)
Expand Down
Loading

0 comments on commit 03f48ae

Please sign in to comment.