Skip to content

Commit

Permalink
updated/fixed hauler directory (#354)
Browse files Browse the repository at this point in the history
* added env variables for haulerDir/tempDir
* updated hauler directory for the install script
* cleanup/fixes for install script
* updated variables based on feedback
* revert "updated variables based on feedback"
  * reverts commit 54f7a4d
* minor restructure to root flags
* updated logic to include haulerdir flag
* cleaned up/formatted new logic
* more cleanup and formatting
  • Loading branch information
zackbradys authored Nov 15, 2024
1 parent 38c7d1b commit 1b77295
Show file tree
Hide file tree
Showing 26 changed files with 256 additions and 184 deletions.
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

0 comments on commit 1b77295

Please sign in to comment.