Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/ci/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ci

import (
"fmt"
"path/filepath"
"strings"

"github.com/ory/viper"
"knative.dev/func/cmd/common"
Expand All @@ -12,6 +14,9 @@ const (

PathFlag = "path"

PlatformFlag = "platform"
DefaultPlatform = "github"

DefaultGitHubWorkflowDir = ".github/workflows"
DefaultGitHubWorkflowFilename = "func-deploy.yaml"

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cmd/ci/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestGitHubWorkflow_Export(t *testing.T) {
// GIVEN
cfg, _ := ci.NewCIGitHubConfig(
cfg, _ := ci.NewCIConfig(
common.CurrentBranchStub("", nil),
common.WorkDirStub("", nil),
)
Expand Down
5 changes: 5 additions & 0 deletions cmd/ci/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion cmd/config_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/config_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading