Skip to content

Commit 23d821d

Browse files
committed
refactor: move normalize, denormalize, version cmd in separate files
1 parent 4e3e0b6 commit 23d821d

File tree

9 files changed

+219
-142
lines changed

9 files changed

+219
-142
lines changed

README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## features
99

1010
- **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.
11-
- **compatibility** - convert the common env vars into a specific format (ie. gitlab) to run a script made for gitlab on any ci provider.
11+
- **compatibility** - convert the common env vars into a specific format (i.e. gitlab) to run a script made for gitlab on any ci provider.
1212

1313
## installation
1414

@@ -28,29 +28,30 @@ sudo chmod +x /usr/local/bin/normalizeci
2828

2929
Examples:
3030

31-
| Id | Command | Description |
32-
|-----|------------------------------------------------|-----------------------------------------------------------------------------------|
33-
| 1 | `normalizeci --format export --output nci.env` | generate nci variables in format export for unix systems, stored as file |
34-
| 2 | `normalizeci --format powershell` | generate nci variables in format export for windows powershell, written to stdout |
35-
| 3 | `normalizeci --output nci.env` | generate nci variables in the suggested format for the current system |
36-
| 4 | `normalizeci --hostenv --output nci.env` | additionally to 3 includes all env vars from the host |
37-
| 5 | `normalizeci --format cmd` | generate nci variables in format export for windows cmd, written to stdout |
38-
| 6 | `normalizeci -v` | print version information |
31+
| Id | Command | Description |
32+
|-----|----------------------------------------------------------|-----------------------------------------------------------------------------------|
33+
| 1 | `normalizeci normalize --format export --output nci.env` | generate nci variables in format export for unix systems, stored as file |
34+
| 2 | `normalizeci normalize --format powershell` | generate nci variables in format export for windows powershell, written to stdout |
35+
| 3 | `normalizeci normalize --output nci.env` | generate nci variables in the suggested format for the current system |
36+
| 4 | `normalizeci normalize --hostenv --output nci.env` | additionally to 3 includes all env vars from the host |
37+
| 5 | `normalizeci normalize --format cmd` | generate nci variables in format export for windows cmd, written to stdout |
38+
| 6 | `normalizeci denormalize --target gitlab` | generate a gitlab ci like environment based on the normalized environment |
39+
| 7 | `normalizeci version` | print version information |
3940

4041
#### file based
4142

4243
Linux/MacOS
4344

4445
```bash
45-
normalizeci --format export --output nci.env
46+
normalizeci normalize --format export --output nci.env
4647
source nci.env
4748
rm nci.env
4849
```
4950

5051
Windows
5152

5253
```powershell
53-
normalizeci --format powershell --output nci.ps1
54+
normalizeci normalize --format powershell --output nci.ps1
5455
& .\nci.ps1
5556
rm nci.ps1
5657
```
@@ -62,13 +63,13 @@ The NormalizeCI CLI will return the commands to set the normalized variables in
6263
Linux/MacOS
6364

6465
```bash
65-
eval $(normalizeci)
66+
eval $(normalizeci normalize)
6667
```
6768

6869
Windows
6970

7071
```powershell
71-
$nenv = normalizeci
72+
$nenv = normalizeci normalize
7273
Invoke-Expression "$nenv"
7374
```
7475

cli/app.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import (
99
"github.com/rs/zerolog/log"
1010
)
1111

12-
// Version will be set at build time
13-
var Version string
14-
15-
// CommitHash will be set at build time
16-
var CommitHash string
17-
18-
// BuildAt will be set at build time
19-
var BuildAt string
12+
var (
13+
version = "dev"
14+
commit = "none"
15+
date = "unknown"
16+
status = "clean"
17+
)
2018

2119
// Init Hook
2220
func init() {
@@ -32,10 +30,11 @@ func init() {
3230
zerolog.SetGlobalLevel(zerolog.TraceLevel)
3331
}
3432

35-
// version information
36-
cmd.Version = Version
37-
cmd.CommitHash = CommitHash
38-
cmd.BuildAt = BuildAt
33+
// Set Version Information
34+
cmd.Version = version
35+
cmd.CommitHash = commit
36+
cmd.BuildAt = date
37+
cmd.RepositoryStatus = status
3938
}
4039

