Skip to content

Commit

Permalink
Add script parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Jul 17, 2024
1 parent b633cdf commit 109ffc6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cli/azd/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
"program": "${workspaceFolder}",
"console": "integratedTerminal",
"args": [
"auth", "login"
"script", "run", "start"
//"pipeline", "config", "--principal-name", "foo", "--provider", "github"
//"package", "api", "--debug"
//"provision"
//"up"
//"env", "new"
],
//"cwd": "~/workspace/cwd/path",
"cwd": "/workspaces/fast-api-test-2",
// "env": {
// "INT_TAG_VALUE":"1989"
// }
Expand Down
77 changes: 47 additions & 30 deletions cli/azd/cmd/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,31 @@ func newScriptsRunFlags(cmd *cobra.Command, global *internal.GlobalCommandOption

func newScriptsRunCmd() *cobra.Command {
return &cobra.Command{
Use: "run <name>",
Use: "run [name]",
Short: "Runs the specified script for the project",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
}
}

type scriptsRunFlags struct {
internal.EnvFlag
global *internal.GlobalCommandOptions
platform string
global *internal.GlobalCommandOptions
platform string
run string
shell string
continueOnError bool
interactive bool
}

func (f *scriptsRunFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
f.EnvFlag.Bind(local, global)
f.global = global

local.StringVar(&f.platform, "platform", "", "Forces scripts to run for the specified platform.")
local.StringVar(&f.run, "run", "", "Inline script to run.")
local.StringVar(&f.shell, "shell", "sh", "Shell to use for the script.")
local.BoolVar(&f.continueOnError, "continueOnError", true, "Continue on error.")
local.BoolVar(&f.interactive, "interactive", true, "Run in interactive mode.")
}

type scriptsRunAction struct {
Expand Down Expand Up @@ -99,28 +107,50 @@ func newScriptsRunAction(
const noScriptFoundMessage = " (No script found)"

func (sra *scriptsRunAction) Run(ctx context.Context) (*actions.ActionResult, error) {
scriptName := sra.args[0]
var scriptName string
if len(sra.args) > 0 {
scriptName = sra.args[0]
}

if scriptName == "" && sra.flags.run == "" {
return nil, fmt.Errorf("either a script name or --run must be specified")
}

// Command title
sra.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: "Running scripts (azd scripts run)",
TitleNote: fmt.Sprintf(
"Finding and executing %s scripts for environment %s",
"Finding and executing %s scripts",
output.WithHighLightFormat(scriptName),
output.WithHighLightFormat(sra.env.Name()),
),
})

// Project level scripts
if err := sra.processScripts(
ctx,
sra.projectConfig.Path,
scriptName,
fmt.Sprintf("Running %s command script for project", scriptName),
fmt.Sprintf("Project: %s Script Output", scriptName),
sra.projectConfig.Scripts, // Use projectConfig.Scripts directly
); err != nil {
return nil, err
if scriptName != "" {
// Named script from azure.yaml
if err := sra.processScripts(
ctx,
sra.projectConfig.Path,
scriptName,
fmt.Sprintf("Running %s command script for project", scriptName),
fmt.Sprintf("Project: %s Script Output", scriptName),
sra.projectConfig.Scripts,
); err != nil {
return nil, err
}
} else {
// Inline script from command-line parameters
script := &ext.HookConfig{
Run: sra.flags.run,
Shell: ext.ShellType(sra.flags.shell),
ContinueOnError: sra.flags.continueOnError,
Interactive: sra.flags.interactive,
}
if err := sra.prepareScript("inline-script", script); err != nil {
return nil, err
}
if err := sra.execScript(ctx, "Running inline script", sra.projectConfig.Path, script); err != nil {
return nil, err
}
}

return &actions.ActionResult{
Expand Down Expand Up @@ -212,18 +242,5 @@ func (sra *scriptsRunAction) prepareScript(name string, script *ext.HookConfig)
}

script.Name = name
script.Interactive = true

sra.configureScriptFlags(script.Windows)
sra.configureScriptFlags(script.Posix)

return nil
}

func (sra *scriptsRunAction) configureScriptFlags(script *ext.HookConfig) {
if script == nil {
return
}

script.Interactive = true
}
6 changes: 5 additions & 1 deletion cli/azd/cmd/testdata/TestUsage-azd-script-run.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
Runs the specified script for the project

Usage
azd script run <name> [flags]
azd script run [name] [flags]

Flags
--continueOnError : Continue on error.
--docs : Opens the documentation for azd script run in your web browser.
-e, --environment string : The name of the environment to use.
-h, --help : Gets help for run.
--interactive : Run in interactive mode.
--platform string : Forces scripts to run for the specified platform.
--run string : Inline script to run.
--shell string : Shell to use for the script.

Global Flags
-C, --cwd string : Sets the current working directory.
Expand Down

0 comments on commit 109ffc6

Please sign in to comment.