Skip to content

Commit

Permalink
feat: allow denormalization to target systems
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Mar 17, 2022
1 parent af51f13 commit b882a10
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cidverse/normalizeci

go 1.17
go 1.18

require (
github.com/Masterminds/semver/v3 v3.1.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iP
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.6.8/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
Expand Down Expand Up @@ -534,11 +533,9 @@ golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d h1:1n1fc535VhN8SYtD4cDUyNlfpAF2ROMM9+11equK3hs=
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -749,7 +746,6 @@ google.golang.org/api v0.59.0/go.mod h1:sT2boj7M9YJxZzgeZqXogmhfmRWDtPzT31xkieUb
google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I=
google.golang.org/api v0.62.0/go.mod h1:dKmwPCydfsad4qCH08MSdgWjfHOyfpd4VtDGgRFdavw=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
Expand Down
5 changes: 5 additions & 0 deletions pkg/azuredevops/azuredevops.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (n Normalizer) GetName() string {
return n.name
}

// GetSlug returns the slug of the normalizer
func (n Normalizer) GetSlug() string {
return n.slug
}

// Check if this package can handle the current environment
func (n Normalizer) Check(env map[string]string) bool {
if env["TF_BUILD"] == "True" {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var rootCmd = &cobra.Command{
output, _ := cmd.Flags().GetString("output")
hostEnv, _ := cmd.Flags().GetBool("hostEnv")
strict, _ := cmd.Flags().GetBool("strict")
targets, _ := cmd.Flags().GetStringArray("target")

normalizationCommand(format, hostEnv, output, strict)
normalizationCommand(format, hostEnv, output, strict, targets)
},
}

Expand All @@ -45,6 +46,7 @@ func init() {
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.
Expand Down
20 changes: 16 additions & 4 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,32 @@ import (
"os"
)

func normalizationCommand(format string, hostEnv bool, output string, strict bool) {
func normalizationCommand(format string, hostEnv bool, output string, strict bool, targets []string) {
// run normalization
var normalizedEnv = normalizeci.RunDefaultNormalization()

normalizeci.ConfigureProcessEnvironment(normalizedEnv)

// set normalized variables in current session
var nci = ncispec.OfMap(normalizedEnv)
if hostEnv == false {
nci.DATA = nil // exclude hostEnv from generation
}
content := normalizeci.FormatEnvironment(ncispec.ToMap(nci), format)
outputEnv := ncispec.ToMap(nci)

// set process env
normalizeci.SetProcessEnvironment(normalizedEnv)

// targets
if len(targets) > 0 {
for _, target := range targets {
denormalized := normalizeci.RunDenormalization(target, normalizedEnv)
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)
Expand Down
1 change: 1 addition & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Normalizer is a common interface to work with all normalizers
type Normalizer interface {
GetName() string
GetSlug() string
Check(env map[string]string) bool
Normalize(env map[string]string) map[string]string
Denormalize(env map[string]string) map[string]string
Expand Down
5 changes: 5 additions & 0 deletions pkg/githubactions/githubactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (n Normalizer) GetName() string {
return n.name
}

// GetSlug returns the slug of the normalizer
func (n Normalizer) GetSlug() string {
return n.slug
}

// Check if this package can handle the current environment
func (n Normalizer) Check(env map[string]string) bool {
if env["GITHUB_ACTIONS"] == "true" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/gitlabci/gitlabci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func (n Normalizer) GetName() string {
return n.name
}

// GetSlug returns the slug of the normalizer
func (n Normalizer) GetSlug() string {
return n.slug
}

// Check if this package can handle the current environment
func (n Normalizer) Check(env map[string]string) bool {
if env["GITLAB_CI"] == "true" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/localgit/localgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (n Normalizer) GetName() string {
return n.name
}

// GetSlug returns the slug of the normalizer
func (n Normalizer) GetSlug() string {
return n.slug
}

// Check if this package can handle the current environment
func (n Normalizer) Check(env map[string]string) bool {
return true
Expand Down
8 changes: 4 additions & 4 deletions pkg/ncispec/spec_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ type NormalizeCISpec struct {
NCI_COMMIT_DESCRIPTION string // The description of the latest commit on the current reference.
NCI_COMMIT_COUNT string `validate:"required"` // The total amount of commits inside of the current reference, can be used as build number.

NCI_LASTRELEASE_REF_NAME string // Human readable name of the last stable release.
NCI_LASTRELEASE_REF_SLUG string // Slug of the last stable release.
NCI_LASTRELEASE_REF_VCS string // Holds the vcs specific absolute reference name of the last stable release. (ex: `refs/heads/main`)
NCI_LASTRELEASE_COMMIT_AFTER_COUNT string // Holds the count of commits since the last stable release.
NCI_LASTRELEASE_REF_NAME string `validate:"required"` // Human readable name of the last stable release.
NCI_LASTRELEASE_REF_SLUG string `validate:"required"` // Slug of the last stable release.
NCI_LASTRELEASE_REF_VCS string `validate:"required"` // Holds the vcs specific absolute reference name of the last stable release. (ex: `refs/heads/main`)
NCI_LASTRELEASE_COMMIT_AFTER_COUNT string `validate:"required"` // Holds the count of commits since the last stable release.

NCI_DEPLOY_FREEZE string `validate:"required,boolean"` // Currently in a deploy freeze window? (`true`, `false`)
}
Expand Down
36 changes: 28 additions & 8 deletions pkg/normalizeci/normalizeci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ import (
"github.com/cidverse/normalizeci/pkg/localgit"
)

// holds all known normalizers
var normalizers []common.Normalizer

func init() {
normalizers = append(normalizers, azuredevops.NewNormalizer())
normalizers = append(normalizers, githubactions.NewNormalizer())
normalizers = append(normalizers, gitlabci.NewNormalizer())
normalizers = append(normalizers, localgit.NewNormalizer())
}

func RunDefaultNormalization() map[string]string {
env := common.GetMachineEnvironment()
return RunNormalization(env)
}

// RunNormalization executes the ci normalization for all supported services
func RunNormalization(env map[string]string) map[string]string {
// initialize normalizers
var normalizers []common.Normalizer
normalizers = append(normalizers, azuredevops.NewNormalizer())
normalizers = append(normalizers, githubactions.NewNormalizer())
normalizers = append(normalizers, gitlabci.NewNormalizer())
normalizers = append(normalizers, localgit.NewNormalizer())

// normalize (iterate over all supported systems and normalize variables if possible)
var normalized map[string]string
for _, normalizer := range normalizers {
Expand All @@ -43,6 +46,23 @@ func RunNormalization(env map[string]string) map[string]string {
return normalized
}

// RunDenormalization will generate ci variables for the target service
func RunDenormalization(target string, env map[string]string) map[string]string {
// denormalize
var normalized map[string]string
for _, normalizer := range normalizers {
if target == normalizer.GetSlug() {
log.Debug().Msg("Matched " + normalizer.GetName() + ", not checking for any other matches.")
normalized = normalizer.Denormalize(env)
break
} else {
log.Debug().Msg("Didn't match in " + normalizer.GetName())
}
}

return normalized
}

// FormatEnvironment makes the normalized environment available in the current session
func FormatEnvironment(normalized map[string]string, format string) string {
if format == "export" {
Expand All @@ -66,7 +86,7 @@ func GetDefaultFormat() string {
return ""
}

func ConfigureProcessEnvironment(normalized map[string]string) {
func SetProcessEnvironment(normalized map[string]string) {
for key, element := range normalized {
err := os.Setenv(key, element)
if err != nil {
Expand Down

0 comments on commit b882a10

Please sign in to comment.