Skip to content

Commit

Permalink
ensure gh az clis (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamero authored Nov 11, 2024
1 parent ea0fc31 commit f6c230c
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 81 deletions.
3 changes: 3 additions & 0 deletions cmd/setup-gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ application and service principle, and will configure that application to trust
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

providers.EnsureAzCli()
providers.EnsureGhCli()

azCred, err := cred.GetCred()
if err != nil {
return fmt.Errorf("getting credentials: %w", err)
Expand Down
67 changes: 13 additions & 54 deletions pkg/providers/providersutils.go → pkg/providers/azcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
log "github.com/sirupsen/logrus"
)

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

func GetAzCliVersion() string {
Expand Down Expand Up @@ -56,7 +57,7 @@ func upgradeAzCli() {
log.Info("Azure CLI upgrade was successful!")
}

func CheckAzCliInstalled() {
func EnsureAzCliInstalled() {
log.Debug("Checking that Azure Cli is installed...")
azCmd := exec.Command("az")
_, err := azCmd.CombinedOutput()
Expand Down Expand Up @@ -93,45 +94,13 @@ func IsLoggedInToAz() bool {
return true
}

func HasGhCli() bool {
log.Debug("Checking that github cli is installed...")
ghCmd := exec.Command("gh")
_, err := ghCmd.CombinedOutput()
if err != nil {
log.Fatal("Error: The github cli is required to complete this process. Find installation instructions at this link: https://github.com/cli/cli#installation")
return false
}

log.Debug("Github cli found!")
return true
}

func IsLoggedInToGh() bool {
log.Debug("Checking that user is logged in to github...")
ghCmd := exec.Command("gh", "auth", "status")
out, err := ghCmd.CombinedOutput()
if err != nil {
fmt.Printf(string(out))
return false
}

log.Debug("User is logged in!")
return true

}

func LogInToGh() error {
log.Debug("Logging user in to github...")
ghCmd := exec.Command("gh", "auth", "login")
ghCmd.Stdin = os.Stdin
ghCmd.Stdout = os.Stdout
ghCmd.Stderr = os.Stderr
err := ghCmd.Run()
if err != nil {
return err
func EnsureAzCliLoggedIn() {
EnsureAzCliInstalled()
if !IsLoggedInToAz() {
if err := LogInToAz(); err != nil {
log.Fatal("Error: unable to log in to Azure")
}
}

return nil
}

func LogInToAz() error {
Expand Down Expand Up @@ -200,16 +169,6 @@ func isValidResourceGroup(
return nil
}

func isValidGhRepo(repo string) error {
listReposCmd := exec.Command("gh", "repo", "view", repo)
_, err := listReposCmd.CombinedOutput()
if err != nil {
log.Fatal("Github repo not found")
return err
}
return nil
}

func AzAppExists(appName string) bool {
filter := fmt.Sprintf("displayName eq '%s'", appName)
checkAppExistsCmd := exec.Command("az", "ad", "app", "list", "--only-show-errors", "--filter", filter, "--query", "[].appId")
Expand Down Expand Up @@ -269,7 +228,7 @@ func AzAksExists(aksName string, resourceGroup string) bool {
}

func GetCurrentAzSubscriptionLabel() (SubLabel, error) {
CheckAzCliInstalled()
EnsureAzCliInstalled()
if !IsLoggedInToAz() {
if err := LogInToAz(); err != nil {
return SubLabel{}, fmt.Errorf("failed to log in to Azure CLI: %v", err)
Expand All @@ -293,7 +252,7 @@ func GetCurrentAzSubscriptionLabel() (SubLabel, error) {
}

func GetAzSubscriptionLabels() ([]SubLabel, error) {
CheckAzCliInstalled()
EnsureAzCliInstalled()
if !IsLoggedInToAz() {
if err := LogInToAz(); err != nil {
return nil, fmt.Errorf("failed to log in to Azure CLI: %v", err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/providers/azcli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package providers

import (
"testing"
)

func TestCheckAzCliInstalled(t *testing.T) {
EnsureAzCliInstalled()
}
15 changes: 6 additions & 9 deletions pkg/providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
"github.com/google/uuid"
"os/exec"
"time"

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

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

if !HasGhCli() || !IsLoggedInToGh() {
s.Stop()
if err := LogInToGh(); err != nil {
return err
}
s.Start()
}
EnsureGhCliInstalled()
EnsureGhCliLoggedIn()
s.Start()

if err := sc.ValidateSetUpConfig(); err != nil {
return err
Expand Down
78 changes: 78 additions & 0 deletions pkg/providers/ghcli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package providers

import (
"fmt"
"os"
"os/exec"

log "github.com/sirupsen/logrus"
)

type SubLabel struct {
ID string `json:"id"`
Name string `json:"name"`
}

// EnsureGhCliInstalled ensures that the Github CLI is installed and the user is logged in
func EnsureGhCli() {
EnsureGhCliInstalled()
EnsureGhCliLoggedIn()
}

func EnsureGhCliInstalled() {
log.Debug("Checking that github cli is installed...")
ghCmd := exec.Command("gh")
_, err := ghCmd.CombinedOutput()
if err != nil {
log.Fatal("Error: The github cli is required to complete this process. Find installation instructions at this link: https://github.com/cli/cli#installation")
}

log.Debug("Github cli found!")
}

func EnsureGhCliLoggedIn() {
EnsureGhCliInstalled()
if !IsLoggedInToGh() {
if err := LogInToGh(); err != nil {
log.Fatal("Error: unable to log in to github")
}
}
}

func IsLoggedInToGh() bool {
log.Debug("Checking that user is logged in to github...")
ghCmd := exec.Command("gh", "auth", "status")
out, err := ghCmd.CombinedOutput()
if err != nil {
fmt.Printf(string(out))
return false
}

log.Debug("User is logged in!")
return true

}

func LogInToGh() error {
log.Debug("Logging user in to github...")
ghCmd := exec.Command("gh", "auth", "login")
ghCmd.Stdin = os.Stdin
ghCmd.Stdout = os.Stdout
ghCmd.Stderr = os.Stderr
err := ghCmd.Run()
if err != nil {
return err
}

return nil
}

func isValidGhRepo(repo string) error {
listReposCmd := exec.Command("gh", "repo", "view", repo)
_, err := listReposCmd.CombinedOutput()
if err != nil {
log.Fatal("Github repo not found")
return err
}
return nil
}
9 changes: 9 additions & 0 deletions pkg/providers/ghcli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package providers

import (
"testing"
)

func TestHasGhCli(t *testing.T) {
EnsureGhCliInstalled()
}
18 changes: 0 additions & 18 deletions pkg/providers/providersutils_test.go

This file was deleted.

0 comments on commit f6c230c

Please sign in to comment.