Skip to content

Commit 27b7c45

Browse files
committed
Add get-value
1 parent d1e6a5a commit 27b7c45

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

cli/azd/cmd/env.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ func envActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
7979
DefaultFormat: output.EnvVarsFormat,
8080
})
8181

82+
group.Add("get-value", &actions.ActionDescriptorOptions{
83+
Command: newEnvGetValueCmd(),
84+
FlagsResolver: newEnvGetValueFlags,
85+
ActionResolver: newEnvGetValueAction,
86+
})
87+
8288
return group
8389
}
8490

@@ -592,6 +598,107 @@ func (eg *envGetValuesAction) Run(ctx context.Context) (*actions.ActionResult, e
592598
return nil, eg.formatter.Format(env.Dotenv(), eg.writer, nil)
593599
}
594600

601+
func newEnvGetValueFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *envGetValueFlags {
602+
flags := &envGetValueFlags{}
603+
flags.Bind(cmd.Flags(), global)
604+
605+
return flags
606+
}
607+
608+
func newEnvGetValueCmd() *cobra.Command {
609+
cmd := &cobra.Command{
610+
Use: "get-value <keyName>",
611+
Short: "Get specific environment value.",
612+
}
613+
cmd.Args = cobra.MaximumNArgs(1)
614+
615+
return cmd
616+
}
617+
618+
type envGetValueFlags struct {
619+
internal.EnvFlag
620+
global *internal.GlobalCommandOptions
621+
}
622+
623+
func (eg *envGetValueFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
624+
eg.EnvFlag.Bind(local, global)
625+
eg.global = global
626+
}
627+
628+
type envGetValueAction struct {
629+
azdCtx *azdcontext.AzdContext
630+
console input.Console
631+
envManager environment.Manager
632+
formatter output.Formatter
633+
writer io.Writer
634+
flags *envGetValueFlags
635+
args []string
636+
}
637+
638+
func newEnvGetValueAction(
639+
azdCtx *azdcontext.AzdContext,
640+
envManager environment.Manager,
641+
console input.Console,
642+
formatter output.Formatter,
643+
writer io.Writer,
644+
flags *envGetValueFlags,
645+
args []string,
646+
647+
) actions.Action {
648+
return &envGetValueAction{
649+
azdCtx: azdCtx,
650+
console: console,
651+
envManager: envManager,
652+
formatter: formatter,
653+
writer: writer,
654+
flags: flags,
655+
args: args,
656+
}
657+
}
658+
659+
func (eg *envGetValueAction) Run(ctx context.Context) (*actions.ActionResult, error) {
660+
if len(eg.args) < 1 {
661+
return nil, fmt.Errorf("no key name provided")
662+
}
663+
664+
keyName := eg.args[0]
665+
666+
name, err := eg.azdCtx.GetDefaultEnvironmentName()
667+
if err != nil {
668+
return nil, err
669+
}
670+
// Note: if there is not an environment yet, GetDefaultEnvironmentName() returns empty string (not error)
671+
// and later, when envManager.Get() is called with the empty string, azd returns an error.
672+
// But if there is already an environment (default to be selected), azd must honor the --environment flag
673+
// over the default environment.
674+
if eg.flags.EnvironmentName != "" {
675+
name = eg.flags.EnvironmentName
676+
}
677+
env, err := eg.envManager.Get(ctx, name)
678+
if errors.Is(err, environment.ErrNotFound) {
679+
return nil, fmt.Errorf(
680+
`environment '%s' does not exist. You can create it with "azd env new %s"`,
681+
name,
682+
name,
683+
)
684+
} else if err != nil {
685+
return nil, fmt.Errorf("ensuring environment exists: %w", err)
686+
}
687+
688+
values := env.Dotenv()
689+
keyValue, exists := values[keyName]
690+
if !exists {
691+
return nil, fmt.Errorf("key '%s' not found in the environment values", keyName)
692+
}
693+
694+
// Directly write the key value to the writer
695+
if _, err := fmt.Fprintln(eg.writer, keyValue); err != nil {
696+
return nil, fmt.Errorf("writing key value: %w", err)
697+
}
698+
699+
return nil, nil
700+
}
701+
595702
func getCmdEnvHelpDescription(*cobra.Command) string {
596703
return generateCmdHelpDescription(
597704
"Manage your application environments. With this command group, you can create a new environment or get, set,"+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
Get specific environment value.
3+
4+
Usage
5+
azd env get-value <keyName> [flags]
6+
7+
Flags
8+
--docs : Opens the documentation for azd env get-value in your web browser.
9+
-e, --environment string : The name of the environment to use.
10+
-h, --help : Gets help for get-value.
11+
12+
Global Flags
13+
-C, --cwd string : Sets the current working directory.
14+
--debug : Enables debugging and diagnostics logging.
15+
--no-prompt : Accepts the default value instead of prompting, or it fails if there is no default.
16+
17+
Find a bug? Want to let us know how we're doing? Fill out this brief survey: https://aka.ms/azure-dev/hats.
18+
19+

cli/azd/cmd/testdata/TestUsage-azd-env.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Usage
1010
azd env [command]
1111

1212
Available Commands
13+
get-value : Get specific environment value.
1314
get-values : Get all environment values.
1415
list : List environments.
1516
new : Create a new environment and set it as the default.

0 commit comments

Comments
 (0)