Skip to content

Commit

Permalink
ports Zarf bundle code
Browse files Browse the repository at this point in the history
Co-authored-by: razzle <[email protected]>
  • Loading branch information
UncleGedd and Noxsios committed Aug 2, 2023
1 parent f4b38e6 commit 1f1860b
Show file tree
Hide file tree
Showing 28 changed files with 5,395 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2023-Present The UDS Authors

ARCH ?= amd64
BUILD_ARGS := -s -w # remove debugging info

build-cli-linux-amd:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/uds main.go

build-cli-linux-arm:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/uds-arm main.go

build-cli-mac-intel:
GOOS=darwin GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/uds-mac-intel main.go

build-cli-mac-apple:
GOOS=darwin GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/uds-mac-apple main.go

clean:
rm -rf build
424 changes: 424 additions & 0 deletions go.mod

Large diffs are not rendered by default.

2,267 changes: 2,267 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

package main

import "github.com/defenseunicorns/uds-cli/src/cmd"

func main() {
cmd.Execute()
}
178 changes: 178 additions & 0 deletions src/cmd/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

// Package cmd contains the CLI commands for Zarf.
package cmd

import (
"fmt"

"github.com/defenseunicorns/uds-cli/src/config/lang"
"github.com/defenseunicorns/uds-cli/src/pkg/bundler"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/oci"

"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/spf13/cobra"
)

var bundleCmd = &cobra.Command{
Use: "bundle",
Aliases: []string{"b"},
Short: lang.CmdBundleShort,
}

var bundleCreateCmd = &cobra.Command{
Use: "create [DIRECTORY]",
Aliases: []string{"c"},
Args: cobra.ExactArgs(1),
Short: lang.CmdBundleCreateShort,
PreRun: func(cmd *cobra.Command, args []string) {
if !utils.IsDir(args[0]) {
message.Fatalf(nil, "first argument (%q) must be a valid path to a directory", args[0])
}
},
Run: func(cmd *cobra.Command, args []string) {
bundleCfg.CreateOpts.SourceDirectory = args[0]

bundleCfg.CreateOpts.SetVariables = bundler.MergeVariables(v.GetStringMapString(V_PKG_CREATE_SET), bundleCfg.CreateOpts.SetVariables)

bndlClient := bundler.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Create(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to create bundle: %s", err.Error())
}
},
}

var bundleDeployCmd = &cobra.Command{
Use: "deploy [BUNDLE_TARBALL|OCI_REF]",
Aliases: []string{"d"},
Short: lang.CmdBundleDeployShort,
Args: cobra.ExactArgs(1),
PreRun: firstArgIsEitherOCIorTarball,
Run: func(cmd *cobra.Command, args []string) {
bundleCfg.DeployOpts.Source = args[0]

bundleCfg.DeployOpts.SetVariables = bundler.MergeVariables(v.GetStringMapString(V_PKG_DEPLOY_SET), bundleCfg.DeployOpts.SetVariables)

bndlClient := bundler.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Deploy(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to deploy bundle: %s", err.Error())
}
},
}

var bundleInspectCmd = &cobra.Command{
Use: "inspect [BUNDLE_TARBALL|OCI_REF]",
Aliases: []string{"i"},
Short: lang.CmdBundleInspectShort,
Args: cobra.ExactArgs(1),
PreRun: firstArgIsEitherOCIorTarball,
Run: func(cmd *cobra.Command, args []string) {
bundleCfg.InspectOpts.Source = args[0]

bndlClient := bundler.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Inspect(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to inspect bundle: %s", err.Error())
}
},
}

var bundleRemoveCmd = &cobra.Command{
Use: "remove [BUNDLE_TARBALL|OCI_REF]",
Aliases: []string{"r"},
Args: cobra.ExactArgs(1),
Short: lang.CmdBundleRemoveShort,
PreRun: firstArgIsEitherOCIorTarball,
Run: func(cmd *cobra.Command, args []string) {
bundleCfg.RemoveOpts.Source = args[0]

bndlClient := bundler.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Remove(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to remove bundle: %s", err.Error())
}
},
}

