Skip to content

Commit fd3a387

Browse files
Ask for confirmation (#341)
1 parent 3841a13 commit fd3a387

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

scripts/automatic_tag.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"errors"
56
"flag"
67
"fmt"
@@ -85,7 +86,7 @@ func run() error {
8586
return fmt.Errorf("prompt for password: %s", err.Error())
8687
}
8788

88-
fmt.Println("Starting automatic tag update...")
89+
fmt.Print("Starting automatic tag update...\n\n")
8990

9091
err = automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target)
9192
if err != nil {
@@ -133,6 +134,8 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
133134
return fmt.Errorf("clone SDK repo: %w", err)
134135
}
135136

137+
fmt.Printf("Cloned %s successfully\n\n", sdkRepo)
138+
136139
tagrefs, err := r.Tags()
137140
if err != nil {
138141
return fmt.Errorf("get tags: %w", err)
@@ -150,6 +153,7 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
150153
return fmt.Errorf("iterate over existing tags: %w", err)
151154
}
152155

156+
var newTagsList []string
153157
for module, version := range latestTags {
154158
updatedVersion, err := computeUpdatedVersion(version, updateType)
155159
if err != nil {
@@ -171,6 +175,16 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
171175
continue
172176
}
173177

178+
newTagsList = append(newTagsList, newTag)
179+
}
180+
181+
fmt.Printf("The following tags will be created:\n%s\n\n", strings.Join(newTagsList, "\n"))
182+
err = promptForConfirmation("Do you want to continue?")
183+
if err != nil {
184+
return fmt.Errorf("ask for confirmation: %w", err)
185+
}
186+
187+
for _, newTag := range newTagsList {
174188
err = createTag(r, newTag)
175189
if err != nil {
176190
fmt.Printf("Create tag %s returned error: %s\n", newTag, err)
@@ -183,6 +197,7 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
183197
if err != nil {
184198
return fmt.Errorf("push tags: %w", err)
185199
}
200+
fmt.Print("\nTags were pushed successfully!\n")
186201
return nil
187202
}
188203

@@ -262,6 +277,30 @@ func computeUpdatedVersion(version, updateType string) (string, error) {
262277
return updatedVersion, nil
263278
}
264279

280+
// Prompts for confirmation.
281+
//
282+
// Returns nil only if the user (explicitly) answers positive.
283+
// Returns error if the user answers negative.
284+
func promptForConfirmation(prompt string) error {
285+
question := fmt.Sprintf("%s [y/N] ", prompt)
286+
reader := bufio.NewReader(os.Stdin)
287+
for i := 0; i < 3; i++ {
288+
fmt.Print(question)
289+
answer, err := reader.ReadString('\n')
290+
if err != nil {
291+
continue
292+
}
293+
answer = strings.ToLower(strings.TrimSpace(answer))
294+
if answer == "y" || answer == "yes" {
295+
return nil
296+
}
297+
if answer == "" || answer == "n" || answer == "no" {
298+
return errors.New("execution aborted")
299+
}
300+
}
301+
return fmt.Errorf("max number of wrong inputs")
302+
}
303+
265304
func createTag(r *git.Repository, tag string) error {
266305
h, err := r.Head()
267306
if err != nil {

0 commit comments

Comments
 (0)