Skip to content

Commit

Permalink
fix: migrate all flags, use exported vars
Browse files Browse the repository at this point in the history
  • Loading branch information
wcrum committed Aug 24, 2024
1 parent 9bbbc5b commit b2035d7
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 50 deletions.
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
18 changes: 3 additions & 15 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 @@ -60,7 +48,7 @@ hauler login reg.example.com -u bob -p haulin`,
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
26 changes: 13 additions & 13 deletions cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/rancherfederal/hauler/internal/flags"
)

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

func addStore(parent *cobra.Command) {
cmd := &cobra.Command{
Expand Down Expand Up @@ -40,7 +40,7 @@ func addStore(parent *cobra.Command) {
}

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

cmd := &cobra.Command{
Use: "extract",
Expand All @@ -64,7 +64,7 @@ func addStoreExtract() *cobra.Command {
}

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

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

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

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

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

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

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

cmd := &cobra.Command{
Use: "save",
Expand All @@ -196,7 +196,7 @@ func addStoreSave() *cobra.Command {
}

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

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

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

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

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

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

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

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

cmd := &cobra.Command{
Use: "image",
Expand All @@ -315,8 +315,8 @@ func addStoreAddImage() *cobra.Command {

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

cmd := &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/hauler/cli/store/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func SyncCmd(ctx context.Context, o *flags.SyncOpts, s *store.Layout) error {
if err != nil {
return err
}
err = ExtractCmd(ctx, &flags.ExtractOpts{RootOpts: o.RootOpts}, s, fmt.Sprintf("hauler/%s-manifest.yaml:%s", parts[0], tag))
err = ExtractCmd(ctx, &flags.ExtractOpts{StoreRootOpts: o.StoreRootOpts}, s, fmt.Sprintf("hauler/%s-manifest.yaml:%s", parts[0], tag))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/flags/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type AddImageOpts struct {
*RootOpts
*StoreRootOpts
Name string
Key string
Platform string
Expand All @@ -19,7 +19,7 @@ func (o *AddImageOpts) AddFlags(cmd *cobra.Command) {
}

type AddFileOpts struct {
*RootOpts
*StoreRootOpts
Name string
}

Expand All @@ -29,7 +29,7 @@ func (o *AddFileOpts) AddFlags(cmd *cobra.Command) {
}

type AddChartOpts struct {
*RootOpts
*StoreRootOpts

ChartOpts *action.ChartPathOptions
}
Expand Down
5 changes: 5 additions & 0 deletions internal/flags/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package flags

type CliRootOpts struct {
LogLevel string
}
2 changes: 1 addition & 1 deletion internal/flags/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type CopyOpts struct {
*RootOpts
*StoreRootOpts

Username string
Password string
Expand Down
2 changes: 1 addition & 1 deletion internal/flags/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type ExtractOpts struct {
*RootOpts
*StoreRootOpts
DestinationDir string
}

Expand Down
2 changes: 1 addition & 1 deletion internal/flags/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type InfoOpts struct {
*RootOpts
*StoreRootOpts

OutputFormat string
TypeFilter string
Expand Down
2 changes: 1 addition & 1 deletion internal/flags/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type LoadOpts struct {
*RootOpts
*StoreRootOpts
TempOverride string
}

Expand Down
16 changes: 16 additions & 0 deletions internal/flags/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flags

import "github.com/spf13/cobra"

type LoginOpts struct {
Username string
Password string
PasswordStdin bool
}

func (o *LoginOpts) 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)")
}
2 changes: 1 addition & 1 deletion internal/flags/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type SaveOpts struct {
*RootOpts
*StoreRootOpts
FileName string
}

Expand Down
4 changes: 2 additions & 2 deletions internal/flags/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type ServeRegistryOpts struct {
*RootOpts
*StoreRootOpts

Port int
RootDir string
Expand Down Expand Up @@ -63,7 +63,7 @@ func (o *ServeRegistryOpts) DefaultRegistryConfig() *configuration.Configuration
}

type ServeFilesOpts struct {
*RootOpts
*StoreRootOpts

Port int
Timeout int
Expand Down
6 changes: 3 additions & 3 deletions internal/flags/root.go → internal/flags/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
"github.com/spf13/cobra"
)

type RootOpts struct {
type StoreRootOpts struct {
StoreDir string
CacheDir string
}

func (o *RootOpts) AddArgs(cmd *cobra.Command) {
func (o *StoreRootOpts) AddArgs(cmd *cobra.Command) {
pf := cmd.PersistentFlags()
pf.StringVarP(&o.StoreDir, "store", "s", consts.DefaultStoreName, "Location to create store at")
pf.StringVar(&o.CacheDir, "cache", "", "(deprecated flag and currently not used)")
}

func (o *RootOpts) Store(ctx context.Context) (*store.Layout, error) {
func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) {
l := log.FromContext(ctx)
dir := o.StoreDir

Expand Down
2 changes: 1 addition & 1 deletion internal/flags/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import "github.com/spf13/cobra"

type SyncOpts struct {
*RootOpts
*StoreRootOpts
ContentFiles []string
Key string
Products []string
Expand Down

0 comments on commit b2035d7

Please sign in to comment.