BuildCommonConfig builds the CommonConfig from the FlagSet, except the TFClient, which is built afterwards as it requires a logger.
()
| 346 | |
| 347 | // BuildCommonConfig builds the CommonConfig from the FlagSet, except the TFClient, which is built afterwards as it requires a logger. |
| 348 | func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) { |
| 349 | // Logger is only enabled when the log path is specified. |
| 350 | // This is because either interactive/non-interactive mode controls the terminal rendering, |
| 351 | // logging to stdout/stderr will impact the rendering. |
| 352 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 353 | if path := f.flagLogPath; path != "" { |
| 354 | level, err := logLevel(f.flagLogLevel) |
| 355 | if err != nil { |
| 356 | return config.CommonConfig{}, err |
| 357 | } |
| 358 | |
| 359 | // #nosec G304 |
| 360 | f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) |
| 361 | if err != nil { |
| 362 | return config.CommonConfig{}, fmt.Errorf("creating log file %s: %v", path, err) |
| 363 | } |
| 364 | |
| 365 | logger = slog.New(slog.NewTextHandler(f, &slog.HandlerOptions{Level: level})) |
| 366 | |
| 367 | // Enable log for azure sdk |
| 368 | os.Setenv("AZURE_SDK_GO_LOGGING", "all") // #nosec G104 |
| 369 | azlog.SetListener(func(cls azlog.Event, msg string) { |
| 370 | logger.Log(context.Background(), log.LevelTrace, msg, "event", cls) |
| 371 | }) |
| 372 | } |
| 373 | |
| 374 | authConfig, err := f.buildAuthConfig() |
| 375 | if err != nil { |
| 376 | return config.CommonConfig{}, err |
| 377 | } |
| 378 | |
| 379 | var cloudCfg cloud.Configuration |
| 380 | switch env := f.flagEnv; strings.ToLower(env) { |
| 381 | case "public": |
| 382 | cloudCfg = cloud.AzurePublic |
| 383 | case "usgovernment": |
| 384 | cloudCfg = cloud.AzureGovernment |
| 385 | case "china": |
| 386 | cloudCfg = cloud.AzureChina |
| 387 | default: |
| 388 | return config.CommonConfig{}, fmt.Errorf("unknown environment specified: %q", env) |
| 389 | } |
| 390 | |
| 391 | clientOpt := arm.ClientOptions{ |
| 392 | ClientOptions: policy.ClientOptions{ |
| 393 | Cloud: cloudCfg, |
| 394 | Telemetry: policy.TelemetryOptions{ |
| 395 | ApplicationID: fmt.Sprintf("aztfexport(%s)", f.flagProviderName), |
| 396 | Disabled: false, |
| 397 | }, |
| 398 | Logging: policy.LogOptions{ |
| 399 | IncludeBody: true, |
| 400 | }, |
| 401 | }, |
| 402 | AuxiliaryTenants: authConfig.AuxiliaryTenantIDs, |
| 403 | DisableRPRegistration: true, |
| 404 | } |
| 405 |
no test coverage detected