@@ -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,107 @@ 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
+ 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
+
595
702
func getCmdEnvHelpDescription (* cobra.Command ) string {
596
703
return generateCmdHelpDescription (
597
704
"Manage your application environments. With this command group, you can create a new environment or get, set," +
0 commit comments