|
| 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/container" |
| 13 | + "github.com/localstack/lstk/internal/endpoint" |
| 14 | + "github.com/localstack/lstk/internal/env" |
| 15 | + "github.com/localstack/lstk/internal/output" |
| 16 | + "github.com/localstack/lstk/internal/runtime" |
| 17 | + "github.com/localstack/lstk/internal/terminal" |
| 18 | + "github.com/spf13/cobra" |
| 19 | +) |
| 20 | + |
| 21 | +func newAzCmd(cfg *env.Env) *cobra.Command { |
| 22 | + return &cobra.Command{ |
| 23 | + Use: "az [args...]", |
| 24 | + Short: "Run Azure CLI commands against LocalStack", |
| 25 | + Long: `Run Azure CLI commands against the LocalStack Azure emulator. |
| 26 | +
|
| 27 | +Runs 'az <args>' with an isolated AZURE_CONFIG_DIR in which a custom Azure cloud |
| 28 | +is registered against LocalStack's endpoints, so your global ~/.azure |
| 29 | +configuration is left untouched and plain 'az' commands keep talking to real |
| 30 | +Azure. |
| 31 | +
|
| 32 | +Run 'lstk setup azure' once before using this command. |
| 33 | +
|
| 34 | +Examples: |
| 35 | + lstk az group list |
| 36 | + lstk az storage account list`, |
| 37 | + DisableFlagParsing: true, |
| 38 | + PreRunE: initConfig(nil), |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + rt, err := runtime.NewDockerRuntime(cfg.DockerHost) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + appCfg, err := config.Get() |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("failed to get config: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + azureContainer := config.ContainerConfig{Type: config.EmulatorAzure, Port: config.DefaultPort} |
| 51 | + for _, c := range appCfg.Containers { |
| 52 | + if c.Type == config.EmulatorAzure { |
| 53 | + azureContainer = c |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + sink := output.NewPlainSink(os.Stdout) |
| 59 | + |
| 60 | + configDir, err := config.ConfigDir() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to resolve config directory: %w", err) |
| 63 | + } |
| 64 | + azureConfigDir := azureconfig.ConfigDir(configDir) |
| 65 | + if !azureconfig.IsSetUp(azureConfigDir) { |
| 66 | + sink.Emit(output.ErrorEvent{ |
| 67 | + Title: "Azure CLI integration is not set up", |
| 68 | + Actions: []output.ErrorAction{ |
| 69 | + {Label: "Set it up:", Value: "lstk setup azure"}, |
| 70 | + }, |
| 71 | + }) |
| 72 | + return output.NewSilentError(fmt.Errorf("azure CLI integration not set up")) |
| 73 | + } |
| 74 | + |
| 75 | + if err := rt.IsHealthy(cmd.Context()); err != nil { |
| 76 | + rt.EmitUnhealthyError(sink, err) |
| 77 | + return output.NewSilentError(fmt.Errorf("runtime not healthy: %w", err)) |
| 78 | + } |
| 79 | + |
| 80 | + runningName, err := container.ResolveRunningContainerName(cmd.Context(), rt, azureContainer) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("checking emulator status: %w", err) |
| 83 | + } |
| 84 | + if runningName == "" { |
| 85 | + sink.Emit(output.ErrorEvent{ |
| 86 | + Title: fmt.Sprintf("%s is not running", azureContainer.DisplayName()), |
| 87 | + Actions: []output.ErrorAction{ |
| 88 | + {Label: "Start LocalStack:", Value: "lstk"}, |
| 89 | + {Label: "See help:", Value: "lstk -h"}, |
| 90 | + }, |
| 91 | + }) |
| 92 | + return output.NewSilentError(fmt.Errorf("%s is not running", azureContainer.Name())) |
| 93 | + } |
| 94 | + |
| 95 | + _, dnsOK := endpoint.ResolveHost(cmd.Context(), azureContainer.Port, cfg.LocalStackHost) |
| 96 | + if !dnsOK { |
| 97 | + sink.Emit(output.ErrorEvent{ |
| 98 | + Title: "DNS resolution required for 'lstk az'", |
| 99 | + Actions: []output.ErrorAction{ |
| 100 | + {Label: "Note:", Value: endpoint.DNSRebindNote}, |
| 101 | + {Label: "Why:", Value: "LocalStack routes Azure requests by Host header"}, |
| 102 | + {Label: "Fix:", Value: "configure DNS or set LOCALSTACK_HOST"}, |
| 103 | + }, |
| 104 | + }) |
| 105 | + return output.NewSilentError(fmt.Errorf("dns resolution required for 'lstk az'")) |
| 106 | + } |
| 107 | + |
| 108 | + azEnv := azureconfig.Env(azureConfigDir) |
| 109 | + |
| 110 | + stdout, stderr := io.Writer(os.Stdout), io.Writer(os.Stderr) |
| 111 | + if terminal.IsTerminal(os.Stderr) { |
| 112 | + s := terminal.NewSpinner(os.Stderr, "Loading service...", 4*time.Second) |
| 113 | + s.Start() |
| 114 | + defer s.Stop() |
| 115 | + stdout = &terminal.StopOnWriteWriter{W: os.Stdout, Spinner: s} |
| 116 | + stderr = &terminal.StopOnWriteWriter{W: os.Stderr, Spinner: s} |
| 117 | + } |
| 118 | + |
| 119 | + return azurecli.Exec(cmd.Context(), azEnv, os.Stdin, stdout, stderr, args...) |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
0 commit comments