|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/localstack/lstk/internal/azurecli" |
| 10 | + "github.com/localstack/lstk/internal/azureconfig" |
| 11 | + "github.com/localstack/lstk/internal/config" |
| 12 | + "github.com/localstack/lstk/internal/endpoint" |
| 13 | + "github.com/localstack/lstk/internal/env" |
| 14 | + "github.com/localstack/lstk/internal/output" |
| 15 | + "github.com/localstack/lstk/internal/terminal" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +func newAzCmd(cfg *env.Env) *cobra.Command { |
| 20 | + return &cobra.Command{ |
| 21 | + Use: "az [args...]", |
| 22 | + Short: "Run Azure CLI commands against LocalStack", |
| 23 | + Long: `Proxy Azure CLI commands to the LocalStack Azure emulator. |
| 24 | +
|
| 25 | +Runs 'az <args>' with an isolated AZURE_CONFIG_DIR routed through LocalStack's |
| 26 | +HTTPS proxy, so your global ~/.azure configuration is left untouched and plain |
| 27 | +'az' commands keep talking to real Azure. |
| 28 | +
|
| 29 | +Run 'lstk setup azure' once before using this command. |
| 30 | +
|
| 31 | +Examples: |
| 32 | + lstk az group list |
| 33 | + lstk az storage account list`, |
| 34 | + DisableFlagParsing: true, |
| 35 | + PreRunE: initConfig(nil), |
| 36 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + appCfg, err := config.Get() |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to get config: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + azureContainer := config.ContainerConfig{Type: config.EmulatorAzure, Port: config.DefaultAWSPort} |
| 43 | + for _, c := range appCfg.Containers { |
| 44 | + if c.Type == config.EmulatorAzure { |
| 45 | + azureContainer = c |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + sink := output.NewPlainSink(os.Stdout) |
| 51 | + |
| 52 | + configDir, err := config.ConfigDir() |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to resolve config directory: %w", err) |
| 55 | + } |
| 56 | + azureConfigDir := azureconfig.ConfigDir(configDir) |
| 57 | + if !azureconfig.IsSetUp(azureConfigDir) { |
| 58 | + sink.Emit(output.ErrorEvent{ |
| 59 | + Title: "Azure CLI integration is not set up", |
| 60 | + Actions: []output.ErrorAction{ |
| 61 | + {Label: "Set it up:", Value: "lstk setup azure"}, |
| 62 | + }, |
| 63 | + }) |
| 64 | + return output.NewSilentError(fmt.Errorf("azure CLI integration not set up")) |
| 65 | + } |
| 66 | + |
| 67 | + host, _ := endpoint.ResolveHost(cmd.Context(), azureContainer.Port, cfg.LocalStackHost) |
| 68 | + endpointURL := azureconfig.BuildEndpoint(host) |
| 69 | + |
| 70 | + if err := azureconfig.IsRunning(cmd.Context(), endpointURL); err != nil { |
| 71 | + sink.Emit(output.ErrorEvent{ |
| 72 | + Title: fmt.Sprintf("%s is not running", azureContainer.DisplayName()), |
| 73 | + Actions: []output.ErrorAction{ |
| 74 | + {Label: "Start LocalStack:", Value: "lstk"}, |
| 75 | + {Label: "See help:", Value: "lstk -h"}, |
| 76 | + }, |
| 77 | + }) |
| 78 | + return output.NewSilentError(fmt.Errorf("%s is not running", azureContainer.Name())) |
| 79 | + } |
| 80 | + |
| 81 | + proxyEndpoint, err := azureconfig.ProxyEndpoint(cmd.Context(), endpointURL) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("could not discover LocalStack proxy: %w", err) |
| 84 | + } |
| 85 | + azEnv := azureconfig.ProxyEnv(azureConfigDir, proxyEndpoint, azureconfig.CACertPath(azureConfigDir)) |
| 86 | + |
| 87 | + stdout, stderr := io.Writer(os.Stdout), io.Writer(os.Stderr) |
| 88 | + if terminal.IsTerminal(os.Stderr) { |
| 89 | + s := terminal.NewSpinner(os.Stderr, "Loading service...", 4*time.Second) |
| 90 | + s.Start() |
| 91 | + defer s.Stop() |
| 92 | + stdout = &terminal.StopOnWriteWriter{W: os.Stdout, Spinner: s} |
| 93 | + stderr = &terminal.StopOnWriteWriter{W: os.Stderr, Spinner: s} |
| 94 | + } |
| 95 | + |
| 96 | + return azurecli.Exec(cmd.Context(), azEnv, os.Stdin, stdout, stderr, args...) |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
0 commit comments