From 479e2b4e32ce303bb7aaabba9231bc75f5188367 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 22 May 2023 05:58:05 +0200 Subject: [PATCH] Add config-file flag --- cmd/bundle/init.go | 3 ++- internal/init_test.go | 24 +++++++++++++++++------- libs/template/materialize.go | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/bundle/init.go b/cmd/bundle/init.go index 2cc2ffbe..bfca7735 100644 --- a/cmd/bundle/init.go +++ b/cmd/bundle/init.go @@ -14,10 +14,11 @@ var initCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { // initialize default value for config file path + // TODO: add a test for this if configFile == "" { configFile = filepath.Join(targetDir, template.ConfigFileName) } - return template.Materialize(args[0], targetDir) + return template.Materialize(args[0], targetDir, configFile) }, } diff --git a/internal/init_test.go b/internal/init_test.go index 5e679ea6..98c378b5 100644 --- a/internal/init_test.go +++ b/internal/init_test.go @@ -51,7 +51,12 @@ func TestTemplateInitializationForDevConfig(t *testing.T) { func TestTemplateInitializationForProdConfig(t *testing.T) { // create target directory with the input config tmp := t.TempDir() - f, err := os.Create(filepath.Join(tmp, "config.json")) + + // create target directory to with the input config + configDir := filepath.Join(tmp, "dir-with-config") + err := os.Mkdir(configDir, os.ModePerm) + require.NoError(t, err) + f, err := os.Create(filepath.Join(configDir, "my_config.json")) require.NoError(t, err) _, err = f.WriteString(` { @@ -64,18 +69,23 @@ func TestTemplateInitializationForProdConfig(t *testing.T) { f.Close() require.NoError(t, err) + // create directory to initialize the template instance within + instanceDir := filepath.Join(tmp, "dir-with-instance") + err = os.Mkdir(instanceDir, os.ModePerm) + require.NoError(t, err) + // materialize the template cmd := root.RootCmd childCommands := cmd.Commands() fmt.Println(childCommands) - cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp}) + cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", instanceDir, "--config-file", filepath.Join(configDir, "my_config.json")}) err = cmd.Execute() require.NoError(t, err) // assert on materialized template - assert.FileExists(t, filepath.Join(tmp, "production_project", "azure_file")) - assert.FileExists(t, filepath.Join(tmp, "production_project", ".azure_devops")) - assert.NoFileExists(t, filepath.Join(tmp, "production_project", "aws_file")) - assertFileContains(t, filepath.Join(tmp, "production_project", "azure_file"), "This file should only be generated for Azure") - assertFileContains(t, filepath.Join(tmp, "production_project", ".azure_devops"), "This is a production project") + assert.FileExists(t, filepath.Join(instanceDir, "production_project", "azure_file")) + assert.FileExists(t, filepath.Join(instanceDir, "production_project", ".azure_devops")) + assert.NoFileExists(t, filepath.Join(instanceDir, "production_project", "aws_file")) + assertFileContains(t, filepath.Join(instanceDir, "production_project", "azure_file"), "This file should only be generated for Azure") + assertFileContains(t, filepath.Join(instanceDir, "production_project", ".azure_devops"), "This is a production project") } diff --git a/libs/template/materialize.go b/libs/template/materialize.go index 57963d12..07d2f057 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -8,7 +8,7 @@ const ConfigFileName = "config.json" const schemaFileName = "schema.json" const templateDirName = "template" -func Materialize(templatePath, instancePath string) error { +func Materialize(templatePath, instancePath, configPath string) error { // read the file containing schema for template input parameters schema, err := ReadSchema(filepath.Join(templatePath, schemaFileName)) if err != nil { @@ -16,7 +16,7 @@ func Materialize(templatePath, instancePath string) error { } // read user config to initalize the template with - config, err := schema.ReadConfig(filepath.Join(instancePath, ConfigFileName)) + config, err := schema.ReadConfig(configPath) if err != nil { return err }