From 23d821d6246109f3a04029494b4a4f633698d26a Mon Sep 17 00:00:00 2001 From: Philipp Heuer Date: Fri, 24 Mar 2023 22:51:31 +0100 Subject: [PATCH] refactor: move normalize, denormalize, version cmd in separate files --- README.md | 27 ++++++------- cli/app.go | 23 ++++++----- cli/cmd/denormalize.go | 69 +++++++++++++++++++++++++++++++++ cli/cmd/normalize.go | 70 ++++++++++++++++++++++++++++++++++ cli/cmd/root.go | 51 ++----------------------- cli/cmd/run.go | 56 --------------------------- cli/cmd/util.go | 9 ----- cli/cmd/version.go | 45 ++++++++++++++++++++++ pkg/normalizeci/normalizeci.go | 11 +++--- 9 files changed, 219 insertions(+), 142 deletions(-) create mode 100644 cli/cmd/denormalize.go create mode 100644 cli/cmd/normalize.go delete mode 100644 cli/cmd/run.go create mode 100644 cli/cmd/version.go diff --git a/README.md b/README.md index 20a8991..bbcc92f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## features - **normalization** - check the env vars and the local repository to provide a [common set of env vars](docs/spec/variables.md) on any ci platform. -- **compatibility** - convert the common env vars into a specific format (ie. gitlab) to run a script made for gitlab on any ci provider. +- **compatibility** - convert the common env vars into a specific format (i.e. gitlab) to run a script made for gitlab on any ci provider. ## installation @@ -28,21 +28,22 @@ sudo chmod +x /usr/local/bin/normalizeci Examples: -| Id | Command | Description | -|-----|------------------------------------------------|-----------------------------------------------------------------------------------| -| 1 | `normalizeci --format export --output nci.env` | generate nci variables in format export for unix systems, stored as file | -| 2 | `normalizeci --format powershell` | generate nci variables in format export for windows powershell, written to stdout | -| 3 | `normalizeci --output nci.env` | generate nci variables in the suggested format for the current system | -| 4 | `normalizeci --hostenv --output nci.env` | additionally to 3 includes all env vars from the host | -| 5 | `normalizeci --format cmd` | generate nci variables in format export for windows cmd, written to stdout | -| 6 | `normalizeci -v` | print version information | +| Id | Command | Description | +|-----|----------------------------------------------------------|-----------------------------------------------------------------------------------| +| 1 | `normalizeci normalize --format export --output nci.env` | generate nci variables in format export for unix systems, stored as file | +| 2 | `normalizeci normalize --format powershell` | generate nci variables in format export for windows powershell, written to stdout | +| 3 | `normalizeci normalize --output nci.env` | generate nci variables in the suggested format for the current system | +| 4 | `normalizeci normalize --hostenv --output nci.env` | additionally to 3 includes all env vars from the host | +| 5 | `normalizeci normalize --format cmd` | generate nci variables in format export for windows cmd, written to stdout | +| 6 | `normalizeci denormalize --target gitlab` | generate a gitlab ci like environment based on the normalized environment | +| 7 | `normalizeci version` | print version information | #### file based Linux/MacOS ```bash -normalizeci --format export --output nci.env +normalizeci normalize --format export --output nci.env source nci.env rm nci.env ``` @@ -50,7 +51,7 @@ rm nci.env Windows ```powershell -normalizeci --format powershell --output nci.ps1 +normalizeci normalize --format powershell --output nci.ps1 & .\nci.ps1 rm nci.ps1 ``` @@ -62,13 +63,13 @@ The NormalizeCI CLI will return the commands to set the normalized variables in Linux/MacOS ```bash -eval $(normalizeci) +eval $(normalizeci normalize) ``` Windows ```powershell -$nenv = normalizeci +$nenv = normalizeci normalize Invoke-Expression "$nenv" ``` diff --git a/cli/app.go b/cli/app.go index 90b0f43..333500d 100644 --- a/cli/app.go +++ b/cli/app.go @@ -9,14 +9,12 @@ import ( "github.com/rs/zerolog/log" ) -// Version will be set at build time -var Version string - -// CommitHash will be set at build time -var CommitHash string - -// BuildAt will be set at build time -var BuildAt string +var ( + version = "dev" + commit = "none" + date = "unknown" + status = "clean" +) // Init Hook func init() { @@ -32,10 +30,11 @@ func init() { zerolog.SetGlobalLevel(zerolog.TraceLevel) } - // version information - cmd.Version = Version - cmd.CommitHash = CommitHash - cmd.BuildAt = BuildAt + // Set Version Information + cmd.Version = version + cmd.CommitHash = commit + cmd.BuildAt = date + cmd.RepositoryStatus = status } // CLI Main Entrypoint diff --git a/cli/cmd/denormalize.go b/cli/cmd/denormalize.go new file mode 100644 index 0000000..a5b78f3 --- /dev/null +++ b/cli/cmd/denormalize.go @@ -0,0 +1,69 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/cidverse/normalizeci/pkg/ncispec" + "github.com/cidverse/normalizeci/pkg/normalizeci" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(denormalizeCmd) + denormalizeCmd.PersistentFlags().StringP("format", "f", normalizeci.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)") + denormalizeCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.") + denormalizeCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?") + denormalizeCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services") +} + +var denormalizeCmd = &cobra.Command{ + Use: "denormalize", + Short: "denormalizes information about the current CI environment", + Run: func(cmd *cobra.Command, args []string) { + format, _ := cmd.Flags().GetString("format") + outputFile, _ := cmd.Flags().GetString("output") + strict, _ := cmd.Flags().GetBool("strict") + targets, _ := cmd.Flags().GetStringArray("target") + + // run normalization + var normalized = normalizeci.Normalize() + outputEnv := make(map[string]string) + normalizeci.SetProcessEnvironment(ncispec.ToMap(normalized)) + + // targets + if len(targets) > 0 { + for _, target := range targets { + denormalized := normalizeci.Denormalize(target, normalized) + for key, value := range denormalized { + outputEnv[key] = value + } + } + } + + // content? + content, err := normalizeci.FormatEnvironment(outputEnv, format) + if err != nil { + log.Fatal().Str("format", format).Str("supported", "export,powershell,cmd").Msg("unsupported format!") + } + + // validate? + if strict { + errors := normalized.Validate() + if len(errors) > 0 { + for _, line := range errors { + fmt.Printf("%s: %s [%s]\n", line.Field, line.Description, line.Value) + } + os.Exit(1) + } + } + + // output + if len(outputFile) > 0 { + fileOutput(outputFile, content) + } else { + consoleOutput(content) + } + }, +} diff --git a/cli/cmd/normalize.go b/cli/cmd/normalize.go new file mode 100644 index 0000000..119ba9a --- /dev/null +++ b/cli/cmd/normalize.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/cidverse/normalizeci/pkg/ncispec" + "github.com/cidverse/normalizeci/pkg/normalizeci" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(normalizeCmd) + normalizeCmd.PersistentFlags().StringP("format", "f", normalizeci.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)") + normalizeCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.") + normalizeCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?") + normalizeCmd.PersistentFlags().BoolP("version", "v", false, "all software has versions, this prints version information for normalizeci") + normalizeCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services") +} + +var normalizeCmd = &cobra.Command{ + Use: "normalize", + Short: "normalizes information about the current CI environment", + Run: func(cmd *cobra.Command, args []string) { + format, _ := cmd.Flags().GetString("format") + outputFile, _ := cmd.Flags().GetString("output") + strict, _ := cmd.Flags().GetBool("strict") + targets, _ := cmd.Flags().GetStringArray("target") + + // run normalization + var normalized = normalizeci.Normalize() + outputEnv := ncispec.ToMap(normalized) + normalizeci.SetProcessEnvironment(ncispec.ToMap(normalized)) + + // targets + if len(targets) > 0 { + for _, target := range targets { + denormalized := normalizeci.Denormalize(target, normalized) + for key, value := range denormalized { + outputEnv[key] = value + } + } + } + + // format content + content, err := normalizeci.FormatEnvironment(outputEnv, format) + if err != nil { + log.Fatal().Str("format", format).Str("supported", "export,powershell,cmd").Msg("unsupported format!") + } + + // validate? + if strict { + errors := normalized.Validate() + if len(errors) > 0 { + for _, line := range errors { + fmt.Printf("%s: %s [%s]\n", line.Field, line.Description, line.Value) + } + os.Exit(1) + } + } + + // output + if len(outputFile) > 0 { + fileOutput(outputFile, content) + } else { + consoleOutput(content) + } + }, +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index f8cef02..7cef0c6 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,65 +1,22 @@ package cmd import ( - "fmt" "os" - "runtime" "github.com/spf13/cobra" ) -// Version will be set at build time -var Version string - -// CommitHash will be set at build time -var CommitHash string - -// BuildAt will be set at build time -var BuildAt string - var rootCmd = &cobra.Command{ Use: `normalizeci`, - Short: `normalizeci provides a foundation for platform-agnostic CICD processes.`, - Long: `normalizeci provides a foundation for platform-agnostic CICD processes.`, - PreRunE: func(cmd *cobra.Command, args []string) error { - version, _ := cmd.Flags().GetBool("version") - if version { - printVersion() - os.Exit(0) - } - return nil - }, + Short: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`, + Long: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`, Run: func(cmd *cobra.Command, args []string) { - format, _ := cmd.Flags().GetString("format") - format = defaultFormat(format) - output, _ := cmd.Flags().GetString("output") - strict, _ := cmd.Flags().GetBool("strict") - targets, _ := cmd.Flags().GetStringArray("target") - - normalizationCommand(format, output, strict, targets) + _ = cmd.Help() + os.Exit(0) }, } -func init() { - rootCmd.PersistentFlags().StringP("format", "f", "systemdefault", "The format in which to store the normalized variables. (export, powershell, cmd)") - rootCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.") - rootCmd.PersistentFlags().Bool("hostenv", false, "Should include os env along with normalized variables into the target?") - rootCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?") - rootCmd.PersistentFlags().BoolP("version", "v", false, "all software has versions, this prints version information for normalizeci") - rootCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services") -} - // Execute executes the root command. func Execute() error { return rootCmd.Execute() } - -func printVersion() { - fmt.Fprintf(os.Stdout, "GitVersion: %s\n", Version) - fmt.Fprintf(os.Stdout, "GitCommit: %s\n", CommitHash) - fmt.Fprintf(os.Stdout, "GitTreeState: %s\n", "clean") - fmt.Fprintf(os.Stdout, "BuildDate: %s\n", BuildAt) - fmt.Fprintf(os.Stdout, "GoVersion: %s\n", runtime.Version()) - fmt.Fprintf(os.Stdout, "Compiler: %s\n", runtime.Compiler) - fmt.Fprintf(os.Stdout, "Platform: %s\n", runtime.GOOS+"/"+runtime.GOARCH) -} diff --git a/cli/cmd/run.go b/cli/cmd/run.go deleted file mode 100644 index 47fc91f..0000000 --- a/cli/cmd/run.go +++ /dev/null @@ -1,56 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "github.com/cidverse/normalizeci/pkg/ncispec" - "github.com/cidverse/normalizeci/pkg/normalizeci" - "github.com/rs/zerolog/log" -) - -func normalizationCommand(format string, output string, strict bool, targets []string) { - // run normalization - var normalized = normalizeci.Normalize() - - // set normalized variables in current session - outputEnv := ncispec.ToMap(normalized) - - // set process env - normalizeci.SetProcessEnvironment(ncispec.ToMap(normalized)) - - // targets - if len(targets) > 0 { - for _, target := range targets { - denormalized := normalizeci.Denormalize(target, normalized) - for key, value := range denormalized { - outputEnv[key] = value - } - } - } - - // content? - content := normalizeci.FormatEnvironment(outputEnv, format) - if len(content) == 0 { - log.Error().Msg("unsupported format!") - os.Exit(1) - } - - // validate? - if strict { - errors := normalized.Validate() - if len(errors) > 0 { - for _, line := range errors { - fmt.Printf("%s: %s [%s]\n", line.Field, line.Description, line.Value) - } - os.Exit(1) - } - } - - // output - if len(output) > 0 { - fileOutput(output, content) - } else { - consoleOutput(content) - } -} diff --git a/cli/cmd/util.go b/cli/cmd/util.go index 4980cbf..c32b977 100644 --- a/cli/cmd/util.go +++ b/cli/cmd/util.go @@ -4,18 +4,9 @@ import ( "io" "os" - "github.com/cidverse/normalizeci/pkg/normalizeci" "github.com/rs/zerolog/log" ) -func defaultFormat(value string) string { - if value == "systemdefault" { - return normalizeci.GetDefaultFormat() - } - - return value -} - func fileOutput(file string, content string) { contentByteArray := []byte(content) err := os.WriteFile(file, contentByteArray, 0644) diff --git a/cli/cmd/version.go b/cli/cmd/version.go new file mode 100644 index 0000000..30d87f0 --- /dev/null +++ b/cli/cmd/version.go @@ -0,0 +1,45 @@ +package cmd + +import ( + "fmt" + "os" + "runtime" + + "github.com/spf13/cobra" +) + +// Version will be set at build time +var Version string + +// RepositoryStatus will be set at build time +var RepositoryStatus string + +// CommitHash will be set at build time +var CommitHash string + +// BuildAt will be set at build time +var BuildAt string + +func init() { + rootCmd.AddCommand(versionCmd) + versionCmd.PersistentFlags().Bool("short", false, "only prints the plain version number without any other information") +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "print the version", + Run: func(cmd *cobra.Command, args []string) { + short, _ := cmd.Flags().GetBool("short") + if short { + fmt.Fprintf(os.Stdout, Version) + } else { + fmt.Fprintf(os.Stdout, "GitVersion: %s\n", Version) + fmt.Fprintf(os.Stdout, "GitCommit: %s\n", CommitHash) + fmt.Fprintf(os.Stdout, "GitTreeState: %s\n", RepositoryStatus) + fmt.Fprintf(os.Stdout, "BuildDate: %s\n", BuildAt) + fmt.Fprintf(os.Stdout, "GoVersion: %s\n", runtime.Version()) + fmt.Fprintf(os.Stdout, "Compiler: %s\n", runtime.Compiler) + fmt.Fprintf(os.Stdout, "Platform: %s\n", runtime.GOOS+"/"+runtime.GOARCH) + } + }, +} diff --git a/pkg/normalizeci/normalizeci.go b/pkg/normalizeci/normalizeci.go index 2b44780..193e404 100644 --- a/pkg/normalizeci/normalizeci.go +++ b/pkg/normalizeci/normalizeci.go @@ -1,6 +1,7 @@ package normalizeci import ( + "errors" "fmt" "os" "runtime" @@ -66,16 +67,16 @@ func Denormalize(target string, env ncispec.NormalizeCISpec) map[string]string { } // FormatEnvironment makes the normalized environment available in the current session -func FormatEnvironment(normalized map[string]string, format string) string { +func FormatEnvironment(normalized map[string]string, format string) (string, error) { if format == "export" { - return setNormalizedEnvironmentExport(normalized) + return setNormalizedEnvironmentExport(normalized), nil } else if format == "powershell" { - return setNormalizedEnvironmentPowershell(normalized) + return setNormalizedEnvironmentPowershell(normalized), nil } else if format == "cmd" { - return setNormalizedEnvironmentCmd(normalized) + return setNormalizedEnvironmentCmd(normalized), nil } - return "" + return "", errors.New("unsupported format: " + format) } func GetDefaultFormat() string {