Skip to content

Commit 09e5e36

Browse files
Read password from terminal in automatic tag script (#339)
* Read password from terminal * Update scripts/automatic_tag.go Co-authored-by: João Palet <[email protected]> --------- Co-authored-by: João Palet <[email protected]>
1 parent 57742a8 commit 09e5e36

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

Makefile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,9 @@ test:
4040

4141
# AUTOMATIC TAG
4242
sdk-tag-services:
43-
@if [ "${password}" = "" ]; then \
44-
go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path}; \
45-
else \
46-
go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path} --password ${password}; \
47-
fi
43+
@go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path};
4844

4945

5046
sdk-tag-core:
51-
@if [ "${password}" = "" ]; then \
52-
go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path} --target core; \
53-
else \
54-
go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path} --target core --password ${password}; \
55-
fi
47+
@go run $(SCRIPTS_BASE)/automatic_tag.go --update-type ${update-type} --ssh-private-key-file-path ${ssh-private-key-file-path} --target core;
5648

scripts/automatic_tag.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"os"
88
"strconv"
99
"strings"
10+
"syscall"
1011

1112
"github.com/go-git/go-git/v5"
1213
"github.com/go-git/go-git/v5/config"
1314
"github.com/go-git/go-git/v5/plumbing"
1415
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
1516
"golang.org/x/mod/semver"
17+
"golang.org/x/term"
1618
)
1719

1820
const (
@@ -24,14 +26,13 @@ const (
2426

2527
updateTypeFlag = "update-type"
2628
sshPrivateKeyFilePathFlag = "ssh-private-key-file-path"
27-
passwordFlag = "password"
2829
targetFlag = "target"
2930
)
3031

3132
var (
3233
updateTypes = []string{minor, patch}
3334
targets = []string{allServices, core}
34-
usage = "go run automatic_tag.go --update-type [minor|patch] --ssh-private-key-file-path path/to/private-key --password password --target [all-services|core]"
35+
usage = "go run automatic_tag.go --update-type [minor|patch] --ssh-private-key-file-path path/to/private-key --target [all-services|core]"
3536
)
3637

3738
func main() {
@@ -44,12 +45,10 @@ func main() {
4445
func run() error {
4546
var updateType string
4647
var sshPrivateKeyFilePath string
47-
var password string
4848
var target string
4949

5050
flag.StringVar(&updateType, updateTypeFlag, "", fmt.Sprintf("Update type, must be one of: %s (required)", strings.Join(updateTypes, ",")))
5151
flag.StringVar(&sshPrivateKeyFilePath, sshPrivateKeyFilePathFlag, "", "Path to the ssh private key (required)")
52-
flag.StringVar(&password, passwordFlag, "", "Password of the ssh private key (optional)")
5352
flag.StringVar(&target, targetFlag, allServices, fmt.Sprintf("Create tags for this target, must be one of %s (optional, default is %s)", strings.Join(targets, ","), allServices))
5453

5554
flag.Parse()
@@ -81,13 +80,31 @@ func run() error {
8180
return fmt.Errorf("the provided private key file path %s is not valid: %w\nUsage: %s", sshPrivateKeyFilePath, err, usage)
8281
}
8382

83+
password, err := promptForPassword()
84+
if err != nil {
85+
return fmt.Errorf("prompt for password: %s", err.Error())
86+
}
87+
88+
fmt.Println("Starting automatic tag update...")
89+
8490
err = automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target)
8591
if err != nil {
86-
return fmt.Errorf("updating tags: %s", err.Error())
92+
return fmt.Errorf("update tags: %s", err.Error())
8793
}
8894
return nil
8995
}
9096

97+
// Prompts the user for the ssh key password.
98+
func promptForPassword() (string, error) {
99+
fmt.Print("Enter SSH key passphrase (empty for no passphrase): ")
100+
defer fmt.Print("\n")
101+
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
102+
if err != nil {
103+
return "", fmt.Errorf("read password: %w", err)
104+
}
105+
return string(bytePassword), nil
106+
}
107+
91108
// automaticTagUpdate goes through all of the existing tags, gets the latest for the target, creates a new one according to the updateType and pushes them
92109
func automaticTagUpdate(updateType, sshPrivateKeyFilePath, password, target string) error {
93110
tempDir, err := os.MkdirTemp("", "")

scripts/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/stackitcloud/stackit-sdk-go/scripts
22

33
go 1.18
44

5-
require github.com/go-git/go-git/v5 v5.11.0
5+
require (
6+
github.com/go-git/go-git/v5 v5.11.0
7+
golang.org/x/term v0.17.0
8+
)
69

710
require (
811
dario.cat/mergo v1.0.0 // indirect

scripts/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
104104
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
105105
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
106106
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
107+
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
107108
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
108109
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
109110
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

0 commit comments

Comments
 (0)