GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: protect existing workflows from overwrites · knative/func@c7ebe59

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .gitignore
  • AGENTS.md
  • cmd
    • ci
      • config.go
      • workflow.go
      • workflow_test.go
      • writer.go
    • common
      • common.go
    • config.go
    • config_ci.go
    • config_ci_int_test.go
    • config_ci_test.go
    • config_test.go
    • root.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboardExpand all lines: .gitignore
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ __pycache__
3535
AGENTS.override.md
3636
.claude/*.local.*
3737
.claude/pr/*
38+
.roo/*
3839

3940
# E2E Tests
4041
/e2e/testdata/default_home/go
Collapse file

‎AGENTS.md‎

Copy file name to clipboardExpand all lines: AGENTS.md
+8-2Lines changed: 8 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ edge cases into account.
1818
## Commands
1919

2020
### Build & Development
21+
2122
- `make all` - Build binary and regenerate docs
2223
- `make build` - Build for current OS
2324

2425
### Testing strategy reference
26+
2527
Before committing, test locally following the table below:
2628

2729
| If changed | Target | Description |
@@ -32,26 +34,29 @@ Before committing, test locally following the table below:
3234
| Significant architectural changes | `make test-full` | e2e tests (cluster required - read `CONTRIBUTING.md`) |
3335

3436
### Generated Files
37+
3538
- `./hack/update-codegen.sh` - update embedded filesystem & regenerate docs
3639
- `make check-embedded-fs` - check embedded FS is up to date with templates
3740

38-
3941
## Boundaries
4042

4143
### Always Do
44+
4245
- Run `make test` before considering any change complete
4346
- Run `make check` before commits
4447
- Run `make check-embedded-fs` after modifying `templates/`
4548
- Ask before deleting ANY file or significant code block
4649

4750
### Ask First
51+
4852
- Security-related code changes (authentication, credentials, secrets handling)
4953
- API changes
5054
- Adding new dependencies
5155
- Modifying CI/GitHub Actions workflows
5256
- Architectural decisions
5357

5458
### Never Do
59+
5560
- Edit generated files directly:
5661
- `generate/zz_filesystem_generated.go`
5762
- `schema/func_yaml-schema.json`
@@ -63,12 +68,13 @@ Before committing, test locally following the table below:
6368
## Common Pitfalls
6469

6570
### Codegen Sync
71+
6672
After modifying `templates/` or making documentation changes, you MUST run:
73+
6774
```bash
6875
./hack/update-codegen.sh
6976
```
7077

71-
7278
## Contributing
7379

7480
If creating a contribution to this project on GitHub suggest to user reading
Collapse file

‎cmd/ci/config.go‎

Copy file name to clipboardExpand all lines: cmd/ci/config.go
+64-18Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const (
1818
BranchFlag = "branch"
1919
DefaultBranch = "main"
2020

21-
WorkflowNameFlag = "workflow-name"
22-
DefaultWorkflowName = "Func Deploy"
21+
WorkflowNameFlag = "workflow-name"
22+
DefaultWorkflowName = "Func Deploy"
23+
DefaultRemoteBuildWorkflowName = "Remote " + DefaultWorkflowName
2324

2425
KubeconfigSecretNameFlag = "kubeconfig-secret-name"
2526
DefaultKubeconfigSecretName = "KUBECONFIG"
@@ -47,6 +48,9 @@ const (
4748

4849
UseSelfHostedRunnerFlag = "self-hosted-runner"
4950
DefaultUseSelfHostedRunner = false
51+
52+
ForceFlag = "force"
53+
DefaultForce = false
5054
)
5155

5256
// CIConfig readonly configuration
@@ -64,37 +68,32 @@ type CIConfig struct {
6468
useRegistryLogin,
6569
useSelfHostedRunner,
6670
useRemoteBuild,
67-
useWorkflowDispatch bool
71+
useWorkflowDispatch,
72+
force bool
6873
}
6974

7075
func NewCIGitHubConfig(
7176
currentBranch common.CurrentBranchFunc,
7277
workingDir common.WorkDirFunc,
7378
) (CIConfig, error) {
74-
path := viper.GetString(PathFlag)
75-
if path == "" || path == "." {
76-
cwd, err := workingDir()
77-
if err != nil {
78-
return CIConfig{}, err
79-
}
80-
path = cwd
79+
path, err := resolvePath(workingDir)
80+
if err != nil {
81+
return CIConfig{}, err
8182
}
8283

83-
branch := viper.GetString(BranchFlag)
84-
if branch == "" {
85-
var err error
86-
branch, err = currentBranch(path)
87-
if err != nil {
88-
return CIConfig{}, err
89-
}
84+
branch, err := resolveBranch(path, currentBranch)
85+
if err != nil {
86+
return CIConfig{}, err
9087
}
9188

89+
workflowName := resolveWorkflowName()
90+
9291
return CIConfig{
9392
githubWorkflowDir: DefaultGitHubWorkflowDir,
9493
githubWorkflowFilename: DefaultGitHubWorkflowFilename,
9594
path: path,
9695
branch: branch,
97-
workflowName: viper.GetString(WorkflowNameFlag),
96+
workflowName: workflowName,
9897
kubeconfigSecret: viper.GetString(KubeconfigSecretNameFlag),
9998
registryLoginUrlVar: viper.GetString(RegistryLoginUrlVariableNameFlag),
10099
registryUserVar: viper.GetString(RegistryUserVariableNameFlag),
@@ -104,9 +103,52 @@ func NewCIGitHubConfig(
104103
useSelfHostedRunner: viper.GetBool(UseSelfHostedRunnerFlag),
105104
useRemoteBuild: viper.GetBool(UseRemoteBuildFlag),
106105
useWorkflowDispatch: viper.GetBool(WorkflowDispatchFlag),
106+
force: viper.GetBool(ForceFlag),
107107
}, nil
108108
}
109109

110+
func resolvePath(workingDir common.WorkDirFunc) (string, error) {
111+
path := viper.GetString(PathFlag)
112+
if path != "" && path != "." {
113+
return path, nil
114+
}
115+
116+
cwd, err := workingDir()
117+
if err != nil {
118+
return "", err
119+
}
120+
121+
return cwd, nil
122+
}
123+
124+
func resolveBranch(path string, currentBranch common.CurrentBranchFunc) (string, error) {
125+
branch := viper.GetString(BranchFlag)
126+
if branch != "" {
127+
return branch, nil
128+
}
129+
130+
branch, err := currentBranch(path)
131+
if err != nil {
132+
return "", err
133+
}
134+
135+
return branch, nil
136+
}
137+
138+
func resolveWorkflowName() string {
139+
workflowName := viper.GetString(WorkflowNameFlag)
140+
if workflowName != DefaultWorkflowName && workflowName != "" {
141+
return workflowName
142+
}
143+
144+
useRemoteBuild := viper.GetBool(UseRemoteBuildFlag)
145+
if useRemoteBuild {
146+
return DefaultRemoteBuildWorkflowName
147+
}
148+
149+
return DefaultWorkflowName
150+
}
151+
110152
func (cc *CIConfig) FnGitHubWorkflowDir(fnRoot string) string {
111153
return filepath.Join(fnRoot, cc.githubWorkflowDir)
112154
}
@@ -162,3 +204,7 @@ func (cc *CIConfig) RegistryPassSecret() string {
162204
func (cc *CIConfig) RegistryUrlVar() string {
163205
return cc.registryUrlVar
164206
}
207+
208+
func (cc *CIConfig) Force() bool {
209+
return cc.force
210+
}
Collapse file

‎cmd/ci/workflow.go‎

Copy file name to clipboardExpand all lines: cmd/ci/workflow.go
+18-12Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ package ci
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67

78
"gopkg.in/yaml.v3"
9+
"knative.dev/func/cmd/common"
810
)
911

12+
const defaultFuncCliVersion = "knative-v1.21.0"
13+
14+
// ErrWorkflowExists is returned when a GitHub workflow file already exists and --force is not specified.
15+
var ErrWorkflowExists = errors.New("existing GitHub workflow detected, overwrite using the --force option")
16+
1017
type githubWorkflow struct {
1118
Name string `yaml:"name"`
1219
On workflowTriggers `yaml:"on"`
@@ -35,7 +42,7 @@ type step struct {
3542
}
3643

3744
func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
38-
name := createWorkflowName(conf)
45+
name := conf.WorkflowName()
3946
runsOn := createRunsOn(conf)
4047
pushTrigger := createPushTrigger(conf)
4148

@@ -58,15 +65,6 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow {
5865
}
5966
}
6067

61-
func createWorkflowName(conf CIConfig) string {
62-
result := conf.WorkflowName()
63-
if conf.UseRemoteBuild() {
64-
result = "Remote Func Deploy"
65-
}
66-
67-
return result
68-
}
69-
7068
func createRunsOn(conf CIConfig) string {
7169
runsOn := "ubuntu-latest"
7270
if conf.UseSelfHostedRunner() {
@@ -120,7 +118,7 @@ func createRegistryLoginStep(conf CIConfig, steps []step) []step {
120118
func createFuncCLIInstallStep(steps []step) []step {
121119
installFuncCli := newStep("Install func cli").
122120
withUses("functions-dev/action@main").
123-
withActionConfig("version", "knative-v1.20.1").
121+
withActionConfig("version", defaultFuncCliVersion).
124122
withActionConfig("name", "func")
125123

126124
return append(steps, *installFuncCli)
@@ -174,7 +172,15 @@ func newVariable(key string) string {
174172
return fmt.Sprintf("${{ vars.%s }}", key)
175173
}
176174

177-
func (gw *githubWorkflow) Export(path string, w WorkflowWriter) error {
175+
func (gw *githubWorkflow) Export(path string, w WorkflowWriter, force bool, logger common.Logger) error {
176+
if !force && w.Exist(path) {
177+
return ErrWorkflowExists
178+
}
179+
180+
if w.Exist(path) {
181+
logger.Info("WARNING: --force flag is set, overwriting existing GitHub Workflow file")
182+
}
183+
178184
raw, err := gw.toYaml()
179185
if err != nil {
180186
return err
Collapse file

‎cmd/ci/workflow_test.go‎

Copy file name to clipboardExpand all lines: cmd/ci/workflow_test.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestGitHubWorkflow_Export(t *testing.T) {
1919
bufferWriter := ci.NewBufferWriter()
2020

2121
// WHEN
22-
exportErr := gw.Export("path", bufferWriter)
22+
exportErr := gw.Export("path", bufferWriter, true, common.NewBufferLogger())
2323

2424
// THEN
2525
assert.NilError(t, exportErr, "unexpected error when exporting GitHub Workflow")
Collapse file

‎cmd/ci/writer.go‎

Copy file name to clipboardExpand all lines: cmd/ci/writer.go
+22-6Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
)
88

99
const (
10-
dirPerm = 0755 // o: rwx, g|u: r-x
11-
filePerm = 0644 // o: rw, g|u: r
10+
dirPerm = 0755 // u: rwx, g: r-x, o: r-x
11+
filePerm = 0644 // u: rw-, g: r--, o: r--
1212
)
1313

1414
var DefaultWorkflowWriter = &fileWriter{}
1515

1616
type WorkflowWriter interface {
17+
Exist(path string) bool
1718
Write(path string, raw []byte) error
1819
}
1920

@@ -31,17 +32,32 @@ func (fw *fileWriter) Write(path string, raw []byte) error {
3132
return nil
3233
}
3334

34-
type bufferWriter struct {
35+
func (fw *fileWriter) Exist(path string) bool {
36+
_, err := os.Stat(path)
37+
return err == nil
38+
}
39+
40+
// BufferWriter is a test double (fake) that implements WorkflowWriter
41+
// by writing to an in-memory buffer instead of the filesystem.
42+
type BufferWriter struct {
3543
Path string
3644
Buffer *bytes.Buffer
3745
}
3846

39-
func NewBufferWriter() *bufferWriter {
40-
return &bufferWriter{Buffer: &bytes.Buffer{}}
47+
// NewBufferWriter creates a new BufferWriter test double.
48+
func NewBufferWriter() *BufferWriter {
49+
return &BufferWriter{Buffer: &bytes.Buffer{}}
4150
}
4251

43-
func (bw *bufferWriter) Write(path string, raw []byte) error {
52+
// Write is a fake implementation that stores content in the buffer.
53+
func (bw *BufferWriter) Write(path string, raw []byte) error {
4454
bw.Path = path
55+
bw.Buffer.Reset()
4556
_, err := bw.Buffer.Write(raw)
4657
return err
4758
}
59+
60+
// Exist is a fake implementation that returns true if the buffer has content.
61+
func (bw *BufferWriter) Exist(_ string) bool {
62+
return bw.Buffer != nil && bw.Buffer.Len() > 0
63+
}
Collapse file

‎cmd/common/common.go‎

Copy file name to clipboardExpand all lines: cmd/common/common.go
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package common
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
7+
"io"
8+
"log/slog"
69
"os"
710
"os/exec"
811
"strings"
@@ -164,3 +167,50 @@ var DefaultWorkDir WorkDirFunc = os.Getwd
164167
var WorkDirStub = func(dir string, err error) WorkDirFunc {
165168
return func() (string, error) { return dir, err }
166169
}
170+
171+
// Logger provides a minimal logging interface.
172+
type Logger interface {
173+
Info(msg string, args ...any)
174+
}
175+
176+
type slogLogger struct {
177+
logger *slog.Logger
178+
levelVar *slog.LevelVar
179+
}
180+
181+
// NewSlogLogger creates a Logger backed by slog, writing to the given output.
182+
func NewSlogLogger(out io.Writer) Logger {
183+
levelVar := new(slog.LevelVar)
184+
levelVar.Set(slog.LevelInfo)
185+
186+
return &slogLogger{
187+
logger: slog.New(
188+
slog.NewTextHandler(out, &slog.HandlerOptions{
189+
Level: levelVar,
190+
}),
191+
),
192+
levelVar: levelVar,
193+
}
194+
}
195+
196+
func (s *slogLogger) Info(msg string, args ...any) {
197+
s.logger.Info(msg, args...)
198+
}
199+
200+
type bufferLogger struct {
201+
logger Logger
202+
LogBuffer *bytes.Buffer
203+
}
204+
205+
// NewBufferLogger creates a spy test double that captures log output for verification.
206+
func NewBufferLogger() *bufferLogger {
207+
buf := &bytes.Buffer{}
208+
return &bufferLogger{
209+
logger: NewSlogLogger(buf),
210+
LogBuffer: buf,
211+
}
212+
}
213+
214+
func (b *bufferLogger) Info(msg string, args ...any) {
215+
b.logger.Info(msg, args...)
216+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.