Skip to content

Commit d2c850f

Browse files
committed
refactor: extract ci logic into dedicated package
- Move CIConfig and workflow structs to cmd/ci package - Separate config management (config.go) and workflow generation (workflow.go) - Add CIConfig methods for path resolution - Support custom workflow names via NewDefaultCIConfigWithName - Update imports across cmd package Issue SRVOCF-744 Signed-off-by: Stanislav Jakuschevskij <sjakusch@redhat.com>
1 parent e454113 commit d2c850f

File tree

7 files changed

+210
-107
lines changed

7 files changed

+210
-107
lines changed

cmd/ci/config.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ci
2+
3+
import "path/filepath"
4+
5+
type CIConfig struct {
6+
githubWorkflowDir,
7+
githubWorkflowFile,
8+
workflowName string
9+
}
10+
11+
func NewDefaultCIConfigWithName(name string) CIConfig {
12+
result := NewCIConfig(
13+
".github/workflows",
14+
"remote-build-and-deploy.yaml",
15+
"Remote Build and Deploy",
16+
)
17+
result.workflowName = name
18+
19+
return result
20+
}
21+
22+
func NewDefaultCIConfig() CIConfig {
23+
return NewCIConfig(
24+
".github/workflows",
25+
"remote-build-and-deploy.yaml",
26+
"Remote Build and Deploy",
27+
)
28+
}
29+
30+
func NewCIConfig(workflowDir, workflowFile, workflowName string) CIConfig {
31+
return CIConfig{
32+
workflowDir,
33+
workflowFile,
34+
workflowName,
35+
}
36+
}
37+
38+
func (cc *CIConfig) FnGithubWorkflowDir(fnRoot string) string {
39+
return filepath.Join(fnRoot, cc.githubWorkflowDir)
40+
}
41+
42+
func (cc *CIConfig) FnGithubWorkflowYamlPath(fnRoot string) string {
43+
return filepath.Join(cc.FnGithubWorkflowDir(fnRoot), cc.githubWorkflowFile)
44+
}
45+
46+
func (cc *CIConfig) WorkflowName() string {
47+
return cc.workflowName
48+
}

cmd/ci/workflow.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ci
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
type GithubWorkflow struct {
11+
Name string `yaml:"name"`
12+
On WorkflowTriggers `yaml:"on"`
13+
Jobs map[string]Job `yaml:"jobs"`
14+
}
15+
16+
type WorkflowTriggers struct {
17+
Push *PushTrigger `yaml:"push,omitempty"`
18+
}
19+
20+
type PushTrigger struct {
21+
Branches []string `yaml:"branches,omitempty"`
22+
}
23+
24+
type Job struct {
25+
RunsOn string `yaml:"runs-on"`
26+
Steps []Step `yaml:"steps"`
27+
}
28+
29+
type Step struct {
30+
Name string `yaml:"name,omitempty"`
31+
Uses string `yaml:"uses,omitempty"`
32+
Run string `yaml:"run,omitempty"`
33+
With map[string]string `yaml:"with,omitempty"`
34+
}
35+
36+
func NewGithubWorkflow(name string) *GithubWorkflow {
37+
return &GithubWorkflow{
38+
Name: name,
39+
On: WorkflowTriggers{
40+
Push: &PushTrigger{Branches: []string{"main"}},
41+
},
42+
Jobs: map[string]Job{
43+
"deploy": {
44+
RunsOn: "ubuntu-latest",
45+
Steps: []Step{
46+
{
47+
Name: "Checkout code",
48+
Uses: "actions/checkout@v4",
49+
},
50+
{
51+
Name: "Install func cli",
52+
Uses: "gauron99/knative-func-action@main",
53+
With: map[string]string{
54+
"version": "knative-v1.19.1",
55+
"name": "func",
56+
},
57+
},
58+
{
59+
Name: "Deploy function",
60+
Run: "func deploy --remote",
61+
},
62+
},
63+
},
64+
},
65+
}
66+
}
67+
68+
func (gw *GithubWorkflow) AsYaml() ([]byte, error) {
69+
return yaml.Marshal(gw)
70+
}
71+
72+
const (
73+
dirPerm = 0755 // o: rwx, g|u: r-x
74+
filePerm = 0644 // o: rw, g|u: r
75+
)
76+
77+
func PersistToDisk(workflowYamlAsBytes []byte, workflowFilepath string) error {
78+
if err := os.MkdirAll(filepath.Dir(workflowFilepath), dirPerm); err != nil {
79+
return err
80+
}
81+
82+
if err := os.WriteFile(workflowFilepath, workflowYamlAsBytes, filePerm); err != nil {
83+
return err
84+
}
85+
86+
return nil
87+
}

cmd/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"github.com/ory/viper"
88
"github.com/spf13/cobra"
99

10+
"knative.dev/func/cmd/ci"
1011
"knative.dev/func/cmd/common"
1112
"knative.dev/func/pkg/config"
1213
fn "knative.dev/func/pkg/functions"
1314
)
1415

