Skip to content

Commit

Permalink
cdep: prompt on branch override (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
julesjcraske authored Apr 10, 2024
1 parent f7a21b1 commit 1eb088b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
19 changes: 15 additions & 4 deletions tools/cdep/app/add_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func (a App) AddToConfig(path, branchName, commitHash string) (bool, error) {
}

func (a App) doJsonUpdates(path string, branchName string, commitHash string, blob []byte) []byte {
attemptWarnIfOverride(blob, branchRegAdd, branchName, path)
override := promptIfOverride(blob, branchRegAdd, branchName, path)
if !override {
return blob
}

blob = attemptUpdate(blob, commitRegAdd, "commit", commitHash)
blob = attemptUpdate(blob, branchRegAdd, "branch", branchName)
Expand All @@ -68,7 +71,10 @@ func (a App) doJsonUpdates(path string, branchName string, commitHash string, bl
}

func (a App) doYamlUpdates(path string, branchName string, commitHash string, blob []byte) []byte {
attemptWarnIfOverride(blob, branchRegAddYaml, branchName, path)
override := promptIfOverride(blob, branchRegAddYaml, branchName, path)
if !override {
return blob
}

imagePrefix := "master"
if branchName != "master" {
Expand Down Expand Up @@ -133,16 +139,21 @@ func attemptUpdateYaml(blob []byte, reg *regexp.Regexp, key, value string) []byt
return blob
}

func attemptWarnIfOverride(blob []byte, reg *regexp.Regexp, newBranch, path string) {
func promptIfOverride(blob []byte, reg *regexp.Regexp, newBranch, path string) bool {
matches := reg.FindSubmatch(blob)

if len(matches) <= 1 {
return
return true
}

branchName := string(matches[1])

if branchName != "master" && branchName != newBranch {
log.Warnf("Custom branch is going to be overriden, old: %s, new: %s, path: %s", branchName, newBranch, path)
override := boolPrompt("Override branch?")

return override
}

return true
}
22 changes: 22 additions & 0 deletions tools/cdep/app/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app

import (
"bufio"
"fmt"
"os"
"strings"
)

// boolPrompt asks for a bool value using the label
func boolPrompt(label string) bool {
var s string
r := bufio.NewReader(os.Stdin)
for {
fmt.Fprint(os.Stderr, label+" (y/n) ")
s, _ = r.ReadString('\n')
if s != "" {
break
}
}
return strings.TrimSpace(s) == "y"
}

0 comments on commit 1eb088b

Please sign in to comment.