Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option: "--env" #378

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var flagset FlagSet

type FlagSet struct {
// common flags
flagEnv string
flagSubscriptionId string
flagOutputDir string
flagOverwrite bool
Expand Down Expand Up @@ -70,6 +71,10 @@ func (flag FlagSet) DescribeCLI(mode string) string {
// - flagBackendConfig
// - all hflags

if flag.flagEnv != "" {
args = append(args, "--env="+flag.flagEnv)
}

if flag.flagOverwrite {
args = append(args, "--overwrite=true")
}
Expand Down
23 changes: 13 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ func prepareConfigFile(ctx *cli.Context) error {

func main() {
commonFlags := []cli.Flag{
&cli.StringFlag{
Name: "env",
// Honor the "ARM_ENVIRONMENT" as is used by the AzureRM provider, for easier use.
EnvVars: []string{"AZTFEXPORT_ENV", "ARM_ENVIRONMENT"},
Usage: `The cloud environment, can be one of "public", "usgovernment" and "china"`,
Destination: &flagset.flagEnv,
Value: "public",
},
&cli.StringFlag{
Name: "subscription-id",
// Honor the "ARM_SUBSCRIPTION_ID" as is used by the AzureRM provider, for easier use.
Expand Down Expand Up @@ -380,7 +388,7 @@ func main() {
return fmt.Errorf("invalid resource id: %v", err)
}

cred, clientOpt, err := buildAzureSDKCredAndClientOpt()
cred, clientOpt, err := buildAzureSDKCredAndClientOpt(flagset.flagEnv)
if err != nil {
return err
}
Expand Down Expand Up @@ -432,7 +440,7 @@ func main() {

rg := c.Args().First()

cred, clientOpt, err := buildAzureSDKCredAndClientOpt()
cred, clientOpt, err := buildAzureSDKCredAndClientOpt(flagset.flagEnv)
if err != nil {
return err
}
Expand Down Expand Up @@ -483,7 +491,7 @@ func main() {

predicate := c.Args().First()

cred, clientOpt, err := buildAzureSDKCredAndClientOpt()
cred, clientOpt, err := buildAzureSDKCredAndClientOpt(flagset.flagEnv)
if err != nil {
return err
}
Expand Down Expand Up @@ -535,7 +543,7 @@ func main() {

mapFile := c.Args().First()

cred, clientOpt, err := buildAzureSDKCredAndClientOpt()
cred, clientOpt, err := buildAzureSDKCredAndClientOpt(flagset.flagEnv)
if err != nil {
return err
}
Expand Down Expand Up @@ -660,12 +668,7 @@ func initTelemetryClient() telemetry.Client {
}

// buildAzureSDKCredAndClientOpt builds the Azure SDK credential and client option from multiple sources (i.e. environment variables, MSI, Azure CLI).
func buildAzureSDKCredAndClientOpt() (azcore.TokenCredential, *arm.ClientOptions, error) {
env := "public"
if v := os.Getenv("ARM_ENVIRONMENT"); v != "" {
env = v
}

func buildAzureSDKCredAndClientOpt(env string) (azcore.TokenCredential, *arm.ClientOptions, error) {
var cloudCfg cloud.Configuration
switch strings.ToLower(env) {
case "public":
Expand Down