Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated/fixed hauler directory #354

Merged
merged 10 commits into from
Nov 15, 2024
35 changes: 22 additions & 13 deletions cmd/hauler/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
package cli

import (
"context"
"embed"

"github.com/spf13/cobra"

"hauler.dev/go/hauler/internal/flags"
"hauler.dev/go/hauler/pkg/consts"
"hauler.dev/go/hauler/pkg/cosign"
"hauler.dev/go/hauler/pkg/log"
)

var ro = &flags.CliRootOpts{}

func New() *cobra.Command {
func New(ctx context.Context, binaries embed.FS, ro *flags.CliRootOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "hauler",
Short: "Airgap Swiss Army Knife",
Use: "hauler",
Short: "Airgap Swiss Army Knife",
Example: " View the Docs: https://docs.hauler.dev\n Environment Variables: " + consts.HaulerDir + " | " + consts.HaulerTempDir,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
l := log.FromContext(cmd.Context())
l := log.FromContext(ctx)
l.SetLevel(ro.LogLevel)
l.Debugf("running cli command [%s]", cmd.CommandPath())

// ensure cosign binary is available
if err := cosign.EnsureBinaryExists(ctx, binaries, ro); err != nil {
l.Errorf("cosign binary missing: %v", err)
return err
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

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

// Add subcommands
addLogin(cmd)
addStore(cmd)
addVersion(cmd)
addCompletion(cmd)
addLogin(cmd, ro)
addStore(cmd, ro)
addVersion(cmd, ro)
addCompletion(cmd, ro)

return cmd
}
19 changes: 10 additions & 9 deletions cmd/hauler/cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"os"

"github.com/spf13/cobra"
"hauler.dev/go/hauler/internal/flags"
)

func addCompletion(parent *cobra.Command) {
func addCompletion(parent *cobra.Command, ro *flags.CliRootOpts) {
cmd := &cobra.Command{
Use: "completion",
Short: "Generate auto-completion scripts for various shells",
}

cmd.AddCommand(
addCompletionZsh(),
addCompletionBash(),
addCompletionFish(),
addCompletionPowershell(),
addCompletionZsh(ro),
addCompletionBash(ro),
addCompletionFish(ro),
addCompletionPowershell(ro),
)

parent.AddCommand(cmd)
}

func addCompletionZsh() *cobra.Command {
func addCompletionZsh(ro *flags.CliRootOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "zsh",
Short: "Generates auto-completion scripts for zsh",
Expand Down Expand Up @@ -52,7 +53,7 @@ func addCompletionZsh() *cobra.Command {
return cmd
}

func addCompletionBash() *cobra.Command {
func addCompletionBash(ro *flags.CliRootOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "bash",
Short: "Generates auto-completion scripts for bash",
Expand All @@ -71,7 +72,7 @@ func addCompletionBash() *cobra.Command {
return cmd
}

func addCompletionFish() *cobra.Command {
func addCompletionFish(ro *flags.CliRootOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "fish",
Short: "Generates auto-completion scripts for fish",
Expand All @@ -87,7 +88,7 @@ func addCompletionFish() *cobra.Command {
return cmd
}

func addCompletionPowershell() *cobra.Command {
func addCompletionPowershell(ro *flags.CliRootOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "powershell",
Short: "Generates auto-completion scripts for powershell",
Expand Down
8 changes: 4 additions & 4 deletions cmd/hauler/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"hauler.dev/go/hauler/pkg/cosign"
)

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

cmd := &cobra.Command{
Expand All @@ -39,21 +39,21 @@ func addLogin(parent *cobra.Command) {
return fmt.Errorf("username and password required")
}

return login(ctx, o, arg[0])
return login(ctx, o, arg[0], ro)
},
}
o.AddFlags(cmd)

parent.AddCommand(cmd)
}

func login(ctx context.Context, o *flags.LoginOpts, registry string) error {
func login(ctx context.Context, o *flags.LoginOpts, registry string, ro *flags.CliRootOpts) error {
ropts := content.RegistryOptions{
Username: o.Username,
Password: o.Password,
}

err := cosign.RegistryLogin(ctx, nil, registry, ropts)
err := cosign.RegistryLogin(ctx, nil, registry, ropts, ro)
if err != nil {
return err
}
Expand Down
Loading
Loading