Skip to content

Commit

Permalink
update internal, package, prepare to use lang/english
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewg-xyz committed Feb 17, 2023
1 parent 90fa1ac commit 4a0d263
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 70 deletions.
34 changes: 16 additions & 18 deletions src/cmd/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"

"github.com/alecthomas/jsonschema"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/internal/agent"
"github.com/defenseunicorns/zarf/src/internal/api"
"github.com/defenseunicorns/zarf/src/internal/cluster"
Expand All @@ -25,23 +26,21 @@ var internalCmd = &cobra.Command{
Use: "internal",
Aliases: []string{"dev"},
Hidden: true,
Short: "Internal tools used by zarf",
Short: lang.CmdInternalShort,
}

var agentCmd = &cobra.Command{
Use: "agent",
Short: "Runs the zarf agent",
Long: "NOTE: This command is a hidden command and generally shouldn't be run by a human.\n" +
"This command starts up a http webhook that Zarf deployments use to mutate pods to conform " +
"with the Zarf container registry and Gitea server URLs.",
Short: lang.CmdInternalAgentShort,
Long: lang.CmdInternalAgentLong,
Run: func(cmd *cobra.Command, args []string) {
agent.StartWebhook()
},
}

var generateCLIDocs = &cobra.Command{
Use: "generate-cli-docs",
Short: "Creates auto-generated markdown of all the commands for the CLI",
Short: lang.CmdInternalGenerateCliDocsShort,
Run: func(cmd *cobra.Command, args []string) {
// Don't include the datestamp in the output
rootCmd.DisableAutoGenTag = true
Expand All @@ -55,68 +54,67 @@ var generateCLIDocs = &cobra.Command{
if err := doc.GenMarkdownTree(rootCmd, "./docs/4-user-guide/1-the-zarf-cli/100-cli-commands"); err != nil {
message.Fatalf("Unable to generate the CLI documentation: %s", err.Error())
} else {
message.SuccessF("Successfully created the CLI documentation")
message.SuccessF(lang.CmdInternalGenerateCliDocsSuccess)
}
},
}

var configSchemaCmd = &cobra.Command{
Use: "config-schema",
Aliases: []string{"c"},
Short: "Generates a JSON schema for the zarf.yaml configuration",
Short: lang.CmdInternalConfigSchemaShort,
Run: func(cmd *cobra.Command, args []string) {
schema := jsonschema.Reflect(&types.ZarfPackage{})
output, err := json.MarshalIndent(schema, "", " ")
if err != nil {
message.Fatal(err, "Unable to generate the zarf config schema")
message.Fatal(err, lang.CmdInternalConfigSchemaErr)
}
fmt.Print(string(output) + "\n")
},
}

var apiSchemaCmd = &cobra.Command{
Use: "api-schema",
Short: "Generates a JSON schema from the API types",
Short: lang.CmdInternalApiSchemaShort,
Run: func(cmd *cobra.Command, args []string) {
schema := jsonschema.Reflect(&types.RestAPI{})
output, err := json.MarshalIndent(schema, "", " ")
if err != nil {
message.Fatal(err, "Unable to generate the zarf api schema")
message.Fatal(err, lang.CmdInternalApiSchemaGenerateErr)
}
fmt.Print(string(output) + "\n")
},
}

var createReadOnlyGiteaUser = &cobra.Command{
Use: "create-read-only-gitea-user",
Short: "Creates a read-only user in Gitea",
Long: "Creates a read-only user in Gitea by using the Gitea API. " +
"This is called internally by the supported Gitea package component.",
Short: lang.CmdInternalCreateReadOnlyGiteaUserShort,
Long: lang.CmdInternalCreateReadOnlyGiteaUserLong,
Run: func(cmd *cobra.Command, args []string) {
// Load the state so we can get the credentials for the admin git user
state, err := cluster.NewClusterOrDie().LoadZarfState()
if err != nil {
message.Error(err, "Unable to load the Zarf state")
message.Error(err, lang.CmdInternalCreateReadOnlyGiteaUserErr)
}

// Create the non-admin user
if err = git.New(state.GitServer).CreateReadOnlyUser(); err != nil {
message.Error(err, "Unable to create a read-only user in the Gitea service.")
message.Error(err, lang.CmdInternalCreateReadOnlyGiteaUserErr)
}
},
}

var uiCmd = &cobra.Command{
Use: "ui",
Short: "Launch the experimental Zarf UI",
Short: lang.CmdInternalUiShort,
Run: func(cmd *cobra.Command, args []string) {
api.LaunchAPIServer()
},
}

var isValidHostname = &cobra.Command{
Use: "is-valid-hostname",
Short: "Checks if the current machine's hostname is RFC1123 compliant",
Short: lang.CmdInternalIsValidHostnameShort,
Run: func(cmd *cobra.Command, args []string) {
if valid := utils.IsValidHostName(); !valid {
hostname, _ := os.Hostname()
Expand Down
65 changes: 31 additions & 34 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"strings"

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"
Expand All @@ -30,17 +31,15 @@ var outputInspectSBOM string
var packageCmd = &cobra.Command{
Use: "package",
Aliases: []string{"p"},
Short: "Zarf package commands for creating, deploying, and inspecting packages",
Short: lang.CmdPackageShort,
}

var packageCreateCmd = &cobra.Command{
Use: "create [DIRECTORY]",
Aliases: []string{"c"},
Args: cobra.MaximumNArgs(1),
Short: "Use to create a Zarf package from a given directory or the current directory",
Long: "Builds an archive of resources and dependencies defined by the 'zarf.yaml' in the active directory.\n" +
"Private registries and repositories are accessed via credentials in your local '~/.docker/config.json', " +
"'~/.git-credentials' and '~/.netrc'.\n",
Short: lang.CmdPackageCreateShort,
Long: lang.CmdPackageCreateLong,
Run: func(cmd *cobra.Command, args []string) {

var baseDir string
Expand Down Expand Up @@ -74,8 +73,8 @@ var packageCreateCmd = &cobra.Command{
var packageDeployCmd = &cobra.Command{
Use: "deploy [PACKAGE]",
Aliases: []string{"d"},
Short: "Use to deploy a Zarf package from a local file or URL (runs offline)",
Long: "Uses current kubecontext to deploy the packaged tarball onto a k8s cluster.",
Short: lang.CmdPackageDeployShort,
Long: lang.CmdPackageDeployLong,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.DeployOpts.PackagePath = choosePackage(args)
Expand All @@ -98,11 +97,9 @@ var packageDeployCmd = &cobra.Command{
var packageInspectCmd = &cobra.Command{
Use: "inspect [PACKAGE]",
Aliases: []string{"i"},
Short: "Lists the payload of a Zarf package (runs offline)",
Long: "Lists the payload of a compiled package file (runs offline)\n" +
"Unpacks the package tarball into a temp directory and displays the " +
"contents of the archive.",
Args: cobra.MaximumNArgs(1),
Short: lang.CmdPackageInspectShort,
Long: lang.CmdPackageInspectLong,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.DeployOpts.PackagePath = choosePackage(args)

Expand All @@ -120,12 +117,12 @@ var packageInspectCmd = &cobra.Command{
var packageListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "List out all of the packages that have been deployed to the cluster",
Short: lang.CmdPackageListShort,
Run: func(cmd *cobra.Command, args []string) {
// Get all the deployed packages
deployedZarfPackages, err := cluster.NewClusterOrDie().GetDeployedZarfPackages()
if err != nil {
message.Fatalf(err, "Unable to get the packages deployed to the cluster")
message.Fatalf(err, lang.CmdPackageListNoPackageWarn)
}

// Populate a pterm table of all the deployed packages
Expand Down Expand Up @@ -155,15 +152,15 @@ var packageRemoveCmd = &cobra.Command{
Use: "remove {PACKAGE_NAME|PACKAGE_FILE}",
Aliases: []string{"u"},
Args: cobra.ExactArgs(1),
Short: "Use to remove a Zarf package that has been deployed already",
Short: lang.CmdPackageRemoveShort,
Run: func(cmd *cobra.Command, args []string) {
pkgName := args[0]

// If the user input is a path to a package, extract the name from the package
isTarball := regexp.MustCompile(`.*zarf-package-.*\.tar\.zst$`).MatchString
if isTarball(pkgName) {
if utils.InvalidPath(pkgName) {
message.Fatalf(nil, "Invalid tarball path provided")
message.Fatalf(nil, lang.CmdPackageRemoveTarballErr)
}

tempPath, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
Expand All @@ -173,13 +170,13 @@ var packageRemoveCmd = &cobra.Command{
defer os.RemoveAll(tempPath)

if err := archiver.Extract(pkgName, config.ZarfYAML, tempPath); err != nil {
message.Fatalf(err, "Unable to extract the package contents")
message.Fatalf(err, lang.CmdPackageRemoveExtractErr)
}

var pkgConfig types.ZarfPackage
configPath := filepath.Join(tempPath, config.ZarfYAML)
if err := utils.ReadYaml(configPath, &pkgConfig); err != nil {
message.Fatalf(err, "Unable to read zarf.yaml")
message.Fatalf(err, lang.CmdPackageRemoveReadZarfErr)
}

pkgName = pkgConfig.Metadata.Name
Expand Down Expand Up @@ -240,7 +237,7 @@ func bindCreateFlags() {
createFlags := packageCreateCmd.Flags()

// Always require confirm flag (no viper)
createFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, "Confirm package creation without prompting")
createFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, lang.CmdPackageCreateFlagConfirm)

v.SetDefault(V_PKG_CREATE_SET, map[string]string{})
v.SetDefault(V_PKG_CREATE_OUTPUT_DIR, "")
Expand All @@ -249,40 +246,40 @@ func bindCreateFlags() {
v.SetDefault(V_PKG_CREATE_SKIP_SBOM, false)
v.SetDefault(V_PKG_CREATE_MAX_PACKAGE_SIZE, 0)

createFlags.StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(V_PKG_CREATE_SET), "Specify package variables to set on the command line (KEY=value)")
createFlags.StringVarP(&pkgConfig.CreateOpts.OutputDirectory, "output-directory", "o", v.GetString(V_PKG_CREATE_OUTPUT_DIR), "Specify the output directory for the created Zarf package")
createFlags.BoolVarP(&pkgConfig.CreateOpts.ViewSBOM, "sbom", "s", v.GetBool(V_PKG_CREATE_SBOM), "View SBOM contents after creating the package")
createFlags.StringVar(&pkgConfig.CreateOpts.SBOMOutputDir, "sbom-out", v.GetString(V_PKG_CREATE_SBOM_OUTPUT), "Specify an output directory for the SBOMs from the created Zarf package")
createFlags.BoolVar(&pkgConfig.CreateOpts.SkipSBOM, "skip-sbom", v.GetBool(V_PKG_CREATE_SKIP_SBOM), "Skip generating SBOM for this package")
createFlags.IntVarP(&pkgConfig.CreateOpts.MaxPackageSizeMB, "max-package-size", "m", v.GetInt(V_PKG_CREATE_MAX_PACKAGE_SIZE), "Specify the maximum size of the package in megabytes, packages larger than this will be split into multiple parts. Use 0 to disable splitting.")
createFlags.StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(V_PKG_CREATE_SET), lang.CmdPackageCreateFlagSet)
createFlags.StringVarP(&pkgConfig.CreateOpts.OutputDirectory, "output-directory", "o", v.GetString(V_PKG_CREATE_OUTPUT_DIR), lang.CmdPackageCreateFlagOutputDirectory)
createFlags.BoolVarP(&pkgConfig.CreateOpts.ViewSBOM, "sbom", "s", v.GetBool(V_PKG_CREATE_SBOM), lang.CmdPackageCreateFlagSbom)
createFlags.StringVar(&pkgConfig.CreateOpts.SBOMOutputDir, "sbom-out", v.GetString(V_PKG_CREATE_SBOM_OUTPUT), lang.CmdPackageCreateFlagSbomOut)
createFlags.BoolVar(&pkgConfig.CreateOpts.SkipSBOM, "skip-sbom", v.GetBool(V_PKG_CREATE_SKIP_SBOM), lang.CmdPackageCreateFlagSkipSbom)
createFlags.IntVarP(&pkgConfig.CreateOpts.MaxPackageSizeMB, "max-package-size", "m", v.GetInt(V_PKG_CREATE_MAX_PACKAGE_SIZE), lang.CmdPackageCreateFlagMaxPackageSize)
}

func bindDeployFlags() {
deployFlags := packageDeployCmd.Flags()

// Always require confirm flag (no viper)
deployFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, "Confirm package deployment without prompting")
deployFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, lang.CmdPackageDeployFlagConfirm)

v.SetDefault(V_PKG_DEPLOY_SET, map[string]string{})
v.SetDefault(V_PKG_DEPLOY_COMPONENTS, "")
v.SetDefault(V_PKG_DEPLOY_SHASUM, "")
v.SetDefault(V_PKG_DEPLOY_SGET, "")

deployFlags.StringToStringVar(&pkgConfig.DeployOpts.SetVariables, "set", v.GetStringMapString(V_PKG_DEPLOY_SET), "Specify deployment variables to set on the command line (KEY=value)")
deployFlags.StringVar(&pkgConfig.DeployOpts.Components, "components", v.GetString(V_PKG_DEPLOY_COMPONENTS), "Comma-separated list of components to install. Adding this flag will skip the init prompts for which components to install")
deployFlags.StringVar(&pkgConfig.DeployOpts.Shasum, "shasum", v.GetString(V_PKG_DEPLOY_SHASUM), "Shasum of the package to deploy. Required if deploying a remote package and \"--insecure\" is not provided")
deployFlags.StringVar(&pkgConfig.DeployOpts.SGetKeyPath, "sget", v.GetString(V_PKG_DEPLOY_SGET), "Path to public sget key file for remote packages signed via cosign")
deployFlags.StringToStringVar(&pkgConfig.DeployOpts.SetVariables, "set", v.GetStringMapString(V_PKG_DEPLOY_SET), lang.CmdPackageDeployFlagSet)
deployFlags.StringVar(&pkgConfig.DeployOpts.Components, "components", v.GetString(V_PKG_DEPLOY_COMPONENTS), lang.CmdPackageDeployFlagComponents)
deployFlags.StringVar(&pkgConfig.DeployOpts.Shasum, "shasum", v.GetString(V_PKG_DEPLOY_SHASUM), lang.CmdPackageDeployFlagShasum)
deployFlags.StringVar(&pkgConfig.DeployOpts.SGetKeyPath, "sget", v.GetString(V_PKG_DEPLOY_SGET), lang.CmdPackageDeployFlagSget)
}

func bindInspectFlags() {
inspectFlags := packageInspectCmd.Flags()
inspectFlags.BoolVarP(&includeInspectSBOM, "sbom", "s", false, "View SBOM contents while inspecting the package")
inspectFlags.StringVar(&outputInspectSBOM, "sbom-out", "", "Specify an output directory for the SBOMs from the inspected Zarf package")
inspectFlags.BoolVarP(&includeInspectSBOM, "sbom", "s", false, lang.CmdPackageInspectFlagSbom)
inspectFlags.StringVar(&outputInspectSBOM, "sbom-out", "", lang.CmdPackageInspectFlagSbomOut)
}

func bindRemoveFlags() {
removeFlags := packageRemoveCmd.Flags()
removeFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, "REQUIRED. Confirm the removal action to prevent accidental deletions")
removeFlags.StringVar(&pkgConfig.DeployOpts.Components, "components", v.GetString(V_PKG_DEPLOY_COMPONENTS), "Comma-separated list of components to uninstall")
removeFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, lang.CmdPackageRemoveFlagConfirm)
removeFlags.StringVar(&pkgConfig.DeployOpts.Components, "components", v.GetString(V_PKG_DEPLOY_COMPONENTS), lang.CmdPackageRemoveFlagComponents)
_ = packageRemoveCmd.MarkFlagRequired("confirm")
}
32 changes: 14 additions & 18 deletions src/cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/internal/packager/git"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager"
Expand All @@ -23,15 +24,14 @@ var repoHelmChartPath string
var prepareCmd = &cobra.Command{
Use: "prepare",
Aliases: []string{"prep"},
Short: "Tools to help prepare assets for packaging",
Short: lang.CmdPrepareShort,
}

var prepareTransformGitLinks = &cobra.Command{
Use: "patch-git [HOST] [FILE]",
Aliases: []string{"p"},
Short: "Converts all .git URLs to the specified Zarf HOST and with the Zarf URL pattern in a given FILE. NOTE: \n" +
"This should only be used for manifests that are not mutated by the Zarf Agent Mutating Webhook.",
Args: cobra.ExactArgs(2),
Short: lang.CmdPreparePatchGitShort,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
host, fileName := args[0], args[1]

Expand Down Expand Up @@ -61,7 +61,7 @@ var prepareTransformGitLinks = &cobra.Command{
// Overwrite the file
err = os.WriteFile(fileName, []byte(processedText), 0640)
if err != nil {
message.Fatal(err, "Unable to write the changes back to the file")
message.Fatal(err, lang.CmdPreparePatchGitFileWriteErr)
}
}

Expand All @@ -71,13 +71,13 @@ var prepareTransformGitLinks = &cobra.Command{
var prepareComputeFileSha256sum = &cobra.Command{
Use: "sha256sum [FILE|URL]",
Aliases: []string{"s"},
Short: "Generate a SHA256SUM for the given file",
Short: lang.CmdPrepareSha256sumShort,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fileName := args[0]
hash, err := utils.GetCryptoHash(fileName, crypto.SHA256)
if err != nil {
message.Fatal(err, "Unable to compute the hash")
message.Fatal(err, lang.CmdPrepareSha256sumHashErr)
} else {
fmt.Println(hash)
}
Expand All @@ -88,9 +88,8 @@ var prepareFindImages = &cobra.Command{
Use: "find-images [PACKAGE]",
Aliases: []string{"f"},
Args: cobra.MaximumNArgs(1),
Short: "Evaluates components in a zarf file to identify images specified in their helm charts and manifests",
Long: "Evaluates components in a zarf file to identify images specified in their helm charts and manifests.\n\n" +
"Components that have repos that host helm charts can be processed by providing the --repo-chart-path.",
Short: lang.CmdPrepareFindImagesShort,
Long: lang.CmdPrepareFindImagesLong,
Run: func(cmd *cobra.Command, args []string) {
var baseDir string

Expand Down Expand Up @@ -118,11 +117,8 @@ var prepareGenerateConfigFile = &cobra.Command{
Use: "generate-config [FILENAME]",
Aliases: []string{"gc"},
Args: cobra.MaximumNArgs(1),
Short: "Generates a config file for Zarf",
Long: "Generates a Zarf config file for controlling how the Zarf CLI operates. Optionally accepts a filename to write the config to.\n\n" +
"The extension will determine the format of the config file, e.g. env-1.yaml, env-2.json, env-3.toml etc. \n" +
"Accepted extensions are json, toml, yaml.\n\n" +
"NOTE: This file must not already exist. If no filename is provided, the config will be written to the current working directory as zarf-config.toml.",
Short: lang.CmdPrepareGenerateConfigShort,
Long: lang.CmdPrepareGenerateConfigLong,
Run: func(cmd *cobra.Command, args []string) {
fileName := "zarf-config.toml"

Expand All @@ -148,9 +144,9 @@ func init() {

v.SetDefault(V_PKG_CREATE_SET, map[string]string{})

prepareFindImages.Flags().StringVarP(&repoHelmChartPath, "repo-chart-path", "p", "", `If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"`)
prepareFindImages.Flags().StringVarP(&repoHelmChartPath, "repo-chart-path", "p", "", lang.CmdPrepareFlagRepoChartPath)
// use the package create config for this and reset it here to avoid overwriting the config.CreateOptions.SetVariables
prepareFindImages.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(V_PKG_CREATE_SET), "Specify package variables to set on the command line (KEY=value). Note, if using a config file, this will be set by [package.create.set].")
prepareFindImages.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(V_PKG_CREATE_SET), lang.CmdPrepareFlagSet)

prepareTransformGitLinks.Flags().StringVar(&pkgConfig.InitOpts.GitServer.PushUsername, "git-account", config.ZarfGitPushUser, "User or organization name for the git account that the repos are created under.")
prepareTransformGitLinks.Flags().StringVar(&pkgConfig.InitOpts.GitServer.PushUsername, "git-account", config.ZarfGitPushUser, lang.CmdPrepareFlagGitAccount)
}
Loading

0 comments on commit 4a0d263

Please sign in to comment.