Skip to content

Commit

Permalink
fix: move from repo-name to container-registry (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani authored Aug 15, 2023
1 parent 6c9f3ed commit cebfa95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
appNameFlag string = "app-name"
appVersionFlag string = "app-version"
projectDirectoryFlag string = "project-directory"
repoNameFlag string = "repo-name"
repoNameFlag string = "container-registry"
dockerFileNameFlag string = "dockerfile-name"
configFileFlag string = "config-file"
namespaceFlag string = "namespace"
Expand Down Expand Up @@ -138,10 +138,11 @@ func init() {
},
&cli.StringFlag{
Name: repoNameFlag,
Usage: "The base address of the container repository",
Aliases: []string{"repo-name"}, // keep compatibility with old version of the config
Usage: "The base address of the container registry",
Value: registry,
Required: registry == "",
EnvVars: []string{"INITIUM_REPO_NAME"},
EnvVars: []string{"INITIUM_CONTAINER_REGISTRY", "INITIUM_REPO_NAME"}, // INITIUM_REPO_NAME to keep compatibility with older config
},
&cli.StringFlag{
Name: dockerFileNameFlag,
Expand Down Expand Up @@ -211,12 +212,14 @@ func (c CLI) loadFlagsFromConfig(ctx *cli.Context) error {
}

for _, v := range ctx.Command.Flags {
name := v.Names()[0]
c.Logger.Debugf("%s is set to %s", name, ctx.String(name))
if name != "help" && !ctx.IsSet(name) {
if config[name] != nil {
c.Logger.Debugf("Loading %s as %s", name, config[name])
ctx.Set(name, config[name].(string))
mainName := v.Names()[0]
for _, name := range v.Names() {
c.Logger.Debugf("%s is set to %s", name, ctx.String(name))
if name != "help" && !ctx.IsSet(mainName) {
if config[name] != nil {
c.Logger.Debugf("Loading %s as %s", name, config[name])
ctx.Set(mainName, config[name].(string))
}
}
}
}
Expand Down
56 changes: 45 additions & 11 deletions src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,39 @@ import (
"github.com/charmbracelet/log"
)

func compareConfig(t *testing.T, appName string, writer io.Writer) {
func compareConfig(t *testing.T, appName string, registry string, writer io.Writer) {
configTemplate := fmt.Sprintf(`app-name: %s
cluster-endpoint: null
container-registry: %s
default-branch: main
dockerfile-name: null
registry-user: null
repo-name: ghcr.io/nearform
runtime-version: null
`,
appName,
registry,
)

result := fmt.Sprint(writer.(*bytes.Buffer))

if configTemplate != result {
t.Errorf("no match between\n%sand\n%s", configTemplate, result)
}

}

func TestInitConfig(t *testing.T) {
cli := CLI{
func getCLI() CLI {
return CLI{
Writer: new(bytes.Buffer),
Logger: log.NewWithOptions(os.Stderr, log.Options{
Level: log.ParseLevel(os.Getenv("INITIUM_LOG_LEVEL")),
ReportCaller: true,
ReportTimestamp: true,
}),
}
}

os.Setenv("INITIUM_REPO_NAME", "ghcr.io/nearform")

func TestInitConfig(t *testing.T) {
cli := getCLI()
// Config file is read correctly

// Generate temporary file and add app-name parameter
Expand All @@ -51,28 +53,60 @@ func TestInitConfig(t *testing.T) {
defer f.Close()
defer os.Remove(f.Name())

if _, err := f.Write([]byte("app-name: FromFile")); err != nil {
registry := "ghcr.io/nearform"

if _, err := f.WriteString("app-name: FromFile\ncontainer-registry: " + registry); err != nil {
t.Errorf("writing config content %v", err)
}

cli.Writer = new(bytes.Buffer)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromFile", cli.Writer)
compareConfig(t, "FromFile", registry, cli.Writer)

// Environment Variable wins over config
os.Setenv("INITIUM_APP_NAME", "FromEnv")
cli.Writer = new(bytes.Buffer)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromEnv", cli.Writer)
compareConfig(t, "FromEnv", registry, cli.Writer)

// Command line argument wins over config and Environment variable
cli.Writer = new(bytes.Buffer)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "--app-name=FromParam", "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromParam", cli.Writer)
compareConfig(t, "FromParam", registry, cli.Writer)

}

func TestRepoNameRetrocompatibiliy(t *testing.T) {
cli := getCLI()

// Generate temporary file and add repo-name parameter
f, err := os.CreateTemp("", "tmpfile-")
if err != nil {
t.Errorf("creating temporary file %v", err)
}
defer f.Close()
defer os.Remove(f.Name())

if _, err := f.WriteString("repo-name: FromFile"); err != nil {
t.Errorf("writing config content %v", err)
}

cli.Writer = new(bytes.Buffer)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "--app-name=FromParam", "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromParam", "FromFile", cli.Writer)

//Override from parameter
cli.Writer = new(bytes.Buffer)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "--app-name=FromParam", "--container-registry=ghcr.io/nearform", "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromParam", "ghcr.io/nearform", cli.Writer)
}

0 comments on commit cebfa95

Please sign in to comment.