var bundlePullCmd = &cobra.Command{
Use: "pull [OCI_REF]",
Aliases: []string{"p"},
Short: lang.CmdBundlePullShort,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
if err := oci.ValidateReference(args[0]); err != nil {
message.Fatalf(err, "First agument (%q) must be a valid OCI URL: %s", args[0], err.Error())
}
},
Run: func(cmd *cobra.Command, args []string) {
bundleCfg.PullOpts.Source = args[0]

bndlClient := bundler.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Pull(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to pull bundle: %s", err.Error())
}
},
}

func firstArgIsEitherOCIorTarball(_ *cobra.Command, args []string) {
var errString string
var err error
if bundler.IsValidTarballPath(args[0]) {
return
}
if !utils.IsOCIURL(args[0]) && !bundler.IsValidTarballPath(args[0]) {
errString = fmt.Sprintf("First argument (%q) must either be a valid OCI URL or a valid path to a bundle tarball", args[0])
} else {
err = oci.ValidateReference(args[0])
}
if errString != "" {
message.Fatalf(err, "Failed to validate first argument: %s", errString)
}
}

func init() {
initViper()

rootCmd.AddCommand(bundleCmd)
v.SetDefault(V_BNDL_OCI_CONCURRENCY, 3)
bundleCmd.PersistentFlags().IntVar(&config.CommonOptions.OCIConcurrency, "oci-concurrency", v.GetInt(V_BNDL_OCI_CONCURRENCY), lang.CmdBundleFlagConcurrency)

bundleCmd.AddCommand(bundleCreateCmd)
bundleCreateCmd.Flags().BoolVarP(&config.CommonOptions.Confirm, "confirm", "c", false, lang.CmdBundleRemoveFlagConfirm)
bundleCreateCmd.Flags().StringVarP(&bundleCfg.CreateOpts.Output, "output", "o", v.GetString(V_BNDL_CREATE_OUTPUT), lang.CmdBundleCreateFlagOutput)
bundleCreateCmd.Flags().StringVarP(&bundleCfg.CreateOpts.SigningKeyPath, "signing-key", "k", v.GetString(V_BNDL_CREATE_SIGNING_KEY), lang.CmdBundleCreateFlagSigningKey)
bundleCreateCmd.Flags().StringVarP(&bundleCfg.CreateOpts.SigningKeyPassword, "signing-key-password", "p", v.GetString(V_BNDL_CREATE_SIGNING_KEY_PASSWORD), lang.CmdBundleCreateFlagSigningKeyPassword)
bundleCreateCmd.Flags().StringToStringVarP(&bundleCfg.CreateOpts.SetVariables, "set", "s", v.GetStringMapString(V_BNDL_CREATE_SET), lang.CmdBundleCreateFlagSet)

bundleCmd.AddCommand(bundleDeployCmd)
bundleDeployCmd.Flags().StringToStringVarP(&bundleCfg.DeployOpts.SetVariables, "set", "s", v.GetStringMapString(V_BNDL_DEPLOY_SET), lang.CmdBundleDeployFlagSet)
bundleDeployCmd.Flags().BoolVarP(&config.CommonOptions.Confirm, "confirm", "c", false, lang.CmdBundleDeployFlagConfirm)

bundleCmd.AddCommand(bundleInspectCmd)
bundleInspectCmd.Flags().StringVarP(&bundleCfg.InspectOpts.PublicKeyPath, "key", "k", v.GetString(V_BNDL_INSPECT_KEY), lang.CmdBundleInspectFlagKey)

bundleCmd.AddCommand(bundleRemoveCmd)
// confirm does not use the Viper config
bundleRemoveCmd.Flags().BoolVarP(&config.CommonOptions.Confirm, "confirm", "c", false, lang.CmdBundleRemoveFlagConfirm)
_ = bundleRemoveCmd.MarkFlagRequired("confirm")

bundleCmd.AddCommand(bundlePullCmd)
bundlePullCmd.Flags().StringVarP(&bundleCfg.PullOpts.OutputDirectory, "output", "o", v.GetString(V_BNDL_PULL_OUTPUT), lang.CmdBundlePullFlagOutput)
bundlePullCmd.Flags().StringVarP(&bundleCfg.PullOpts.PublicKeyPath, "key", "k", v.GetString(V_BNDL_PULL_KEY), lang.CmdBundlePullFlagKey)
}
119 changes: 119 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

