Skip to content

Commit b882a10

Browse files
committed
feat: allow denormalization to target systems
1 parent af51f13 commit b882a10

File tree

11 files changed

+73
-22
lines changed

11 files changed

+73
-22
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/cidverse/normalizeci
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/Masterminds/semver/v3 v3.1.1

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iP
258258
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
259259
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
260260
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
261-
github.com/hashicorp/go-retryablehttp v0.6.8/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
262261
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
263262
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
264263
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
@@ -534,11 +533,9 @@ golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy
534533
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
535534
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
536535
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
537-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
538536
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d h1:1n1fc535VhN8SYtD4cDUyNlfpAF2ROMM9+11equK3hs=
539537
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
540538
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
541-
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
542539
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
543540
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
544541
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -749,7 +746,6 @@ google.golang.org/api v0.59.0/go.mod h1:sT2boj7M9YJxZzgeZqXogmhfmRWDtPzT31xkieUb
749746
google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I=
750747
google.golang.org/api v0.62.0/go.mod h1:dKmwPCydfsad4qCH08MSdgWjfHOyfpd4VtDGgRFdavw=
751748
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
752-
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
753749
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
754750
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
755751
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=

pkg/azuredevops/azuredevops.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func (n Normalizer) GetName() string {
2424
return n.name
2525
}
2626

