diff --git a/cmd/ci/config.go b/cmd/ci/config.go index 08c10f1915..169cbd4c48 100644 --- a/cmd/ci/config.go +++ b/cmd/ci/config.go @@ -1,7 +1,9 @@ package ci import ( + "fmt" "path/filepath" + "strings" "github.com/ory/viper" "knative.dev/func/cmd/common" @@ -12,6 +14,9 @@ const ( PathFlag = "path" + PlatformFlag = "platform" + DefaultPlatform = "github" + DefaultGitHubWorkflowDir = ".github/workflows" DefaultGitHubWorkflowFilename = "func-deploy.yaml" @@ -67,10 +72,15 @@ type CIConfig struct { useWorkflowDispatch bool } -func NewCIGitHubConfig( +func NewCIConfig( currentBranch common.CurrentBranchFunc, workingDir common.WorkDirFunc, ) (CIConfig, error) { + platform := viper.GetString(PlatformFlag) + if strings.ToLower(platform) != DefaultPlatform { + return CIConfig{}, fmt.Errorf("%s support is not implemented", platform) + } + path := viper.GetString(PathFlag) if path == "" || path == "." { cwd, err := workingDir() diff --git a/cmd/ci/workflow_test.go b/cmd/ci/workflow_test.go index 697e84673d..a317b8d4aa 100644 --- a/cmd/ci/workflow_test.go +++ b/cmd/ci/workflow_test.go @@ -11,7 +11,7 @@ import ( func TestGitHubWorkflow_Export(t *testing.T) { // GIVEN - cfg, _ := ci.NewCIGitHubConfig( + cfg, _ := ci.NewCIConfig( common.CurrentBranchStub("", nil), common.WorkDirStub("", nil), ) diff --git a/cmd/ci/writer.go b/cmd/ci/writer.go index 40c7872849..74c5abd1a3 100644 --- a/cmd/ci/writer.go +++ b/cmd/ci/writer.go @@ -11,14 +11,17 @@ const ( filePerm = 0644 // o: rw, g|u: r ) +// DefaultWorkflowWriter is the default implementation for writing workflow files to disk. var DefaultWorkflowWriter = &fileWriter{} +// WorkflowWriter defines the interface for writing workflow files. type WorkflowWriter interface { Write(path string, raw []byte) error } type fileWriter struct{} +// Write writes raw bytes to the specified path, creating directories as needed. func (fw *fileWriter) Write(path string, raw []byte) error { if err := os.MkdirAll(filepath.Dir(path), dirPerm); err != nil { return err @@ -36,10 +39,12 @@ type bufferWriter struct { Buffer *bytes.Buffer } +// NewBufferWriter creates a new bufferWriter for testing purposes. func NewBufferWriter() *bufferWriter { return &bufferWriter{Buffer: &bytes.Buffer{}} } +// Write stores the path and writes raw bytes to the internal buffer. func (bw *bufferWriter) Write(path string, raw []byte) error { bw.Path = path _, err := bw.Buffer.Write(raw) diff --git a/cmd/config_ci.go b/cmd/config_ci.go index e4e7cfc63c..25a2b141ab 100644 --- a/cmd/config_ci.go +++ b/cmd/config_ci.go @@ -19,6 +19,7 @@ func NewConfigCICmd( Use: "ci", Short: "Generate a GitHub Workflow for function deployment", PreRunE: bindEnv( + ci.PlatformFlag, ci.PathFlag, ci.UseRegistryLoginFlag, ci.WorkflowDispatchFlag, @@ -37,6 +38,12 @@ func NewConfigCICmd( }, } + cmd.Flags().String( + ci.PlatformFlag, + ci.DefaultPlatform, + "Pick a CI/CD platform for which a manifest will be generated. Currently only GitHub is supported.", + ) + addPathFlag(cmd) cmd.Flags().Bool( @@ -116,7 +123,7 @@ func runConfigCIGitHub( currentBranch common.CurrentBranchFunc, workingDir common.WorkDirFunc, ) error { - cfg, err := ci.NewCIGitHubConfig(currentBranch, workingDir) + cfg, err := ci.NewCIConfig(currentBranch, workingDir) if err != nil { return err } diff --git a/cmd/config_ci_test.go b/cmd/config_ci_test.go index 161741a3d5..0c2372e611 100644 --- a/cmd/config_ci_test.go +++ b/cmd/config_ci_test.go @@ -228,6 +228,54 @@ func TestNewConfigCICmd_BranchFlagResolutionError(t *testing.T) { assert.Error(t, result.executeErr, expectedErr.Error()) } +func TestNewConfigCICmd_GithubPlatformFlagSupported(t *testing.T) { + testCases := []struct { + name string + platformArg string + }{ + { + name: "empty value picks GitHub CI/CD platform as default", + platformArg: "", + }, + { + name: "GitHub value is supported", + platformArg: "--platform=github", + }, + { + name: "GitHub value is case insensitive", + platformArg: "--platform=GitHub", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // GIVEN + opts := defaultOpts() + opts.args = append(opts.args, tc.platformArg) + + // WHEN + result := runConfigCiCmd(t, opts) + + // THEN + assert.NilError(t, result.executeErr) + }) + } +} + +func TestNewConfigCICmd_UnsupportedPlatformError(t *testing.T) { + // GIVEN + platform := "unsupported" + expectedErr := fmt.Errorf("%s support is not implemented", platform) + opts := defaultOpts() + opts.args = append(opts.args, "--platform="+platform) + + // WHEN + result := runConfigCiCmd(t, opts) + + // THEN + assert.Error(t, result.executeErr, expectedErr.Error()) +} + // --------------------- // END: Broad Unit Tests