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

refactor!: de-zarfify maru-runner #73

Merged
merged 28 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ee56e77
chore: de-zarfify maru-runner
Racer159 May 8, 2024
d54329c
fix some linting
Racer159 May 10, 2024
fd48372
fix env var handling
Racer159 May 10, 2024
be0cace
fix variable loading
Racer159 May 10, 2024
a266924
remove files and fix list all
Racer159 May 10, 2024
3f1c8c3
remove zarf completely
Racer159 May 14, 2024
98440da
fix lint and little errors
Racer159 May 14, 2024
9ac65de
fix some nice-to-haves
Racer159 May 14, 2024
b35aca3
switch variables to internal
Racer159 May 17, 2024
e11cb0a
switch variables to internal
Racer159 May 17, 2024
a2e2a68
unit tests target
ericwyles May 17, 2024
5203fde
remove zarf specific things
Racer159 May 17, 2024
b0a70e8
remove text template
Racer159 May 17, 2024
abef76b
switch variables to have generics
Racer159 May 17, 2024
3fcc592
resolve lint and Makefile *isms
Racer159 May 17, 2024
44718d3
segment runaction logic out of runner
Racer159 May 17, 2024
a34b491
fix actions test
Racer159 May 17, 2024
d2e5df0
add cmd mutation check in case they are already set
Racer159 May 20, 2024
c831651
fix schema
Racer159 May 20, 2024
5a30461
test schema workflow
Racer159 May 20, 2024
226222d
fix schema workflow
Racer159 May 20, 2024
a1891ba
fix schema
Racer159 May 20, 2024
cc7912d
remove zarf grype ignores
Racer159 May 20, 2024
4e8b002
add dependency review workflow
Racer159 May 20, 2024
e1a8865
update more zarf references
Racer159 May 20, 2024
7d26500
fix issue templates
Racer159 May 20, 2024
fc47b74
fix lints
Racer159 May 20, 2024
5107679
update to released exec
Racer159 May 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ builds:
- linux
- darwin
ldflags:
- -s -w -X 'github.com/defenseunicorns/maru-runner/src/config.CLIVersion={{.Tag}}' -X 'github.com/defenseunicorns/zarf/src/config.ActionsCommandZarfPrefix=zarf'
- -s -w -X 'github.com/defenseunicorns/maru-runner/src/config.CLIVersion={{.Tag}}'
goarch:
- amd64
- arm64
Expand Down
39 changes: 32 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2023-Present The UDS Authors
# SPDX-FileCopyrightText: 2023-Present the Maru Authors

ARCH ?= amd64
CLI_VERSION ?= $(if $(shell git describe --tags),$(shell git describe --tags),"UnknownVersion")
BUILD_ARGS := -s -w -X 'github.com/defenseunicorns/maru-runner/src/config.CLIVersion=$(CLI_VERSION)' \
-X 'github.com/defenseunicorns/zarf/src/config.ActionsCommandZarfPrefix=zarf'
BUILD_ARGS := -s -w -X 'github.com/defenseunicorns/maru-runner/src/config.CLIVersion=$(CLI_VERSION)'
SRC_FILES ?= $(shell find . -type f -name "*.go")

BUILD_CLI_FOR_SYSTEM := build-cli
UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_S),Darwin)
ifeq ($(UNAME_P),i386)
BUILD_CLI_FOR_SYSTEM := $(addsuffix -mac-intel,$(BUILD_CLI_FOR_SYSTEM))
endif
ifeq ($(UNAME_P),arm)
BUILD_CLI_FOR_SYSTEM := $(addsuffix -mac-apple,$(BUILD_CLI_FOR_SYSTEM))
endif
else ifeq ($(UNAME_S),Linux)
ifeq ($(UNAME_P),x86_64)
BUILD_CLI_FOR_SYSTEM := $(addsuffix -linux-amd,$(BUILD_CLI_FOR_SYSTEM))
endif
ifeq ($(UNAME_P),aarch64)
BUILD_CLI_FOR_SYSTEM := $(addsuffix -linux-arm,$(BUILD_CLI_FOR_SYSTEM))
endif
endif

