@@ -31,7 +31,6 @@ import (
3131 "github.com/azure/azure-dev/cli/azd/pkg/tools"
3232 "github.com/azure/azure-dev/cli/azd/pkg/tools/git"
3333 "github.com/azure/azure-dev/cli/azd/pkg/workflow"
34- "github.com/fatih/color"
3534 "github.com/joho/godotenv"
3635 "github.com/spf13/cobra"
3736 "github.com/spf13/pflag"
@@ -59,6 +58,7 @@ type initFlags struct {
5958 location string
6059 global * internal.GlobalCommandOptions
6160 fromCode bool
61+ minimal bool
6262 up bool
6363 internal.EnvFlag
6464}
@@ -99,6 +99,13 @@ func (i *initFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOpt
9999 false ,
100100 "Initializes a new application from your existing code." ,
101101 )
102+ local .BoolVarP (
103+ & i .minimal ,
104+ "minimal" ,
105+ "m" ,
106+ false ,
107+ "Initializes a minimal project." ,
108+ )
102109 local .BoolVarP (
103110 & i .up ,
104111 "up" ,
@@ -206,28 +213,35 @@ func (i *initAction) Run(ctx context.Context) (*actions.ActionResult, error) {
206213 return nil , fmt .Errorf ("checking if project exists: %w" , err )
207214 }
208215
209- var initTypeSelect initType
216+ var initTypeSelect initType = initUnknown
217+ initTypeCount := 0
210218 if i .flags .templatePath != "" || len (i .flags .templateTags ) > 0 {
211- // an explicit --template passed, always initialize from app template
219+ initTypeCount ++
212220 initTypeSelect = initAppTemplate
213221 }
214-
215222 if i .flags .fromCode {
216- if i .flags .templatePath != "" {
217- return nil , errors .New ("only one of init modes: --template, or --from-code should be set" )
218- }
223+ initTypeCount ++
219224 initTypeSelect = initFromApp
220225 }
226+ if i .flags .minimal {
227+ initTypeCount ++
228+ initTypeSelect = initFromApp // Minimal now also uses initFromApp path
229+ }
221230
222- if i .flags .templatePath == "" && ! i .flags .fromCode && existingProject {
223- // only initialize environment when no mode is set explicitly
224- initTypeSelect = initEnvironment
231+ if initTypeCount > 1 {
232+ return nil , errors .New ("only one of init modes: --template, --from-code, or --minimal should be set" )
225233 }
226234
227235 if initTypeSelect == initUnknown {
228- initTypeSelect , err = promptInitType (i .console , ctx )
229- if err != nil {
230- return nil , err
236+ if existingProject {
237+ // only initialize environment when no mode is set explicitly
238+ initTypeSelect = initEnvironment
239+ } else {
240+ // Prompt for init type for new projects
241+ initTypeSelect , err = promptInitType (i .console , ctx )
242+ if err != nil {
243+ return nil , err
244+ }
231245 }
232246 }
233247
@@ -277,36 +291,48 @@ func (i *initAction) Run(ctx context.Context) (*actions.ActionResult, error) {
277291
278292 case initFromApp :
279293 tracing .SetUsageAttributes (fields .InitMethod .String ("app" ))
280-
281294 header = "Your app is ready for the cloud!"
282295 followUp = "Run " + output .WithHighLightFormat ("azd up" ) + " to provision and deploy your app to Azure.\n " +
283296 "Run " + output .WithHighLightFormat ("azd add" ) + " to add new Azure components to your project.\n " +
284297 "Run " + output .WithHighLightFormat ("azd infra gen" ) + " to generate IaC for your project to disk, " +
285298 "allowing you to manually manage it.\n " +
286299 "See " + output .WithHighLightFormat ("./next-steps.md" ) + " for more information on configuring your app."
287- entries , err := os .ReadDir (azdCtx .ProjectDirectory ())
288- if err != nil {
289- return nil , fmt .Errorf ("reading current directory: %w" , err )
300+
301+ envSpecified := i .flags .EnvironmentName != ""
302+ initializeEnv := func () (* environment.Environment , error ) {
303+ return i .initializeEnv (ctx , azdCtx , templates.Metadata {})
290304 }
305+ initializeMinimal := func () error {
306+ tracing .SetUsageAttributes (fields .InitMethod .String ("project" ))
307+ err := i .repoInitializer .InitializeMinimal (ctx , azdCtx )
308+ if err != nil {
309+ return err
310+ }
291311
292- if len (entries ) == 0 {
293- return nil , & internal.ErrorWithSuggestion {
294- Err : errors .New ("no files found in the current directory" ),
295- Suggestion : "Ensure you're in the directory where your app code is located and try again." +
296- " If you do not have code and would like to start with an app template, run '" +
297- output .WithHighLightFormat ("azd init" ) + "' and select the option to " +
298- color .MagentaString ("Use a template" ) + "." ,
312+ // Create env upfront only if the environment name is passed in.
313+ if envSpecified {
314+ _ , err := initializeEnv ()
315+ if err != nil {
316+ return err
317+ }
299318 }
319+
320+ header = "Generated azure.yaml project file."
321+ followUp = "Run " + output .WithHighLightFormat ("azd add" ) + " to add new Azure components to your project."
322+ return nil
300323 }
301324
302- err = i .repoInitializer .InitFromApp (
303- ctx ,
304- azdCtx ,
305- func () (* environment.Environment , error ) {
306- return i .initializeEnv (ctx , azdCtx , templates.Metadata {})
307- },
308- i .flags .EnvironmentName != "" ,
309- )
325+ if i .flags .minimal {
326+ err = initializeMinimal ()
327+ } else {
328+ err = i .repoInitializer .InitFromApp (
329+ ctx ,
330+ azdCtx ,
331+ initializeEnv ,
332+ initializeMinimal ,
333+ envSpecified ,
334+ )
335+ }
310336 if err != nil {
311337 return nil , err
312338 }
@@ -318,24 +344,6 @@ func (i *initAction) Run(ctx context.Context) (*actions.ActionResult, error) {
318344
319345 header = fmt .Sprintf ("Initialized environment %s." , env .Name ())
320346 followUp = ""
321- case initProject :
322- tracing .SetUsageAttributes (fields .InitMethod .String ("project" ))
323-
324- err = i .repoInitializer .InitializeMinimal (ctx , azdCtx )
325- if err != nil {
326- return nil , err
327- }
328-
329- // Create env upfront only if the environment name is passed in.
330- if i .flags .EnvironmentName != "" {
331- _ , err := i .initializeEnv (ctx , azdCtx , templates.Metadata {})
332- if err != nil {
333- return nil , err
334- }
335- }
336-
337- header = "Generated azure.yaml project file."
338- followUp = "Run " + output .WithHighLightFormat ("azd add" ) + " to add new Azure components to your project."
339347 default :
340348 panic ("unhandled init type" )
341349 }
@@ -358,17 +366,15 @@ const (
358366 initUnknown = iota
359367 initFromApp
360368 initAppTemplate
361- initProject
362369 initEnvironment
363370)
364371
365372func promptInitType (console input.Console , ctx context.Context ) (initType , error ) {
366373 selection , err := console .Select (ctx , input.ConsoleOptions {
367374 Message : "How do you want to initialize your app?" ,
368375 Options : []string {
369- "Use code in the current directory" ,
376+ "Scan current directory" , // This now covers minimal project creation too
370377 "Select a template" ,
371- "Create a minimal project" ,
372378 },
373379 })
374380 if err != nil {
@@ -380,8 +386,6 @@ func promptInitType(console input.Console, ctx context.Context) (initType, error
380386 return initFromApp , nil
381387 case 1 :
382388 return initAppTemplate , nil
383- case 2 :
384- return initProject , nil
385389 default :
386390 panic ("unhandled selection" )
387391 }
0 commit comments