4140
// CLI Main Entrypoint

cli/cmd/denormalize.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/cidverse/normalizeci/pkg/ncispec"
8+
"github.com/cidverse/normalizeci/pkg/normalizeci"
9+
"github.com/rs/zerolog/log"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func init() {
14+
rootCmd.AddCommand(denormalizeCmd)
15+
denormalizeCmd.PersistentFlags().StringP("format", "f", normalizeci.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)")
16+
denormalizeCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.")
17+
denormalizeCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?")
18+
denormalizeCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services")
19+
}
20+
21+
var denormalizeCmd = &cobra.Command{
22+
Use: "denormalize",
23+
Short: "denormalizes information about the current CI environment",
24+
Run: func(cmd *cobra.Command, args []string) {
25+
format, _ := cmd.Flags().GetString("format")
26+
outputFile, _ := cmd.Flags().GetString("output")
27+
strict, _ := cmd.Flags().GetBool("strict")
28+
targets, _ := cmd.Flags().GetStringArray("target")
29+
30+
// run normalization
31+
var normalized = normalizeci.Normalize()
32+
outputEnv := make(map[string]string)
33+
normalizeci.SetProcessEnvironment(ncispec.ToMap(normalized))
34+
35+
// targets
36+
if len(targets) > 0 {
37+
for _, target := range targets {
38+
denormalized := normalizeci.Denormalize(target, normalized)
39+
for key, value := range denormalized {
40+
outputEnv[key] = value
41+
}
42+
}
43+
}
44+
45+
// content?
46+
content, err := normalizeci.FormatEnvironment(outputEnv, format)
47+
if err != nil {
48+
log.Fatal().Str("format", format).Str("supported", "export,powershell,cmd").Msg("unsupported format!")
49+
}
50+
51+
// validate?
52+
if strict {
53+
errors := normalized.Validate()
54+
if len(errors) > 0 {
55+
for _, line := range errors {
56+
fmt.Printf("%s: %s [%s]\n", line.Field, line.Description, line.Value)
57+
}
58+
os.Exit(1)
59+
}
60+
}
61+
62+
// output
63+
if len(outputFile) > 0 {
64+
fileOutput(outputFile, content)
65+
} else {
66+
consoleOutput(content)
67+
}
68+
},
69+
}

cli/cmd/normalize.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/cidverse/normalizeci/pkg/ncispec"
8+
"github.com/cidverse/normalizeci/pkg/normalizeci"
9+
"github.com/rs/zerolog/log"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func init() {
14+
rootCmd.AddCommand(normalizeCmd)
15+
normalizeCmd.PersistentFlags().StringP("format", "f", normalizeci.GetDefaultFormat(), "The format in which to store the normalized variables. (export, powershell, cmd)")
16+
normalizeCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.")
17+
normalizeCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?")
18+
normalizeCmd.PersistentFlags().BoolP("version", "v", false, "all software has versions, this prints version information for normalizeci")
19+
normalizeCmd.PersistentFlags().StringArrayP("target", "t", []string{}, "Additionally generates the environment for the target ci services")
20+
}
21+
22+
var normalizeCmd = &cobra.Command{
23+
Use: "normalize",
24+
Short: "normalizes information about the current CI environment",
25+
Run: func(cmd *cobra.Command, args []string) {
26+
format, _ := cmd.Flags().GetString("format")
27+
outputFile, _ := cmd.Flags().GetString("output")
28+
strict, _ := cmd.Flags().GetBool("strict")
29+
targets, _ := cmd.Flags().GetStringArray("target")
30+
31+
// run normalization
32+
var normalized = normalizeci.Normalize()
33+
outputEnv := ncispec.ToMap(normalized)
34+
normalizeci.SetProcessEnvironment(ncispec.ToMap(normalized))
35+
36+
// targets
37+
if len(targets) > 0 {
38+
for _, target := range targets {
39+
denormalized := normalizeci.Denormalize(target, normalized)
40+
for key, value := range denormalized {
41+
outputEnv[key] = value
42+
}
43+
}
44+
}
45+
46+
// format content
47+
content, err := normalizeci.FormatEnvironment(outputEnv, format)
48+
if err != nil {
49+
log.Fatal().Str("format", format).Str("supported", "export,powershell,cmd").Msg("unsupported format!")
50+
}
51+
52+
// validate?
53+
if strict {
54+
errors := normalized.Validate()
55+
if len(errors) > 0 {
56+
for _, line := range errors {
57+
fmt.Printf("%s: %s [%s]\n", line.Field, line.Description, line.Value)
58+
}
59+
os.Exit(1)
60+
}
61+
}
62+
63+
// output
64+
if len(outputFile) > 0 {
65+
fileOutput(outputFile, content)
66+
} else {
67+
consoleOutput(content)
68+
}
69+
},
70+
}

