-
Notifications
You must be signed in to change notification settings - Fork 268
emulate az with azd - for terraform only #3971
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
Changes from 3 commits
619caa3
791fa3d
d110a34
86b5cc5
3102262
139f60c
61560ba
5a98a54
9891526
df9e2f6
e6afa00
2144ff3
1aee2fd
33132a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/cmd/actions" | ||
| "github.com/azure/azure-dev/cli/azd/internal" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/account" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/input" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/output" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| func azCliEmulateAccountCommands(root *actions.ActionDescriptor) *actions.ActionDescriptor { | ||
|
||
| group := root.Add("account", &actions.ActionDescriptorOptions{ | ||
| Command: &cobra.Command{ | ||
| Use: "account", | ||
| Short: "Emulates az account commands", | ||
| Hidden: true, | ||
| }, | ||
| }) | ||
|
|
||
| group.Add("show", &actions.ActionDescriptorOptions{ | ||
| Command: newAccountShowCmd(), | ||
| FlagsResolver: newAccountShowFlags, | ||
| ActionResolver: newAccountAction, | ||
| OutputFormats: []output.Format{output.JsonFormat}, | ||
| DefaultFormat: output.JsonFormat, | ||
| }) | ||
|
|
||
| group.Add("get-access-token", &actions.ActionDescriptorOptions{ | ||
| Command: &cobra.Command{ | ||
| Use: "get-access-token", | ||
| Hidden: true, | ||
| }, | ||
| FlagsResolver: newAuthTokenFlags, | ||
| ActionResolver: newAuthTokenAction, | ||
| OutputFormats: []output.Format{output.JsonFormat}, | ||
| DefaultFormat: output.JsonFormat, | ||
| }) | ||
|
|
||
| return group | ||
| } | ||
|
|
||
| type accountShowFlags struct { | ||
| global *internal.GlobalCommandOptions | ||
| internal.EnvFlag | ||
| } | ||
|
|
||
| func (s *accountShowFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) { | ||
| s.EnvFlag.Bind(local, global) | ||
| s.global = global | ||
| } | ||
|
|
||
| func newAccountShowFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *accountShowFlags { | ||
| flags := &accountShowFlags{} | ||
| flags.Bind(cmd.Flags(), global) | ||
|
|
||
| return flags | ||
| } | ||
|
|
||
| func newAccountShowCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "show", | ||
| Hidden: true, | ||
| } | ||
| } | ||
|
|
||
| type accountShowAction struct { | ||
| env *environment.Environment | ||
| console input.Console | ||
| subManager *account.SubscriptionsManager | ||
| } | ||
|
|
||
| func newAccountAction( | ||
| console input.Console, | ||
| env *environment.Environment, | ||
| subManager *account.SubscriptionsManager, | ||
| ) actions.Action { | ||
| return &accountShowAction{ | ||
| console: console, | ||
| env: env, | ||
| subManager: subManager, | ||
| } | ||
| } | ||
|
|
||
| type accountShowOutput struct { | ||
| Id string `json:"id"` | ||
| TenantId string `json:"tenantId"` | ||
| } | ||
|
|
||
| func (s *accountShowAction) Run(ctx context.Context) (*actions.ActionResult, error) { | ||
| subId := s.env.GetSubscriptionId() | ||
| tenantId, err := s.subManager.LookupTenant(ctx, subId) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| o := accountShowOutput{ | ||
| Id: subId, | ||
| TenantId: tenantId, | ||
| } | ||
| output, err := json.Marshal(o) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| fmt.Fprint(s.console.Handles().Stdout, string(output)) | ||
| return nil, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package exec | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/config" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/osutil" | ||
| ) | ||
|
|
||
| const ( | ||
| emulatorEnvName string = "AZURE_AZ_EMULATOR" | ||
| ) | ||
|
|
||
| // IsAzEmulator returns true if the AZURE_AZ_EMULATOR environment variable is defined. | ||
| // It does not matter the value of the environment variable, as long as it is defined. | ||
| func IsAzEmulator() bool { | ||
| _, emulateEnvVarDefined := os.LookupEnv(emulatorEnvName) | ||
| return emulateEnvVarDefined | ||
| } | ||
|
|
||
| // creates a copy of azd binary and renames it to az and returns the path to it | ||
| func emulateAzFromPath() (string, error) { | ||
| path, err := exec.LookPath("azd") | ||
|
||
| if err != nil { | ||
| return "", fmt.Errorf("azd binary not found in PATH: %w", err) | ||
| } | ||
| azdConfigPath, err := config.GetUserConfigDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("could not get user config dir: %w", err) | ||
| } | ||
| emuPath := filepath.Join(azdConfigPath, "bin", "azEmulate") | ||
| err = os.MkdirAll(emuPath, osutil.PermissionDirectoryOwnerOnly) | ||
| if err != nil { | ||
| return "", fmt.Errorf("could not create directory for azEmulate: %w", err) | ||
| } | ||
| emuPath = filepath.Join(emuPath, strings.ReplaceAll(filepath.Base(path), "azd", "az")) | ||
|
|
||
| srcFile, err := os.Open(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("opening src: %w", err) | ||
| } | ||
| defer srcFile.Close() | ||
|
|
||
| destFile, err := os.OpenFile(emuPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) | ||
| if err != nil { | ||
| return "", fmt.Errorf("creating dest: %w", err) | ||
| } | ||
| defer destFile.Close() | ||
|
|
||
| _, err = io.Copy(destFile, srcFile) | ||
vhvb1989 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return "", fmt.Errorf("copying binary: %w", err) | ||
| } | ||
|
|
||
| return emuPath, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.