@@ -79,6 +79,12 @@ func envActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
79
79
DefaultFormat : output .EnvVarsFormat ,
80
80
})
81
81
82
+ group .Add ("get-value" , & actions.ActionDescriptorOptions {
83
+ Command : newEnvGetValueCmd (),
84
+ FlagsResolver : newEnvGetValueFlags ,
85
+ ActionResolver : newEnvGetValueAction ,
86
+ })
87
+
82
88
return group
83
89
}
84
90
@@ -592,6 +598,104 @@ func (eg *envGetValuesAction) Run(ctx context.Context) (*actions.ActionResult, e
592
598
return nil , eg .formatter .Format (env .Dotenv (), eg .writer , nil )
593
599
}
594
600
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
+ writer io.Writer
633
+ flags * envGetValueFlags
634
+ args []string
635
+ }
636
+
637
+ func newEnvGetValueAction (
638
+ azdCtx * azdcontext.AzdContext ,
639
+ envManager environment.Manager ,
640
+ console input.Console ,
641
+ writer io.Writer ,
642
+ flags * envGetValueFlags ,
643
+ args []string ,
644
+
645
+ ) actions.Action {
646
+ return & envGetValueAction {
647
+ azdCtx : azdCtx ,
648
+ console : console ,
649
+ envManager : envManager ,
650
+ writer : writer ,
651
+ flags : flags ,
652
+ args : args ,
653
+ }
654
+ }
655
+
656
+ func (eg * envGetValueAction ) Run (ctx context.Context ) (* actions.ActionResult , error ) {
657
+ if len (eg .args ) < 1 {
658
+ return nil , fmt .Errorf ("no key name provided" )
659
+ }
660
+
661
+ keyName := eg .args [0 ]
662
+
663
+ name , err := eg .azdCtx .GetDefaultEnvironmentName ()
664
+ if err != nil {
665
+ return nil , err
666
+ }
667
+ // Note: if there is not an environment yet, GetDefaultEnvironmentName() returns empty string (not error)
668
+ // and later, when envManager.Get() is called with the empty string, azd returns an error.
669
+ // But if there is already an environment (default to be selected), azd must honor the --environment flag
670
+ // over the default environment.
671
+ if eg .flags .EnvironmentName != "" {
672
+ name = eg .flags .EnvironmentName
673
+ }
674
+ env , err := eg .envManager .Get (ctx , name )
675
+ if errors .Is (err , environment .ErrNotFound ) {
676
+ return nil , fmt .Errorf (
677
+ `environment '%s' does not exist. You can create it with "azd env new %s"` ,
678
+ name ,
679
+ name ,
680
+ )
681
+ } else if err != nil {
682
+ return nil , fmt .Errorf ("ensuring environment exists: %w" , err )
683
+ }
684
+
685
+ values := env .Dotenv ()
686
+ keyValue , exists := values [keyName ]
687
+ if ! exists {
688
+ return nil , fmt .Errorf ("key '%s' not found in the environment values" , keyName )
689
+ }
690
+
691
+ // Directly write the key value to the writer
692
+ if _ , err := fmt .Fprintln (eg .writer , keyValue ); err != nil {
693
+ return nil , fmt .Errorf ("writing key value: %w" , err )
694
+ }
695
+
696
+ return nil , nil
697
+ }
698
+
595
699
func getCmdEnvHelpDescription (* cobra.Command ) string {
596
700
return generateCmdHelpDescription (
597
701
"Manage your application environments. With this command group, you can create a new environment or get, set," +
0 commit comments