15-
// TODO(twoGiants): move CIConfig later to a different place, probably to ./pkg/ci/*
16-
func NewConfigCmd(loadSaver common.FunctionLoaderSaver, newClient ClientFactory, TEMP_ciConfig CIConfig) *cobra.Command {
16+
// TODO(twoGiants): decide if you want to remove TEMP_ciConfig and init somewhere else
17+
func NewConfigCmd(loadSaver common.FunctionLoaderSaver, newClient ClientFactory, TEMP_ciConfig ci.CIConfig) *cobra.Command {
1718
cmd := &cobra.Command{
1819
Use: "config",
1920
Short: "Configure a function",

cmd/config_ci.go

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package cmd
22

33
import (
4-
"os"
5-
"path/filepath"
6-
74
"github.com/spf13/cobra"
5+
6+
"knative.dev/func/cmd/ci"
87
"knative.dev/func/cmd/common"
98
)
109

11-
func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, ciConfig CIConfig) *cobra.Command {
10+
func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, ciConfig ci.CIConfig) *cobra.Command {
1211
cmd := &cobra.Command{
1312
Use: "ci",
1413
RunE: func(cmd *cobra.Command, args []string) (err error) {
@@ -21,93 +20,22 @@ func NewConfigCICmd(loaderSaver common.FunctionLoaderSaver, ciConfig CIConfig) *
2120
return cmd
2221
}
2322

24-
type CIConfig struct {
25-
GithubWorkflowDir,
26-
GithubWorkflowFile string
27-
dirPerm,
28-
filePerm os.FileMode
29-
}
30-
31-
func NewDefaultCIConfig() CIConfig {
32-
return NewCIConfig(
33-
".github/workflows",
34-
"remote-build-and-deploy.yaml",
35-
)
36-
}
37-
38-
func NewCIConfig(workflowDir, workflowFile string) CIConfig {
39-
return CIConfig{
40-
workflowDir,
41-
workflowFile,
42-
0755, // o: rwx, g|u: r-x
43-
0644, // o: rw, g|u: r
44-
}
45-
}
46-
47-
type GithubWorkflow struct {
48-
Name string `yaml:"name"`
49-
On WorkflowTriggers `yaml:"on"`
50-
Jobs map[string]Job `yaml:"jobs"`
51-
}
52-
53-
type WorkflowTriggers struct {
54-
Push *PushTrigger `yaml:"push,omitempty"`
55-
}
56-
57-
type PushTrigger struct {
58-
Branches []string `yaml:"branches,omitempty"`
59-
}
60-
61-
type Job struct {
62-
RunsOn string `yaml:"runs-on"`
63-
Steps []Step `yaml:"steps"`
64-
}
65-
66-
type Step struct {
67-
Name string `yaml:"name,omitempty"`
68-
Uses string `yaml:"uses,omitempty"`
69-
Run string `yaml:"run,omitempty"`
70-
With map[string]string `yaml:"with,omitempty"`
71-
}
72-
7323
func runConfigCIGithub(
7424
fnLoaderSaver common.FunctionLoaderSaver,
75-
ciConfig CIConfig,
25+
ciConfig ci.CIConfig,
7626
) error {
7727
f, err := initConfigCommand(fnLoaderSaver)
7828
if err != nil {
7929
return err
8030
}
8131

82-
fnWorkflowDirPath := filepath.Join(f.Root, ciConfig.GithubWorkflowDir)
83-
if err := os.MkdirAll(fnWorkflowDirPath, ciConfig.dirPerm); err != nil {
32+
githubWorkflow := ci.NewGithubWorkflow(ciConfig.WorkflowName())
33+
githubWorkflowAsYamlBytes, err := githubWorkflow.AsYaml()
34+
if err != nil {
8435
return err
8536
}
8637

87-
workflowYamlContent := `name: Remote Build and Deploy
88-
89-
on:
90-
push:
91-
branches:
92-
- main
93-
94-
jobs:
95-
deploy:
96-
runs-on: ubuntu-latest
97-
steps:
98-
- name: Checkout code
99-
uses: actions/checkout@v4
100-
101-
- name: Install func cli
102-
uses: gauron99/knative-func-action@main
103-
with:
104-
version: knative-v1.19.1
105-
name: func
106-
107-
- name: Deploy function
108-
run: func deploy --remote`
109-
fnWorkflowYamlPath := filepath.Join(fnWorkflowDirPath, ciConfig.GithubWorkflowFile)
110-
if err := os.WriteFile(fnWorkflowYamlPath, []byte(workflowYamlContent), ciConfig.filePerm); err != nil {
38+
if err := ci.PersistToDisk(githubWorkflowAsYamlBytes, ciConfig.FnGithubWorkflowYamlPath(f.Root)); err != nil {
11139
return err
11240
}
11341

0 commit comments

Comments
 (0)