.PHONY: help
help: ## Display this help information
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sort | awk 'BEGIN {FS = ":.*?## "}; \
{printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: build
build: ## Build the CLI for the current machine's OS and architecture
$(MAKE) $(BUILD_CLI_FOR_SYSTEM)

build-cli-linux-amd: ## Build the CLI for Linux AMD64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(BUILD_ARGS)" -o build/maru main.go

Expand All @@ -24,15 +47,17 @@ build-cli-mac-intel: ## Build the CLI for Mac Intel
build-cli-mac-apple: ## Build the CLI for Mac Apple
GOOS=darwin GOARCH=arm64 go build -ldflags="$(BUILD_ARGS)" -o build/maru-mac-apple main.go

.PHONY: test-e2e
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
test-e2e: ## Run End to End (e2e) tests
cd src/test/e2e && go test -failfast -v -timeout 30m

schema: ## Update JSON schema for maru tasks
./hack/generate-schema.sh
schema: tasks.schema.json ## Update JSON schema for maru tasks

test-schema: ## Test if the schema has been modified
$(MAKE) schema
test-schema: tasks.schema.json ## Test if the schema has been modified
./hack/test-generate-schema.sh

tasks.schema.json: $(SRC_FILES)
./hack/generate-schema.sh

clean: ## Clean up build artifacts
rm -rf build
381 changes: 5 additions & 376 deletions go.mod

Large diffs are not rendered by default.

1,861 changes: 5 additions & 1,856 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors
// SPDX-FileCopyrightText: 2023-Present the Maru Authors

// Package main is the entrypoint for the maru binary.
package main
Expand Down
6 changes: 0 additions & 6 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
"postUpdateOptions": [
"gomodTidy"
],
"packageRules": [
{
"matchPackageNames": ["defenseunicorns/zarf", "github.com/defenseunicorns/zarf"],
"groupName": "zarf"
}
],
"regexManagers": [
{
"fileMatch": [
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/internal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors
// SPDX-FileCopyrightText: 2023-Present the Maru Authors

// Package cmd contains the CLI commands for maru.
package cmd
Expand All @@ -10,8 +10,8 @@ import (

"github.com/alecthomas/jsonschema"
"github.com/defenseunicorns/maru-runner/src/config/lang"
"github.com/defenseunicorns/maru-runner/src/message"
"github.com/defenseunicorns/maru-runner/src/types"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/spf13/cobra"
)

Expand All @@ -30,7 +30,7 @@ var configTasksSchemaCmd = &cobra.Command{
schema := jsonschema.Reflect(&types.TasksFile{})
output, err := json.MarshalIndent(schema, "", " ")
if err != nil {
message.Fatal(err, lang.CmdInternalConfigSchemaErr)
message.Fatalf(err, "%s", lang.CmdInternalConfigSchemaErr)
}
fmt.Print(string(output) + "\n")
},
Expand Down
63 changes: 32 additions & 31 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors
// SPDX-FileCopyrightText: 2023-Present the Maru Authors

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

import (
"fmt"
"os"
"runtime/debug"
"strings"
"os/signal"
"syscall"

"github.com/defenseunicorns/maru-runner/src/config"
"github.com/defenseunicorns/maru-runner/src/config/lang"
"github.com/defenseunicorns/maru-runner/src/message"
"github.com/defenseunicorns/maru-runner/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/cmd/common"
zarfCommon "github.com/defenseunicorns/zarf/src/cmd/common"
zarfConfig "github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/pkg/exec"
"github.com/spf13/cobra"
)

var logLevelString string
var skipLogFile bool

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

zarfCommon.ExitOnInterrupt()
exitOnInterrupt()

// Don't add the logo to the help command
if cmd.Parent() == nil {
config.SkipLogFile = true
skipLogFile = true
}
cliSetup()
},
Expand All @@ -41,7 +37,7 @@ var rootCmd = &cobra.Command{
_, _ = fmt.Fprintln(os.Stderr)
err := cmd.Help()
if err != nil {
message.Fatal(err, "error calling help command")
message.Fatalf(err, "error calling help command")
}
},
}
Expand All @@ -57,15 +53,6 @@ func RootCmd() *cobra.Command {
}

func init() {
// grab Zarf version to make Zarf library checks happy
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "github.com/defenseunicorns/zarf" {
zarfConfig.CLIVersion = strings.Split(dep.Version, "v")[1]
}
}
}

