1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"errors"
5
6
"flag"
6
7
"fmt"
@@ -85,7 +86,7 @@ func run() error {
85
86
return fmt .Errorf ("prompt for password: %s" , err .Error ())
86
87
}
87
88
88
- fmt .Println ("Starting automatic tag update..." )
89
+ fmt .Print ("Starting automatic tag update...\n \n " )
89
90
90
91
err = automaticTagUpdate (updateType , sshPrivateKeyFilePath , password , target )
91
92
if err != nil {
@@ -133,6 +134,8 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
133
134
return fmt .Errorf ("clone SDK repo: %w" , err )
134
135
}
135
136
137
+ fmt .Printf ("Cloned %s successfully\n \n " , sdkRepo )
138
+
136
139
tagrefs , err := r .Tags ()
137
140
if err != nil {
138
141
return fmt .Errorf ("get tags: %w" , err )
@@ -150,6 +153,7 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
150
153
return fmt .Errorf ("iterate over existing tags: %w" , err )
151
154
}
152
155
156
+ var newTagsList []string
153
157
for module , version := range latestTags {
154
158
updatedVersion , err := computeUpdatedVersion (version , updateType )
155
159
if err != nil {
@@ -171,6 +175,16 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
171
175
continue
172
176
}
173
177
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 {
174
188
err = createTag (r , newTag )
175
189
if err != nil {
176
190
fmt .Printf ("Create tag %s returned error: %s\n " , newTag , err )
@@ -183,6 +197,7 @@ func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target stri
183
197
if err != nil {
184
198
return fmt .Errorf ("push tags: %w" , err )
185
199
}
200
+ fmt .Print ("\n Tags were pushed successfully!\n " )
186
201
return nil
187
202
}
188
203
@@ -262,6 +277,30 @@ func computeUpdatedVersion(version, updateType string) (string, error) {
262
277
return updatedVersion , nil
263
278
}
264
279
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
+
265
304
func createTag (r * git.Repository , tag string ) error {
266
305
h , err := r .Head ()
267
306
if err != nil {
0 commit comments