cli/cmd/root.go

+4-47
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,22 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"os"
6-
"runtime"
75

86
"github.com/spf13/cobra"
97
)
108

11-
// Version will be set at build time
12-
var Version string
13-
14-
// CommitHash will be set at build time
15-
var CommitHash string
16-
17-
// BuildAt will be set at build time
18-
var BuildAt string
19-
209
var rootCmd = &cobra.Command{
2110
Use: `normalizeci`,
22-
Short: `normalizeci provides a foundation for platform-agnostic CICD processes.`,
23-
Long: `normalizeci provides a foundation for platform-agnostic CICD processes.`,
24-
PreRunE: func(cmd *cobra.Command, args []string) error {
25-
version, _ := cmd.Flags().GetBool("version")
26-
if version {
27-
printVersion()
28-
os.Exit(0)
29-
}
30-
return nil
31-
},
11+
Short: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`,
12+
Long: `normalizeci provides a foundation for platform-agnostic CI-CD processes.`,
3213
Run: func(cmd *cobra.Command, args []string) {
33-
format, _ := cmd.Flags().GetString("format")
34-
format = defaultFormat(format)
35-
output, _ := cmd.Flags().GetString("output")
36-
strict, _ := cmd.Flags().GetBool("strict")
37-
targets, _ := cmd.Flags().GetStringArray("target")
38-
39-
normalizationCommand(format, output, strict, targets)
14+
_ = cmd.Help()
15+
os.Exit(0)
4016
},
4117
}
4218

43-
func init() {
44-
rootCmd.PersistentFlags().StringP("format", "f", "systemdefault", "The format in which to store the normalized variables. (export, powershell, cmd)")
45-
rootCmd.PersistentFlags().StringP("output", "o", "", "Write output to this file instead of writing it to stdout.")
46-
rootCmd.PersistentFlags().Bool("hostenv", false, "Should include os env along with normalized variables into the target?")
47-
rootCmd.PersistentFlags().Bool("strict", false, "Validate the generated variables against the spec and fail on errors?")
48-
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")
50-
}
51-
5219
// Execute executes the root command.
5320
func Execute() error {
5421
return rootCmd.Execute()
5522
}
56-
57-
func printVersion() {
58-
fmt.Fprintf(os.Stdout, "GitVersion: %s\n", Version)
59-
fmt.Fprintf(os.Stdout, "GitCommit: %s\n", CommitHash)
60-
fmt.Fprintf(os.Stdout, "GitTreeState: %s\n", "clean")
61-
fmt.Fprintf(os.Stdout, "BuildDate: %s\n", BuildAt)
62-
fmt.Fprintf(os.Stdout, "GoVersion: %s\n", runtime.Version())
63-
fmt.Fprintf(os.Stdout, "Compiler: %s\n", runtime.Compiler)
64-
fmt.Fprintf(os.Stdout, "Platform: %s\n", runtime.GOOS+"/"+runtime.GOARCH)
65-
}

cli/cmd/run.go

-56
This file was deleted.

cli/cmd/util.go

-9
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@ import (
44
"io"
55
"os"
66

7-
"github.com/cidverse/normalizeci/pkg/normalizeci"
87
"github.com/rs/zerolog/log"
98
)
109

11-
func defaultFormat(value string) string {
12-
if value == "systemdefault" {
13-
return normalizeci.GetDefaultFormat()
14-
}
15-
16-
return value
17-
}
18-
1910
func fileOutput(file string, content string) {
2011
contentByteArray := []byte(content)
2112
err := os.WriteFile(file, contentByteArray, 0644)

0 commit comments

Comments
 (0)