GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Feature: Protect existing workflow files from accidental overwrites. by twoGiants · Pull Request #3407 · knative/func Conversation
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## main #3407 +/- ## ========================================== - Coverage 54.60% 54.54% -0.06% ========================================== Files 179 179 Lines 20228 20282 +54 ========================================== + Hits 11045 11063 +18 - Misses 8011 8046 +35 - Partials 1172 1173 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
8162643 to
8109c29
Compare
|
/cc @gauron99 @matejvasek @lkingland The PR is ready for review. Please read the description about the logging. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
/lgtm |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
The codecov tool is poorly configured or am I missing something? It's flagging the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
8109c29 to
6fb5f5e
Compare
| func resolveWorkflowName() string { | ||
| workflowName := viper.GetString(WorkflowNameFlag) | ||
| if workflowName != DefaultWorkflowName && workflowName != "" { | ||
| return workflowName | ||
| } | ||
|
|
||
| useRemoteBuild := viper.GetBool(UseRemoteBuildFlag) | ||
| if useRemoteBuild { | ||
| return DefaultRemoteBuildWorkflowName | ||
| } | ||
|
|
||
| return DefaultWorkflowName | ||
| } |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
So if I understand this right, if a user explicitly sets workflow-name="Func Deploy" (the default value), the function treats it as if no custom name was provided because of the workflowName != DefaultWorkflowName check. This means with --remote --workflow-name="Func Deploy", we'd get "Remote Func Deploy" instead of their explicitly requested "Func Deploy"?
Something like this might help:
workflowNameChanged := cmd.Flags().Changed(WorkflowNameFlag)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch! I was also unsure about this one. Thank you. Will correct it 😸 👍
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am setting the default during resolution, the I don't need to pass down cobra.Command. Is that ok too?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
After talking to David I got a small primer about how default values are set and it's probably best to evaluate the cmd.Flags().Changed(WorkflowNameFlag) and do what you proposed Luke 😺
Sorry, something went wrong.
There was an error while loading. Please reload this page.
6fb5f5e to
09b4258
Compare
09b4258 to
332f403
Compare
b055aaa to
ffadf68
Compare
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Just thinking out loud. Just matter of taste: To me it feels more "atomic" as it does not do separate diff --git a/cmd/ci/workflow.go b/cmd/ci/workflow.go index c69c64b45..5382829fa 100644 --- a/cmd/ci/workflow.go +++ b/cmd/ci/workflow.go @@ -173,21 +173,22 @@ func newVariable(key string) string { } func (gw *githubWorkflow) Export(path string, w WorkflowWriter, force bool, m io.Writer) error { - if !force && w.Exist(path) { - return ErrWorkflowExists - } - - if w.Exist(path) { - // best-effort user message; errors are non-critical - _, _ = fmt.Fprintf(m, "WARNING: --force flag is set, overwriting existing GitHub Workflow file\n") + raw, err := gw.toYaml() + if err != nil { + return err } - raw, err := gw.toYaml() + err = w.Write(path, raw, force) if err != nil { return err } - return w.Write(path, raw) + if force { + // best-effort user message; errors are non-critical + _, _ = fmt.Fprintf(m, "WARNING: --force flag is set, overwriting existing GitHub Workflow file\n") + } + + return nil } func (gw *githubWorkflow) toYaml() ([]byte, error) { diff --git a/cmd/ci/writer.go b/cmd/ci/writer.go index f15be50b1..4c7a159a4 100644 --- a/cmd/ci/writer.go +++ b/cmd/ci/writer.go @@ -16,28 +16,39 @@ var DefaultWorkflowWriter = &fileWriter{} // WorkflowWriter defines the interface for writing workflow files. type WorkflowWriter interface { - Exist(path string) bool - Write(path string, raw []byte) error + Write(path string, raw []byte, override bool) 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 override is false and the file already exists, ErrWorkflowExists is returned. +func (fw *fileWriter) Write(path string, raw []byte, override bool) error { if err := os.MkdirAll(filepath.Dir(path), dirPerm); err != nil { return err } - if err := os.WriteFile(path, raw, filePerm); err != nil { + flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL + + if override { + // clear exclusive flag + flags &= ^os.O_EXCL + } + + f, err := os.OpenFile(path, flags, filePerm) + if err != nil { + if os.IsExist(err) { + return ErrWorkflowExists + } return err } + defer f.Close() - return nil -} + if _, err := f.Write(raw); err != nil { + return err + } -func (fw *fileWriter) Exist(path string) bool { - _, err := os.Stat(path) - return err == nil + return nil } // BufferWriter is a test double (fake) that implements WorkflowWriter @@ -53,14 +64,14 @@ func NewBufferWriter() *BufferWriter { } // Write is a fake implementation that stores content in the buffer. -func (bw *BufferWriter) Write(path string, raw []byte) error { +// If override is false and the buffer has content, ErrWorkflowExists is returned. +func (bw *BufferWriter) Write(path string, raw []byte, override bool) error { + if !override && bw.Buffer != nil && bw.Buffer.Len() > 0 { + return ErrWorkflowExists + } + bw.Path = path bw.Buffer.Reset() _, err := bw.Buffer.Write(raw) return err } - -// Exist is a fake implementation that returns true if the buffer has content. -func (bw *BufferWriter) Exist(_ string) bool { - return bw.Buffer != nil && bw.Buffer.Len() > 0 -} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
/approve |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: matejvasek, twoGiants The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Great comment! 😺 👍 I like that you bring that up. I had the same idea when I started coding but then changed my mind. Here is my path of thoughts I was the following: To finish up and sum up my heuristic => I would prefer to keep business logic and infrastructure logic as separate as possible (not always possible I know). So if I would take my heuristic to the "extreme" I'd decompose Now something else. In your patch this check should only happen when force is set and there is a file. So an accidental setting of force could create a bit of confusion and print a warning where none is needed: + if force { + // best-effort user message; errors are non-critical + _, _ = fmt.Fprintf(m, "WARNING: --force flag is set, overwriting existing GitHub Workflow file\n") + }
Do you think this could be an issue? They would also have to be writing in the same directory. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
The `func config ci` command previously overwrote existing workflow
files silently, risking loss of user customizations.
Now the command checks if a workflow file exists before writing and
returns an error unless the `--force` flag is specified. When --force
is used and a file exists, a warning is logged.
Remote builds now use a distinct default workflow name ("Remote Func
Deploy") to avoid conflicts, while explicit --workflow-name values
are always preserved regardless of --remote flag.
Issue knative#3256
Signed-off-by: Stanislav Jakuschevskij <[email protected]>
ffadf68 to
c6880ed
Compare
|
/lgtm |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
/lgtm |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.