|
| 1 | +package mnq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 11 | + "github.com/scaleway/scaleway-cli/v2/internal/interactive" |
| 12 | + mnq "github.com/scaleway/scaleway-sdk-go/api/mnq/v1beta1" |
| 13 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 14 | +) |
| 15 | + |
| 16 | +type NatsEntity struct { |
| 17 | + Name string |
| 18 | + Content []byte |
| 19 | +} |
| 20 | + |
| 21 | +func makeDirectoryIfNotExists(path string) error { |
| 22 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 23 | + return os.MkdirAll(path, os.ModeDir|0755) |
| 24 | + } |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +func wrapError(err error, message, name, path string) error { |
| 29 | + return &core.CliError{ |
| 30 | + Err: err, |
| 31 | + Message: fmt.Sprintf("%s into file %q", message, path), |
| 32 | + Details: fmt.Sprintf("You may want to delete created credentials %q", name), |
| 33 | + Code: 1, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func fileExists(filePath string) bool { |
| 38 | + _, err := os.Stat(filePath) |
| 39 | + return !os.IsNotExist(err) |
| 40 | +} |
| 41 | + |
| 42 | +func natsContextFrom(account *mnq.NatsAccount, credsPath string) []byte { |
| 43 | + ctx := &natsContext{ |
| 44 | + Description: "Nats context created by Scaleway CLI", |
| 45 | + URL: account.Endpoint, |
| 46 | + CredentialsPath: credsPath, |
| 47 | + } |
| 48 | + b, _ := json.Marshal(ctx) |
| 49 | + return b |
| 50 | +} |
| 51 | + |
| 52 | +func writeFile(ctx context.Context, dir string, entity *NatsEntity, extension string) (string, error) { |
| 53 | + path := filepath.Join(dir, entity.Name+"."+extension) |
| 54 | + if err := makeDirectoryIfNotExists(dir); err != nil { |
| 55 | + return "", wrapError(err, "Failed to create directory", entity.Name, path) |
| 56 | + } |
| 57 | + if fileExists(path) { |
| 58 | + overWrite, err := promptOverWriteFile(ctx, path) |
| 59 | + if err != nil { |
| 60 | + return "", wrapError(err, "Failed to prompt for overwrite", entity.Name, path) |
| 61 | + } |
| 62 | + if !overWrite { |
| 63 | + return "", wrapError(nil, "File already exists", entity.Name, path) |
| 64 | + } |
| 65 | + } |
| 66 | + if err := os.WriteFile(path, entity.Content, 0600); err != nil { |
| 67 | + return "", wrapError(err, "Failed to write file", entity.Name, path) |
| 68 | + } |
| 69 | + _, _ = interactive.Println(entity.Name + " file has been successfully written to " + path) |
| 70 | + return path, nil |
| 71 | +} |
| 72 | + |
| 73 | +func getNATSContextDir(ctx context.Context) (string, error) { |
| 74 | + xdgConfigHome := core.ExtractEnv(ctx, "XDG_CONFIG_HOME") |
| 75 | + interactive.Println("xdgConfigHome:", xdgConfigHome) |
| 76 | + if xdgConfigHome == "" { |
| 77 | + homeDir := core.ExtractEnv(ctx, "HOME") |
| 78 | + if homeDir == "" { |
| 79 | + return "", fmt.Errorf("both XDG_CONFIG_HOME and HOME are not set") |
| 80 | + } |
| 81 | + return filepath.Join(homeDir, ".config", "nats", "context"), nil |
| 82 | + } |
| 83 | + return xdgConfigHome, nil |
| 84 | +} |
| 85 | + |
| 86 | +func saveNATSCredentials(ctx context.Context, creds *mnq.NatsCredentials, natsAccount *mnq.NatsAccount) (string, error) { |
| 87 | + natsContextDir, err := getNATSContextDir(ctx) |
| 88 | + if err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + credsEntity := &NatsEntity{ |
| 92 | + Name: creds.Name, |
| 93 | + Content: []byte(creds.Credentials.Content), |
| 94 | + } |
| 95 | + credsPath, err := writeFile(ctx, natsContextDir, credsEntity, "creds") |
| 96 | + if err != nil { |
| 97 | + return "", err |
| 98 | + } |
| 99 | + |
| 100 | + contextEntity := &NatsEntity{ |
| 101 | + Name: natsAccount.Name, |
| 102 | + Content: natsContextFrom(natsAccount, credsPath), |
| 103 | + } |
| 104 | + |
| 105 | + contextPath, err := writeFile(ctx, natsContextDir, contextEntity, "json") |
| 106 | + if err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + return contextPath, nil |
| 110 | +} |
| 111 | + |
| 112 | +func getNatsAccountID(ctx context.Context, args *CreateContextRequest, api *mnq.NatsAPI) (*mnq.NatsAccount, error) { |
| 113 | + var natsAccount *mnq.NatsAccount |
| 114 | + if args.NatsAccountID == "" { |
| 115 | + natsAccountsResp, err := api.ListNatsAccounts(&mnq.NatsAPIListNatsAccountsRequest{ |
| 116 | + Region: args.Region, |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("failed to list nats account: %w", err) |
| 120 | + } |
| 121 | + natsAccount, err = promptNatsAccounts(ctx, natsAccountsResp.NatsAccounts, natsAccountsResp.TotalCount) |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("failed to list nats account: %w", err) |
| 124 | + } |
| 125 | + } else { |
| 126 | + var err error |
| 127 | + natsAccount, err = api.GetNatsAccount(&mnq.NatsAPIGetNatsAccountRequest{ |
| 128 | + Region: args.Region, |
| 129 | + NatsAccountID: args.NatsAccountID, |
| 130 | + }, scw.WithContext(ctx)) |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("failed to get nats account: %w", err) |
| 133 | + } |
| 134 | + } |
| 135 | + return natsAccount, nil |
| 136 | +} |
0 commit comments