-
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.
- Loading branch information
1 parent
60cf10c
commit 73a2b9e
Showing
8 changed files
with
319 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/cidverse/normalizeci/cmd" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var ( | ||
version = "dev" | ||
commit = "none" | ||
date = "unknown" | ||
status = "clean" | ||
) | ||
|
||
// Init Hook | ||
func init() { | ||
// Set Version Information | ||
cmd.Version = version | ||
cmd.CommitHash = commit | ||
cmd.BuildAt = date | ||
cmd.RepositoryStatus = status | ||
} | ||
|
||
// CLI Main Entrypoint | ||
func main() { | ||
rootCommand := cmd.RootCmd() | ||
cmdErr := rootCommand.Execute() | ||
if cmdErr != nil { | ||
log.Fatal().Err(cmdErr).Msg("cli error") | ||
} | ||
} |
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,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cidverse/normalizeci/pkg/normalizer" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func denormalizeCmd() *cobra.Command { | ||
cmd := &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, err = normalizer.Normalize() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("normalization failed") | ||
} | ||
|
||
// output | ||
outputEnv := make(map[string]string) | ||
|
||
// targets | ||
if len(targets) > 0 { | ||
for _, target := range targets { | ||
denormalized, err := normalizer.Denormalize(target, normalized) | ||
if err != nil { | ||
log.Fatal().Err(err).Str("target", target).Msg("denormalization failed") | ||
} | ||
|
||
for key, value := range denormalized { | ||
outputEnv[key] = value | ||
} | ||
} | ||
} | ||
|
||
// set process env | ||
normalizer.SetProcessEnvironment(outputEnv) | ||
|
||
// content? | ||
content, err := normalizer.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) | ||
} | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().StringP("format", "f", normalizer.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)") | ||
cmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.") | ||
cmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?") | ||
cmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services") | ||
|
||
return cmd | ||
} |
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,82 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cidverse/normalizeci/pkg/envstruct" | ||
"github.com/cidverse/normalizeci/pkg/normalizer" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func normalizeCmd() *cobra.Command { | ||
cmd := &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, err = normalizer.Normalize() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("normalization failed") | ||
} | ||
|
||
// transform to map | ||
outputEnv := envstruct.StructToEnvMap(normalized) | ||
|
||
// targets | ||
if len(targets) > 0 { | ||
for _, target := range targets { | ||
denormalized, err := normalizer.Denormalize(target, normalized) | ||
if err != nil { | ||
log.Fatal().Err(err).Str("target", target).Msg("denormalization failed") | ||
} | ||
|
||
for key, value := range denormalized { | ||
outputEnv[key] = value | ||
} | ||
} | ||
} | ||
|
||
// set process env | ||
normalizer.SetProcessEnvironment(outputEnv) | ||
|
||
// format content | ||
content, err := normalizer.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) | ||
} | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().StringP("format", "f", normalizer.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)") | ||
cmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.") | ||
cmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?") | ||
cmd.PersistentFlags().BoolP("version", "v", false, "all software has versions, this prints version information for normalizeci") | ||
cmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services") | ||
|
||
return cmd | ||
} |
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,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/cidverse/cidverseutils/zerologconfig" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cfg zerologconfig.LogConfig | ||
|
||
func RootCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: `normalizeci`, | ||
Short: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`, | ||
Long: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
if cfg.LogLevel == "" { | ||
if os.Getenv("NCI_LOG_LEVEL") != "" { | ||
cfg.LogLevel = os.Getenv("NCI_LOG_LEVEL") | ||
} else if os.Getenv("NCI_DEBUG") == "true" { // legacy 0.x toggle for debug mode | ||
cfg.LogLevel = "trace" | ||
} | ||
} | ||
|
||
zerologconfig.Configure(cfg) | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_ = cmd.Help() | ||
os.Exit(0) | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "log level - allowed: "+strings.Join(zerologconfig.ValidLogLevels, ",")) | ||
cmd.PersistentFlags().StringVar(&cfg.LogFormat, "log-format", "color", "log format - allowed: "+strings.Join(zerologconfig.ValidLogFormats, ",")) | ||
cmd.PersistentFlags().BoolVar(&cfg.LogCaller, "log-caller", false, "include caller in log functions") | ||
|
||
cmd.AddCommand(versionCmd()) | ||
cmd.AddCommand(normalizeCmd()) | ||
cmd.AddCommand(denormalizeCmd()) | ||
|
||
return cmd | ||
} |
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,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func fileOutput(file string, content string) { | ||
contentByteArray := []byte(content) | ||
err := os.WriteFile(file, contentByteArray, 0644) | ||
if err != nil { | ||
log.Err(err).Str("file", file).Msg("failed to generate file") | ||
} | ||
} | ||
|
||
func consoleOutput(content string) { | ||
_, err := io.WriteString(os.Stdout, content) | ||
if err != nil { | ||
log.Err(err).Msg("failed to write content to stdout") | ||
} | ||
} |
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,39 @@ | ||
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 versionCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "print version information", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_, _ = 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) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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.