Repository: score-spec/score-compose
Pull request #293 (2025-04-10 18:59:59)

feat: add no-default-provisioners flag
<!--- Provide a general summary of your changes in the Title above -->

#### Description
<!--- Describe your changes in detail -->
**Add `--no-default-provisioner flag`**
#### What does this PR do?
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
Adds the ability to disable automatic creation of the default provisioners file (zz-default.provisioners.yaml) during score-compose init
#286 
#### Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] New chore (expected functionality to be implemented)

#### Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [x] My change requires a change to the documentation.
  - [ ] I have updated the documentation accordingly.
- [x] I've signed off with an email address that matches the commit author.


7h3-3mp7y-m4n: > Thanks, @7h3-3mp7y-m4n.
> 
> JFYI, the unit test `init_test.go:38` is failing in the CI pipeline.

Thanks! :)

Patch:
diff --git a/internal/command/init.go b/internal/command/init.go
index 9d43cf6..8b66d3f 100644
--- a/internal/command/init.go
+++ b/internal/command/init.go
@@ -69,11 +69,12 @@ service:
 
 resources: {}
 `
-	initCmdFileFlag          = "file"
-	initCmdFileProjectFlag   = "project"
-	initCmdFileNoSampleFlag  = "no-sample"
-	initCmdProvisionerFlag   = "provisioners"
-	initCmdPatchTemplateFlag = "patch-templates"
+	initCmdFileFlag                  = "file"
+	initCmdFileProjectFlag           = "project"
+	initCmdFileNoSampleFlag          = "no-sample"
+	initCmdProvisionerFlag           = "provisioners"
+	initCmdPatchTemplateFlag         = "patch-templates"
+	initCmdNoDefaultProvisionersFlag = "no-default-provisioners"
 )
 
 //go:embed default.provisioners.yaml
@@ -208,21 +209,34 @@ URI Retrieval:
 			}
 
 			// create and write the default provisioners file if it doesn't already exist
-			dst := "zz-default" + loader.DefaultSuffix
-			if f, err := os.Stat(filepath.Join(sd.Path, "99-default"+loader.DefaultSuffix)); err == nil {
-				slog.Info(fmt.Sprintf("Default provisioners yaml file '%s' already exists, not overwriting it", f.Name()))
-			} else if f, err := os.OpenFile(filepath.Join(sd.Path, dst), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err != nil {
-				if !errors.Is(err, os.ErrExist) {
-					return fmt.Errorf("failed to open default provisioners for writing: %w", err)
+			disableDefaultProvisioners, err := cmd.Flags().GetBool(initCmdNoDefaultProvisionersFlag)
+			if err != nil {
+				return fmt.Errorf("failed to parse --%s flag: %w", initCmdNoDefaultProvisionersFlag, err)
+			}
+
+			if !disableDefaultProvisioners {
+				defaultProvisioners := filepath.Join(sd.Path, "zz-default.provisioners.yaml")
+
+				if _, err := os.Stat(defaultProvisioners); err != nil {
+					if !errors.Is(err, os.ErrNotExist) {
+						return fmt.Errorf("failed to check for existing default provisioners file: %w", err)
+					}
+
+					f, err := os.OpenFile(defaultProvisioners, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
+					if err != nil {
+						return fmt.Errorf("failed to create default provisioners file: %w", err)
+					}
+					defer f.Close()
+
+					slog.Info("Writing default provisioners file", "path", defaultProvisioners)
+					if _, err := f.WriteString(defaultProvisionersContent); err != nil {
+						return fmt.Errorf("failed to write default provisioners content: %w", err)
+					}
+				} else {
+					slog.Info("Default provisioners file already exists, skipping", "path", defaultProvisioners)
 				}
-				slog.Info(fmt.Sprintf("Default provisioners yaml file '%s' already exists, not overwriting it", dst))
 			} else {
-				defer f.Close()
-				slog.Info(fmt.Sprintf("Writing default provisioners yaml file '%s'", dst))
-				if _, err = f.WriteString(defaultProvisionersContent); err != nil {
-					return fmt.Errorf("failed to write provisioners: %w", err)
-				}
-				_ = f.Close()
+				slog.Info("Skipping default provisioners due to --no-default-provisioners flag")
 			}
 		}
 
@@ -270,7 +284,7 @@ URI Retrieval:
 			slog.Debug(fmt.Sprintf("Successfully loaded %d resource provisioners", len(provs)))
 		}
 
-		slog.Info(fmt.Sprintf("Read more about the Score specification at https://docs.score.dev/docs/"))
+		slog.Info("Read more about the Score specification at https://docs.score.dev/docs/")
 
 		return nil
 	},
@@ -282,6 +296,7 @@ func init() {
 	initCmd.Flags().Bool(initCmdFileNoSampleFlag, false, "Disable generation of the sample score file")
 	initCmd.Flags().StringArray(initCmdProvisionerFlag, nil, "Provisioner files to install. May be specified multiple times. Supports URI retrieval.")
 	initCmd.Flags().StringArray(initCmdPatchTemplateFlag, nil, "Patching template files to include. May be specified multiple times. Supports URI retrieval.")
+	initCmd.Flags().Bool(initCmdNoDefaultProvisionersFlag, false, "Disable generation of the default provisioners file")
 
 	rootCmd.AddCommand(initCmd)
 }


Test patch:
diff --git a/internal/command/init_test.go b/internal/command/init_test.go
index a4bf411..3d920ee 100644
--- a/internal/command/init_test.go
+++ b/internal/command/init_test.go
@@ -88,6 +88,7 @@ URI Retrieval:
 Flags:
   -f, --file string                   The score file to initialize (default "./score.yaml")
   -h, --help                          help for init
+      --no-default-provisioners       Disable generation of the default provisioners file
       --no-sample                     Disable generation of the sample score file
       --patch-templates stringArray   Patching template files to include. May be specified multiple times. Supports URI retrieval.
   -p, --project string                Set the name of the docker compose project (defaults to the current directory name)