27+
// GetSlug returns the slug of the normalizer
28+
func (n Normalizer) GetSlug() string {
29+
return n.slug
30+
}
31+
2732
// Check if this package can handle the current environment
2833
func (n Normalizer) Check(env map[string]string) bool {
2934
if env["TF_BUILD"] == "True" {

pkg/cmd/root.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var rootCmd = &cobra.Command{
3434
output, _ := cmd.Flags().GetString("output")
3535
hostEnv, _ := cmd.Flags().GetBool("hostEnv")
3636
strict, _ := cmd.Flags().GetBool("strict")
37+
targets, _ := cmd.Flags().GetStringArray("target")
3738

38-
normalizationCommand(format, hostEnv, output, strict)
39+
normalizationCommand(format, hostEnv, output, strict, targets)
3940
},
4041
}
4142

@@ -45,6 +46,7 @@ func init() {
4546
rootCmd.PersistentFlags().Bool("hostenv", false, "Should include os env along with normalized variables into the target?")
4647
rootCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?")
4748
rootCmd.PersistentFlags().BoolP("version", "v", false, "all software has versions, this prints version information for normalizeci")
49+
rootCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services")
4850
}
4951

5052
// Execute executes the root command.

pkg/cmd/run.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,32 @@ import (
88
"os"
99
)
1010

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

15-
normalizeci.ConfigureProcessEnvironment(normalizedEnv)
16-
1715
// set normalized variables in current session
1816
var nci = ncispec.OfMap(normalizedEnv)
1917
if hostEnv == false {
2018
nci.DATA = nil // exclude hostEnv from generation
2119
}
22-
content := normalizeci.FormatEnvironment(ncispec.ToMap(nci), format)
20+
outputEnv := ncispec.ToMap(nci)
21+
22+
// set process env
23+
normalizeci.SetProcessEnvironment(normalizedEnv)
24+
25+
// targets
26+
if len(targets) > 0 {
27+
for _, target := range targets {
28+
denormalized := normalizeci.RunDenormalization(target, normalizedEnv)
29+
for key, value := range denormalized {
30+
outputEnv[key] = value
31+
}
32+
}
33+
}
2334

2435
// content?
36+
content := normalizeci.FormatEnvironment(outputEnv, format)
2537
if len(content) == 0 {
2638
log.Error().Msg("unsupported format!")
2739
os.Exit(1)

pkg/common/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
// Normalizer is a common interface to work with all normalizers
1212
type Normalizer interface {
1313
GetName() string
14+
GetSlug() string
1415
Check(env map[string]string) bool
1516
Normalize(env map[string]string) map[string]string
1617
Denormalize(env map[string]string) map[string]string

pkg/githubactions/githubactions.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func (n Normalizer) GetName() string {
2424
return n.name
2525
}
2626

27+
// GetSlug returns the slug of the normalizer
28+
func (n Normalizer) GetSlug() string {
29+
return n.slug
30+
}
31+
2732
// Check if this package can handle the current environment
2833
func (n Normalizer) Check(env map[string]string) bool {
2934
if env["GITHUB_ACTIONS"] == "true" {

pkg/gitlabci/gitlabci.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func (n Normalizer) GetName() string {
2121
return n.name
2222
}
2323

24+
// GetSlug returns the slug of the normalizer
25+
func (n Normalizer) GetSlug() string {
26+
return n.slug
27+
}
28+
2429
// Check if this package can handle the current environment
2530
func (n Normalizer) Check(env map[string]string) bool {
2631
if env["GITLAB_CI"] == "true" {

pkg/localgit/localgit.go

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func (n Normalizer) GetName() string {
2323
return n.name
2424
}
2525

26+
// GetSlug returns the slug of the normalizer
27+
func (n Normalizer) GetSlug() string {
28+
return n.slug
29+
}
30+
2631
// Check if this package can handle the current environment
2732
func (n Normalizer) Check(env map[string]string) bool {
2833
return true

pkg/ncispec/spec_v1.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ type NormalizeCISpec struct {
112112
NCI_COMMIT_DESCRIPTION string // The description of the latest commit on the current reference.
113113
NCI_COMMIT_COUNT string `validate:"required"` // The total amount of commits inside of the current reference, can be used as build number.
114114

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

120120
NCI_DEPLOY_FREEZE string `validate:"required,boolean"` // Currently in a deploy freeze window? (`true`, `false`)
121121
}

pkg/normalizeci/normalizeci.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import (
1414
"github.com/cidverse/normalizeci/pkg/localgit"
1515
)
1616

17+
// holds all known normalizers
18+
var normalizers []common.Normalizer
19+
20+
func init() {
21+
normalizers = append(normalizers, azuredevops.NewNormalizer())
22+
normalizers = append(normalizers, githubactions.NewNormalizer())
23+
normalizers = append(normalizers, gitlabci.NewNormalizer())
24+
normalizers = append(normalizers, localgit.NewNormalizer())
25+
}
26+
1727
func RunDefaultNormalization() map[string]string {
1828
env := common.GetMachineEnvironment()
1929
return RunNormalization(env)
2030
}
2131

2232
// RunNormalization executes the ci normalization for all supported services
2333
func RunNormalization(env map[string]string) map[string]string {
24-
// initialize normalizers
25-
var normalizers []common.Normalizer
26-
normalizers = append(normalizers, azuredevops.NewNormalizer())
27-
normalizers = append(normalizers, githubactions.NewNormalizer())
28-
normalizers = append(normalizers, gitlabci.NewNormalizer())
29-
normalizers = append(normalizers, localgit.NewNormalizer())
30-
3134
// normalize (iterate over all supported systems and normalize variables if possible)
3235
var normalized map[string]string
3336
for _, normalizer := range normalizers {
@@ -43,6 +46,23 @@ func RunNormalization(env map[string]string) map[string]string {
4346
return normalized
4447
}
4548

49+
// RunDenormalization will generate ci variables for the target service
50+
func RunDenormalization(target string, env map[string]string) map[string]string {
51+
// denormalize
52+
var normalized map[string]string
53+
for _, normalizer := range normalizers {
54+
if target == normalizer.GetSlug() {
55+
log.Debug().Msg("Matched " + normalizer.GetName() + ", not checking for any other matches.")
56+
normalized = normalizer.Denormalize(env)
57+
break
58+
} else {
59+
log.Debug().Msg("Didn't match in " + normalizer.GetName())
60+
}
61+
}
62+
63+
return normalized
64+
}
65+
4666
// FormatEnvironment makes the normalized environment available in the current session
4767
func FormatEnvironment(normalized map[string]string, format string) string {
4868
if format == "export" {
@@ -66,7 +86,7 @@ func GetDefaultFormat() string {
6686
return ""
6787
}
6888

69-
func ConfigureProcessEnvironment(normalized map[string]string) {
89+
func SetProcessEnvironment(normalized map[string]string) {
7090
for key, element := range normalized {
7191
err := os.Setenv(key, element)
7292
if err != nil {

0 commit comments

Comments
 (0)