package cmd

import (
"fmt"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/cmd/tools"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

var (
logLevel string

// Default global config for the bundler
bundleCfg = types.BundlerConfig{}

// Viper instance used by the cmd package
v *viper.Viper
)

var rootCmd = &cobra.Command{
Use: "uds COMMAND",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Skip for vendor-only commands
if tools.CheckVendorOnlyFromPath(cmd) {
return
}

exec.ExitOnInterrupt()

// Don't add the logo to the help command
if cmd.Parent() == nil {
config.SkipLogFile = true
}
cliSetup()
},
Short: lang.RootCmdShort,
Long: lang.RootCmdLong,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
_, _ = fmt.Fprintln(os.Stderr)
cmd.Help()
},
}

// Execute is the entrypoint for the CLI.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

// RootCmd returns the root command.
func RootCmd() *cobra.Command {
return rootCmd
}

func init() {
// Add the tools commands
tools.Include(rootCmd)

// Skip for vendor-only commands
if tools.CheckVendorOnlyFromArgs() {
return
}

initViper()

v.SetDefault(V_LOG_LEVEL, "info")
v.SetDefault(V_ARCHITECTURE, "")
v.SetDefault(V_NO_LOG_FILE, false)
v.SetDefault(V_NO_PROGRESS, false)
v.SetDefault(V_INSECURE, false)
v.SetDefault(V_ZARF_CACHE, config.ZarfDefaultCachePath)
v.SetDefault(V_TMP_DIR, "")

rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", v.GetString(V_LOG_LEVEL), lang.RootCmdFlagLogLevel)
rootCmd.PersistentFlags().StringVarP(&config.CLIArch, "architecture", "a", v.GetString(V_ARCHITECTURE), lang.RootCmdFlagArch)
rootCmd.PersistentFlags().BoolVar(&config.SkipLogFile, "no-log-file", v.GetBool(V_NO_LOG_FILE), lang.RootCmdFlagSkipLogFile)
rootCmd.PersistentFlags().BoolVar(&message.NoProgress, "no-progress", v.GetBool(V_NO_PROGRESS), lang.RootCmdFlagNoProgress)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.CachePath, "zarf-cache", v.GetString(V_ZARF_CACHE), lang.RootCmdFlagCachePath)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.TempDirectory, "tmpdir", v.GetString(V_TMP_DIR), lang.RootCmdFlagTempDir)
rootCmd.PersistentFlags().BoolVar(&config.CommonOptions.Insecure, "insecure", v.GetBool(V_INSECURE), lang.RootCmdFlagInsecure)
}

func cliSetup() {
match := map[string]message.LogLevel{
"warn": message.WarnLevel,
"info": message.InfoLevel,
"debug": message.DebugLevel,
"trace": message.TraceLevel,
}

// No log level set, so use the default
if logLevel != "" {
if lvl, ok := match[logLevel]; ok {
message.SetLogLevel(lvl)
message.Debug("Log level set to " + logLevel)
} else {
message.Warn(lang.RootCmdErrInvalidLogLevel)
}
}

// Disable progress bars for CI envs
if os.Getenv("CI") == "true" {
message.Debug("CI environment detected, disabling progress bars")
message.NoProgress = true
}

if !config.SkipLogFile {
message.UseLogFile()
}
}
31 changes: 31 additions & 0 deletions src/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The UDS Authors

// Package cmd contains the CLI commands for UDS.
package cmd

import (
"fmt"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v"},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
config.SkipLogFile = true
cliSetup()
},
Short: lang.CmdVersionShort,
Long: lang.CmdVersionLong,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(config.CLIVersion)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
Loading

0 comments on commit 1f1860b

Please sign in to comment.