initViper()

v.SetDefault(V_LOG_LEVEL, "info")
Expand All @@ -74,10 +61,10 @@ func init() {
v.SetDefault(V_TMP_DIR, "")
v.SetDefault(V_ENV_PREFIX, "RUN")

rootCmd.PersistentFlags().StringVarP(&config.LogLevel, "log-level", "l", v.GetString(V_LOG_LEVEL), lang.RootCmdFlagLogLevel)
rootCmd.PersistentFlags().StringVarP(&logLevelString, "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(&message.NoProgress, "no-progress", v.GetBool(V_NO_PROGRESS), lang.RootCmdFlagNoProgress)
rootCmd.PersistentFlags().BoolVar(&config.SkipLogFile, "no-log-file", v.GetBool(V_NO_LOG_FILE), lang.RootCmdFlagSkipLogFile)
rootCmd.PersistentFlags().BoolVar(&skipLogFile, "no-log-file", v.GetBool(V_NO_LOG_FILE), lang.RootCmdFlagSkipLogFile)
rootCmd.PersistentFlags().StringVar(&config.TempDirectory, "tmpdir", v.GetString(V_TMP_DIR), lang.RootCmdFlagTempDir)
}

Expand All @@ -92,16 +79,30 @@ func cliSetup() {
printViperConfigUsed()

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

if !config.SkipLogFile && !ListTasks && !ListAllTasks {
if !skipLogFile && !listTasks && !listAllTasks {
utils.UseLogFile()
}

// Register `uds` and `zarf` as coming from the $PATH when used directly from the CLI
exec.RegisterCmdMutation("uds", "uds")
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
exec.RegisterCmdMutation("zarf", "zarf")
}

// exitOnInterrupt catches an interrupt and exits with fatal error
func exitOnInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
message.Fatalf(lang.ErrInterrupt, "%s", lang.ErrInterrupt.Error())
}()
}
63 changes: 38 additions & 25 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors
// SPDX-FileCopyrightText: 2023-Present the Maru Authors

