-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move normalize, denormalize, version cmd in separate files
- Loading branch information
1 parent
4e3e0b6
commit 23d821d
Showing
9 changed files
with
219 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.