@@ -4,10 +4,11 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "os"
8+ "path/filepath"
79 "strings"
810
9- "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
10- "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
11+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
1112 "github.com/Azure/draft/pkg/cred"
1213 "github.com/Azure/draft/pkg/prompts"
1314 "github.com/manifoldco/promptui"
@@ -34,23 +35,11 @@ application and service principle, and will configure that application to trust
3435 providers .EnsureGhCli ()
3536
3637 azCred , err := cred .GetCred ()
38+ az , err := providers .NewAzClient (azCred )
3739 if err != nil {
3840 return fmt .Errorf ("getting credentials: %w" , err )
3941 }
40-
41- client , err := armsubscription .NewTenantsClient (azCred , nil )
42- if err != nil {
43- return fmt .Errorf ("creating tenants client: %w" , err )
44- }
45-
46- sc .AzClient .AzTenantClient = client
47-
48- roleAssignmentClient , err := armauthorization .NewRoleAssignmentsClient (sc .SubscriptionID , azCred , nil )
49- if err != nil {
50- return fmt .Errorf ("getting role assignment client: %w" , err )
51- }
52-
53- sc .AzClient .RoleAssignClient = roleAssignmentClient
42+ sc .AzClient = az
5443
5544 err = fillSetUpConfig (sc )
5645 if err != nil {
@@ -82,8 +71,26 @@ application and service principle, and will configure that application to trust
8271}
8372
8473func fillSetUpConfig (sc * providers.SetUpCmd ) error {
74+ if sc .TenantId == "" {
75+ tenandId , err := providers .PromptTenantId (sc .AzClient , context .Background ())
76+ if err != nil {
77+ return fmt .Errorf ("prompting tenant ID: %w" , err )
78+ }
79+ sc .TenantId = tenandId
80+ }
81+
8582 if sc .AppName == "" {
86- sc .AppName = getAppName ()
83+ // make the default app name the current directory name plus "workflow" and a string
84+
85+ // get the current directory name
86+ dir , err := os .Getwd ()
87+ if err != nil {
88+ return fmt .Errorf ("getting current directory: %w" , err )
89+ }
90+ dirBase := filepath .Base (dir )
91+ defaultAppName := dirBase + "-workflow"
92+
93+ sc .AppName = PromptAppName (sc .AzClient , defaultAppName )
8794 }
8895
8996 if sc .SubscriptionID == "" {
@@ -108,7 +115,11 @@ func fillSetUpConfig(sc *providers.SetUpCmd) error {
108115 }
109116
110117 if sc .ResourceGroupName == "" {
111- sc .ResourceGroupName = getResourceGroup ()
118+ rg , err := getResourceGroup (sc .AzClient , sc .SubscriptionID )
119+ if err != nil {
120+ return fmt .Errorf ("getting resource group: %w" , err )
121+ }
122+ sc .ResourceGroupName = * rg .Name
112123 }
113124
114125 if sc .Repo == "" {
@@ -132,26 +143,37 @@ func runProviderSetUp(ctx context.Context, sc *providers.SetUpCmd, s spinner.Spi
132143 return nil
133144}
134145
135- func getAppName ( ) string {
146+ func PromptAppName ( az providers. AzClientInterface , defaultAppName string ) string {
136147 validate := func (input string ) error {
137148 if input == "" {
138149 return errors .New ("Invalid app name" )
139150 }
140151 return nil
141152 }
142153
143- prompt := promptui.Prompt {
154+ appNamePrompt := & promptui.Prompt {
144155 Label : "Enter app registration name" ,
145156 Validate : validate ,
157+ Default : defaultAppName ,
146158 }
147-
148- result , err := prompt .Run ()
159+ appName , err := appNamePrompt .Run ()
149160
150161 if err != nil {
151162 return err .Error ()
152163 }
153164
154- return result
165+ if providers .AzAppExists (appName ) {
166+ confirmAppExistsPrompt := promptui.Prompt {
167+ Label : "An app with this name already exists. Would you like to use it?" ,
168+ IsConfirm : true ,
169+ }
170+ _ , err := confirmAppExistsPrompt .Run ()
171+ if err != nil {
172+ appName = PromptAppName (az , defaultAppName )
173+ }
174+ }
175+
176+ return appName
155177}
156178
157179func getSubscriptionID () string {
@@ -176,47 +198,57 @@ func getSubscriptionID() string {
176198 return result
177199}
178200
179- func getResourceGroup () string {
180- validate := func (input string ) error {
181- if input == "" {
182- return errors .New ("Invalid resource group name" )
183- }
184- return nil
185- }
186-
187- prompt := promptui.Prompt {
188- Label : "Enter resource group name" ,
189- Validate : validate ,
190- }
191-
192- result , err := prompt .Run ()
201+ func getResourceGroup (az providers.AzClientInterface , subscriptionID string ) (armresources.ResourceGroup , error ) {
202+ var rg armresources.ResourceGroup
203+ log .Print ("Fetching resource groups..." )
204+ rgs , err := az .ListResourceGroups (context .Background (), subscriptionID )
193205
206+ rg , err = prompts .Select ("Please choose the resource group you would like to use" , rgs , & prompts.SelectOpt [armresources.ResourceGroup ]{
207+ Field : func (rg armresources.ResourceGroup ) string {
208+ return * rg .Name + " (" + * rg .Location + ")"
209+ },
210+ })
194211 if err != nil {
195- return err . Error ( )
212+ return rg , fmt . Errorf ( "selecting resource group: %w" , err )
196213 }
197214
198- return result
215+ return rg , nil
199216}
200217
201218func getGhRepo () string {
202219 validate := func (input string ) error {
203220 if ! strings .Contains (input , "/" ) {
204- return errors .New ("Github repo cannot be empty" )
221+ return errors .New ("github repo cannot be empty" )
205222 }
206223
207224 return nil
208225 }
209226
227+ defaultRepoNameWithOwner , err := providers .GetRepoNameWithOwner ()
228+ if err != nil {
229+ return err .Error ()
230+ }
210231 repoPrompt := promptui.Prompt {
211232 Label : "Enter github organization and repo (organization/repoName)" ,
212233 Validate : validate ,
234+ Default : defaultRepoNameWithOwner ,
213235 }
214236
215237 repo , err := repoPrompt .Run ()
216238 if err != nil {
217239 return err .Error ()
218240 }
219241
242+ if err := providers .IsValidGhRepo (repo ); err != nil {
243+ confirmMissingRepoPrompt := promptui.Prompt {
244+ Label : "Unable to confirm this repo exists. Do you want to proceed anyway?" ,
245+ IsConfirm : true ,
246+ }
247+ _ , err := confirmMissingRepoPrompt .Run ()
248+ if err != nil {
249+ repo = getGhRepo ()
250+ }
251+ }
220252 return repo
221253}
222254
0 commit comments