Skip to content

Commit

Permalink
Add initialize from app option (#2700)
Browse files Browse the repository at this point in the history
Add initialize from an existing app option to `init` (alpha).

Initialize from an existing application will:

1. Detect projects in the existing app. (using `appdetect`)
2. Provide prompt options to customize, troubleshoot, or fill in additional details (`detect_confirm.go` and `infra_confirm.go`)
3. Finally, generate `azure.yaml` and scaffold infrastructure based on the spec.

V1 implementation of #705
  • Loading branch information
weikanglim committed Sep 5, 2023
1 parent 54e9785 commit 3ca9ec6
Show file tree
Hide file tree
Showing 18 changed files with 1,565 additions and 30 deletions.
11 changes: 3 additions & 8 deletions cli/azd/cmd/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,9 @@ func (cb *CobraBuilder) configureActionResolver(cmd *cobra.Command, descriptor *
}

if errors.As(err, &suggestionErr) {
if actionResult != nil && actionResult.TraceID != "" {
console.Message(
ctx,
output.WithErrorFormat(fmt.Sprintf("TraceID: %s", actionResult.TraceID)))
console.Message(
ctx,
output.WithHighLightFormat(suggestionErr.Suggestion))
}
console.Message(
ctx,
output.WithHighLightFormat(suggestionErr.Suggestion))
}
}
})
Expand Down
61 changes: 59 additions & 2 deletions cli/azd/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/internal/repository"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
Expand All @@ -22,7 +23,9 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/templates"
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/azure/azure-dev/cli/azd/pkg/tools/git"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -84,6 +87,7 @@ type initAction struct {
flags *initFlags
repoInitializer *repository.Initializer
templateManager *templates.TemplateManager
featuresManager *alpha.FeatureManager
}

func newInitAction(
Expand All @@ -92,14 +96,16 @@ func newInitAction(
gitCli git.GitCli,
flags *initFlags,
repoInitializer *repository.Initializer,
templateManager *templates.TemplateManager) actions.Action {
templateManager *templates.TemplateManager,
featuresManager *alpha.FeatureManager) actions.Action {
return &initAction{
console: console,
cmdRun: cmdRun,
gitCli: gitCli,
flags: flags,
repoInitializer: repoInitializer,
templateManager: templateManager,
featuresManager: featuresManager,
}
}

Expand Down Expand Up @@ -148,7 +154,14 @@ func (i *initAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}

if initTypeSelect == initUnknown {
initTypeSelect = initAppTemplate
if i.featuresManager.IsEnabled(alpha.EasyInit) {
initTypeSelect, err = promptInitType(i.console, ctx)
if err != nil {
return nil, err
}
} else {
initTypeSelect = initAppTemplate
}
}

header := "New project initialized!"
Expand All @@ -166,6 +179,27 @@ func (i *initAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}

err = i.initializeEnv(ctx, azdCtx)
if err != nil {
return nil, err
}
case initFromApp:
header = "Your app is ready for the cloud!"
followUp = "You can provision and deploy your app to Azure by running the " + color.BlueString("azd up") +
" command in this directory. For more information on configuring your app, see " +
output.WithHighLightFormat("./next-steps.md")
err := i.repoInitializer.InitFromApp(ctx, azdCtx, func() error {
return i.initializeEnv(ctx, azdCtx)
})
if errors.Is(err, repository.ErrNoServicesDetected) {
return nil, &azcli.ErrorWithSuggestion{
Err: errors.New("no files or services detected in the current directory"),
Suggestion: "Ensure you're in the directory where your app code is located and try again." +
" If you do not have code and would like to start with an app template, run '" +
color.BlueString("azd init") + "' in an empty directory and select the option to " +
color.MagentaString("Use a template") + ".",
}
}

if err != nil {
return nil, err
}
Expand All @@ -190,10 +224,33 @@ type initType int

const (
initUnknown = iota
initFromApp
initAppTemplate
initEnvironment
)

func promptInitType(console input.Console, ctx context.Context) (initType, error) {
selection, err := console.Select(ctx, input.ConsoleOptions{
Message: "How do you want to initialize your app?",
Options: []string{
"Use code in the current directory",
"Select a template",
},
})
if err != nil {
return initUnknown, err
}

switch selection {
case 0:
return initFromApp, nil
case 1:
return initAppTemplate, nil
default:
panic("unhandled selection")
}
}

func (i *initAction) initializeTemplate(
ctx context.Context,
azdCtx *azdcontext.AzdContext) error {
Expand Down
10 changes: 8 additions & 2 deletions cli/azd/internal/appdetect/appdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const (
PyFastApi Dependency = "fastapi"
)

var WebUIFrameworks = map[Dependency]struct{}{
JsReact: {},
JsAngular: {},
JsVue: {},
JsJQuery: {},
}

func (f Dependency) Language() Language {
switch f {
case JsReact, JsAngular, JsVue, JsJQuery:
Expand All @@ -79,8 +86,7 @@ func (f Dependency) Display() string {
}

func (f Dependency) IsWebUIFramework() bool {
switch f {
case JsReact, JsAngular, JsVue, JsJQuery:
if _, ok := WebUIFrameworks[f]; ok {
return true
}

Expand Down

0 comments on commit 3ca9ec6

Please sign in to comment.