Skip to content

Commit f6c230c

Browse files
authored
ensure gh az clis (#418)
1 parent ea0fc31 commit f6c230c

File tree

7 files changed

+118
-81
lines changed

7 files changed

+118
-81
lines changed

cmd/setup-gh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ application and service principle, and will configure that application to trust
3030
RunE: func(cmd *cobra.Command, args []string) error {
3131
ctx := cmd.Context()
3232

33+
providers.EnsureAzCli()
34+
providers.EnsureGhCli()
35+
3336
azCred, err := cred.GetCred()
3437
if err != nil {
3538
return fmt.Errorf("getting credentials: %w", err)
Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
log "github.com/sirupsen/logrus"
1313
)
1414

15-
type SubLabel struct {
16-
ID string `json:"id"`
17-
Name string `json:"name"`
15+
// EnsureAzCli ensures that the Azure CLI is installed and the user is logged in
16+
func EnsureAzCli() {
17+
EnsureAzCliInstalled()
18+
EnsureAzCliLoggedIn()
1819
}
1920

2021
func GetAzCliVersion() string {
@@ -56,7 +57,7 @@ func upgradeAzCli() {
5657
log.Info("Azure CLI upgrade was successful!")
5758
}
5859

59-
func CheckAzCliInstalled() {
60+
func EnsureAzCliInstalled() {
6061
log.Debug("Checking that Azure Cli is installed...")
6162
azCmd := exec.Command("az")
6263
_, err := azCmd.CombinedOutput()
@@ -93,45 +94,13 @@ func IsLoggedInToAz() bool {
9394
return true
9495
}
9596

96-
func HasGhCli() bool {
97-
log.Debug("Checking that github cli is installed...")
98-
ghCmd := exec.Command("gh")
99-
_, err := ghCmd.CombinedOutput()
100-
if err != nil {
101-
log.Fatal("Error: The github cli is required to complete this process. Find installation instructions at this link: https://github.com/cli/cli#installation")
102-
return false
103-
}
104-
105-
log.Debug("Github cli found!")
106-
return true
107-
}
108-
109-
func IsLoggedInToGh() bool {
110-
log.Debug("Checking that user is logged in to github...")
111-
ghCmd := exec.Command("gh", "auth", "status")
112-
out, err := ghCmd.CombinedOutput()
113-
if err != nil {
114-
fmt.Printf(string(out))
115-
return false
116-
}
117-
118-
log.Debug("User is logged in!")
119-
return true
120-
121-
}
122-
123-
func LogInToGh() error {
124-
log.Debug("Logging user in to github...")
125-
ghCmd := exec.Command("gh", "auth", "login")
126-
ghCmd.Stdin = os.Stdin
127-
ghCmd.Stdout = os.Stdout
128-
ghCmd.Stderr = os.Stderr
129-
err := ghCmd.Run()
130-
if err != nil {
131-
return err
97+
func EnsureAzCliLoggedIn() {
98+
EnsureAzCliInstalled()
99+
if !IsLoggedInToAz() {
100+
if err := LogInToAz(); err != nil {
101+
log.Fatal("Error: unable to log in to Azure")
102+
}
132103
}
133-
134-
return nil
135104
}
136105

137106
func LogInToAz() error {
@@ -200,16 +169,6 @@ func isValidResourceGroup(
200169
return nil
201170
}
202171

203-
func isValidGhRepo(repo string) error {
204-
listReposCmd := exec.Command("gh", "repo", "view", repo)
205-
_, err := listReposCmd.CombinedOutput()
206-
if err != nil {
207-
log.Fatal("Github repo not found")
208-
return err
209-
}
210-
return nil
211-
}
212-
213172
func AzAppExists(appName string) bool {
214173
filter := fmt.Sprintf("displayName eq '%s'", appName)
215174
checkAppExistsCmd := exec.Command("az", "ad", "app", "list", "--only-show-errors", "--filter", filter, "--query", "[].appId")
@@ -269,7 +228,7 @@ func AzAksExists(aksName string, resourceGroup string) bool {
269228
}
270229

271230
func GetCurrentAzSubscriptionLabel() (SubLabel, error) {
272-
CheckAzCliInstalled()
231+
EnsureAzCliInstalled()
273232
if !IsLoggedInToAz() {
274233
if err := LogInToAz(); err != nil {
275234
return SubLabel{}, fmt.Errorf("failed to log in to Azure CLI: %v", err)
@@ -293,7 +252,7 @@ func GetCurrentAzSubscriptionLabel() (SubLabel, error) {
293252
}
294253

295254
func GetAzSubscriptionLabels() ([]SubLabel, error) {
296-
CheckAzCliInstalled()
255+
EnsureAzCliInstalled()
297256
if !IsLoggedInToAz() {
298257
if err := LogInToAz(); err != nil {
299258
return nil, fmt.Errorf("failed to log in to Azure CLI: %v", err)

pkg/providers/azcli_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package providers
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCheckAzCliInstalled(t *testing.T) {
8+
EnsureAzCliInstalled()
9+
}

pkg/providers/azure.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"os/exec"
9+
"time"
10+
811
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
912
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
1013
"github.com/google/uuid"
11-
"os/exec"
12-
"time"
1314

1415
"github.com/Azure/draft/pkg/spinner"
1516

@@ -33,13 +34,9 @@ type SetUpCmd struct {
3334
func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner) error {
3435
log.Debug("Commencing github connection with azure...")
3536

36-
if !HasGhCli() || !IsLoggedInToGh() {
37-
s.Stop()
38-
if err := LogInToGh(); err != nil {
39-
return err
40-
}
41-
s.Start()
42-
}
37+
EnsureGhCliInstalled()
38+
EnsureGhCliLoggedIn()
39+
s.Start()
4340

4441
if err := sc.ValidateSetUpConfig(); err != nil {
4542
return err

pkg/providers/ghcli.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package providers
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
type SubLabel struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
}
15+
16+
// EnsureGhCliInstalled ensures that the Github CLI is installed and the user is logged in
17+
func EnsureGhCli() {
18+
EnsureGhCliInstalled()
19+
EnsureGhCliLoggedIn()
20+
}
21+
22+
func EnsureGhCliInstalled() {
23+
log.Debug("Checking that github cli is installed...")
24+
ghCmd := exec.Command("gh")
25+
_, err := ghCmd.CombinedOutput()
26+
if err != nil {
27+
log.Fatal("Error: The github cli is required to complete this process. Find installation instructions at this link: https://github.com/cli/cli#installation")
28+
}
29+
30+
log.Debug("Github cli found!")
31+
}
32+
33+
func EnsureGhCliLoggedIn() {
34+
EnsureGhCliInstalled()
35+
if !IsLoggedInToGh() {
36+
if err := LogInToGh(); err != nil {
37+
log.Fatal("Error: unable to log in to github")
38+
}
39+
}
40+
}
41+
42+
func IsLoggedInToGh() bool {
43+
log.Debug("Checking that user is logged in to github...")
44+
ghCmd := exec.Command("gh", "auth", "status")
45+
out, err := ghCmd.CombinedOutput()
46+
if err != nil {
47+
fmt.Printf(string(out))
48+
return false
49+
}
50+
51+
log.Debug("User is logged in!")
52+
return true
53+
54+
}
55+
56+
func LogInToGh() error {
57+
log.Debug("Logging user in to github...")
58+
ghCmd := exec.Command("gh", "auth", "login")
59+
ghCmd.Stdin = os.Stdin
60+
ghCmd.Stdout = os.Stdout
61+
ghCmd.Stderr = os.Stderr
62+
err := ghCmd.Run()
63+
if err != nil {
64+
return err
65+
}
66+
67+
return nil
68+
}
69+
70+
func isValidGhRepo(repo string) error {
71+
listReposCmd := exec.Command("gh", "repo", "view", repo)
72+
_, err := listReposCmd.CombinedOutput()
73+
if err != nil {
74+
log.Fatal("Github repo not found")
75+
return err
76+
}
77+
return nil
78+
}

pkg/providers/ghcli_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package providers
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHasGhCli(t *testing.T) {
8+
EnsureGhCliInstalled()
9+
}

pkg/providers/providersutils_test.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)