// Package cmd contains the CLI commands for maru.
package cmd
Expand All @@ -15,28 +15,29 @@ import (

"github.com/defenseunicorns/maru-runner/src/config"
"github.com/defenseunicorns/maru-runner/src/config/lang"
"github.com/defenseunicorns/maru-runner/src/message"
"github.com/defenseunicorns/maru-runner/src/pkg/runner"
"github.com/defenseunicorns/maru-runner/src/pkg/utils"
"github.com/defenseunicorns/maru-runner/src/types"
"github.com/defenseunicorns/pkg/helpers"
zarfCommon "github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/pkg/message"
zarfUtils "github.com/defenseunicorns/zarf/src/pkg/utils"
goyaml "github.com/goccy/go-yaml"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

// ListTasks is a flag to print available tasks in a TaskFileLocation (no includes)
var ListTasks bool
// listTasks is a flag to print available tasks in a TaskFileLocation (no includes)
var listTasks bool

// ListAllTasks is a flag to print available tasks in a TaskFileLocation
var ListAllTasks bool
// listAllTasks is a flag to print available tasks in a TaskFileLocation
var listAllTasks bool

// setRunnerVariables provides a map of set variables from the command line
var setRunnerVariables map[string]string

var runCmd = &cobra.Command{
Use: "run",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
zarfCommon.ExitOnInterrupt()
exitOnInterrupt()
cliSetup()
},
Short: lang.RootCmdShort,
Expand All @@ -50,29 +51,38 @@ var runCmd = &cobra.Command{
Run: func(_ *cobra.Command, args []string) {
var tasksFile types.TasksFile

// ensure vars are uppercase
config.SetRunnerVariables = helpers.TransformMapKeys(config.SetRunnerVariables, strings.ToUpper)

err := zarfUtils.ReadYaml(config.TaskFileLocation, &tasksFile)
err := utils.ReadYaml(config.TaskFileLocation, &tasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", config.TaskFileLocation)
}

if ListTasks || ListAllTasks {
// ensure vars are uppercase
setRunnerVariables = helpers.TransformMapKeys(setRunnerVariables, strings.ToUpper)

// set any env vars that come from the environment
for _, variable := range tasksFile.Variables {
if _, ok := setRunnerVariables[variable.Name]; !ok {
if value := os.Getenv(fmt.Sprintf("%s_%s", strings.ToUpper(config.EnvPrefix), variable.Name)); value != "" {
setRunnerVariables[variable.Name] = value
}
}
}

if listTasks || listAllTasks {
rows := [][]string{
{"Name", "Description"},
}
for _, task := range tasksFile.Tasks {
rows = append(rows, []string{task.Name, task.Description})
}
// If ListAllTasks, add tasks from included files
if ListAllTasks {
if listAllTasks {
listTasksFromIncludes(&rows, tasksFile)
}

err := pterm.DefaultTable.WithHasHeader().WithData(rows).Render()
if err != nil {
message.Fatal(err, "error listing tasks")
message.Fatalf(err, "Error listing tasks: %s", err.Error())
}

return
Expand All @@ -82,8 +92,8 @@ var runCmd = &cobra.Command{
if len(args) > 0 {
taskName = args[0]
}
if err := runner.Run(tasksFile, taskName, config.SetRunnerVariables); err != nil {
message.Fatalf(err, "Failed to run action: %s", err)
if err := runner.Run(tasksFile, taskName, setRunnerVariables); err != nil {
message.Fatalf(err, "Failed to run action: %s", err.Error())
}
},
}
Expand All @@ -96,7 +106,7 @@ func ListAutoCompleteTasks(_ *cobra.Command, _ []string, _ string) ([]string, co
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

err := zarfUtils.ReadYaml(config.TaskFileLocation, &tasksFile)
err := utils.ReadYaml(config.TaskFileLocation, &tasksFile)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -110,15 +120,18 @@ func ListAutoCompleteTasks(_ *cobra.Command, _ []string, _ string) ([]string, co

func listTasksFromIncludes(rows *[][]string, tasksFile types.TasksFile) {
var includedTasksFile types.TasksFile

variableConfig := runner.GetMaruVariableConfig()
variableConfig.PopulateVariables(tasksFile.Variables, setRunnerVariables)

templatePattern := `\${[^}]+}`
re := regexp.MustCompile(templatePattern)
for _, include := range tasksFile.Includes {
// get included TasksFile
for includeName, includeFileLocation := range include {
// check for templated variables in includeFileLocation value
if re.MatchString(includeFileLocation) {
templateMap := utils.PopulateTemplateMap(tasksFile.Variables, config.SetRunnerVariables)
includeFileLocation = utils.TemplateString(templateMap, includeFileLocation)
includeFileLocation = utils.TemplateString(variableConfig.GetSetVariables(), includeFileLocation)
}
// check if included file is a url
if helpers.IsURL(includeFileLocation) {
Expand Down Expand Up @@ -163,7 +176,7 @@ func loadTasksFromLocalIncludes(includeFileLocation string) types.TasksFile {
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
message.Fatalf(err, "%s not found", fullPath)
}
err := zarfUtils.ReadYaml(fullPath, &includedTasksFile)
err := utils.ReadYaml(fullPath, &includedTasksFile)
if err != nil {
message.Fatalf(err, "Cannot unmarshal %s", fullPath)
}
Expand All @@ -175,7 +188,7 @@ func init() {
rootCmd.AddCommand(runCmd)
runFlags := runCmd.Flags()
runFlags.StringVarP(&config.TaskFileLocation, "file", "f", config.TasksYAML, lang.CmdRunFlag)
runFlags.BoolVar(&ListTasks, "list", false, lang.CmdRunList)
runFlags.BoolVar(&ListAllTasks, "list-all", false, lang.CmdRunListAll)
runFlags.StringToStringVar(&config.SetRunnerVariables, "set", nil, lang.CmdRunSetVarFlag)
runFlags.BoolVar(&listTasks, "list", false, lang.CmdRunList)
runFlags.BoolVar(&listAllTasks, "list-all", false, lang.CmdRunListAll)
runFlags.StringToStringVar(&setRunnerVariables, "set", nil, lang.CmdRunSetVarFlag)
}
Loading
Loading