Add config-file flag

This commit is contained in:
Shreyas Goenka 2023-05-22 05:58:05 +02:00
parent 350053487a
commit 479e2b4e32
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 21 additions and 10 deletions

View File

@ -14,10 +14,11 @@ var initCmd = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// initialize default value for config file path // initialize default value for config file path
// TODO: add a test for this
if configFile == "" { if configFile == "" {
configFile = filepath.Join(targetDir, template.ConfigFileName) configFile = filepath.Join(targetDir, template.ConfigFileName)
} }
return template.Materialize(args[0], targetDir) return template.Materialize(args[0], targetDir, configFile)
}, },
} }

View File

@ -51,7 +51,12 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
func TestTemplateInitializationForProdConfig(t *testing.T) { func TestTemplateInitializationForProdConfig(t *testing.T) {
// create target directory with the input config // create target directory with the input config
tmp := t.TempDir() 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) require.NoError(t, err)
_, err = f.WriteString(` _, err = f.WriteString(`
{ {
@ -64,18 +69,23 @@ func TestTemplateInitializationForProdConfig(t *testing.T) {
f.Close() f.Close()
require.NoError(t, err) 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 // materialize the template
cmd := root.RootCmd cmd := root.RootCmd
childCommands := cmd.Commands() childCommands := cmd.Commands()
fmt.Println(childCommands) 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() err = cmd.Execute()
require.NoError(t, err) require.NoError(t, err)
// assert on materialized template // assert on materialized template
assert.FileExists(t, filepath.Join(tmp, "production_project", "azure_file")) assert.FileExists(t, filepath.Join(instanceDir, "production_project", "azure_file"))
assert.FileExists(t, filepath.Join(tmp, "production_project", ".azure_devops")) assert.FileExists(t, filepath.Join(instanceDir, "production_project", ".azure_devops"))
assert.NoFileExists(t, filepath.Join(tmp, "production_project", "aws_file")) assert.NoFileExists(t, filepath.Join(instanceDir, "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(instanceDir, "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") assertFileContains(t, filepath.Join(instanceDir, "production_project", ".azure_devops"), "This is a production project")
} }

View File

@ -8,7 +8,7 @@ const ConfigFileName = "config.json"
const schemaFileName = "schema.json" const schemaFileName = "schema.json"
const templateDirName = "template" 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 // read the file containing schema for template input parameters
schema, err := ReadSchema(filepath.Join(templatePath, schemaFileName)) schema, err := ReadSchema(filepath.Join(templatePath, schemaFileName))
if err != nil { if err != nil {
@ -16,7 +16,7 @@ func Materialize(templatePath, instancePath string) error {
} }
// read user config to initalize the template with // read user config to initalize the template with
config, err := schema.ReadConfig(filepath.Join(instancePath, ConfigFileName)) config, err := schema.ReadConfig(configPath)
if err != nil { if err != nil {
return err return err
} }