|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "text/tabwriter" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/manifoldco/promptui" |
| 12 | + "github.com/oursky/pageship/internal/api" |
| 13 | + "github.com/oursky/pageship/internal/models" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/spf13/viper" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + rootCmd.AddCommand(domainsCmd) |
| 20 | + domainsCmd.PersistentFlags().String("app", "", "app ID") |
| 21 | + |
| 22 | + domainsCmd.AddCommand(domainsActivateCmd) |
| 23 | + domainsCmd.AddCommand(domainsDeactivateCmd) |
| 24 | +} |
| 25 | + |
| 26 | +var domainsCmd = &cobra.Command{ |
| 27 | + Use: "domains", |
| 28 | + Short: "Manage custom domains", |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + appID := viper.GetString("app") |
| 31 | + if appID == "" { |
| 32 | + appID = tryLoadAppID() |
| 33 | + } |
| 34 | + if appID == "" { |
| 35 | + return fmt.Errorf("app ID is not set") |
| 36 | + } |
| 37 | + |
| 38 | + manifest, err := API().GetManifest(cmd.Context()) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("failed to get manifest: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + app, err := API().GetApp(cmd.Context(), appID) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to get app: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + type domainEntry struct { |
| 49 | + name string |
| 50 | + site string |
| 51 | + model *api.APIDomain |
| 52 | + } |
| 53 | + domains := map[string]domainEntry{} |
| 54 | + for _, dconf := range app.Config.Domains { |
| 55 | + domains[dconf.Domain] = domainEntry{ |
| 56 | + name: dconf.Domain, |
| 57 | + site: dconf.Site, |
| 58 | + model: nil, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + apiDomains, err := API().ListDomains(cmd.Context(), appID) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to list domains: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + for _, d := range apiDomains { |
| 68 | + dd := d |
| 69 | + domains[d.Domain.Domain] = domainEntry{ |
| 70 | + name: d.Domain.Domain, |
| 71 | + site: d.Domain.SiteName, |
| 72 | + model: &dd, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + w := tabwriter.NewWriter(os.Stdout, 1, 4, 4, ' ', 0) |
| 77 | + fmt.Fprintln(w, "NAME\tSITE\tCREATED AT\tSTATUS") |
| 78 | + for _, domain := range domains { |
| 79 | + createdAt := "-" |
| 80 | + site := "-" |
| 81 | + if domain.model != nil { |
| 82 | + createdAt = domain.model.CreatedAt.Local().Format(time.DateTime) |
| 83 | + site = fmt.Sprintf("%s/%s", domain.model.AppID, domain.model.SiteName) |
| 84 | + } else { |
| 85 | + site = fmt.Sprintf("%s/%s", app.ID, domain.site) |
| 86 | + } |
| 87 | + |
| 88 | + var status string |
| 89 | + switch { |
| 90 | + case domain.model != nil && domain.model.AppID != app.ID: |
| 91 | + status = "IN_USE" |
| 92 | + case domain.model != nil && domain.model.AppID == app.ID: |
| 93 | + status = "ACTIVE" |
| 94 | + default: |
| 95 | + status = "INACTIVE" |
| 96 | + } |
| 97 | + |
| 98 | + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", domain.name, site, createdAt, status) |
| 99 | + } |
| 100 | + w.Flush() |
| 101 | + |
| 102 | + if manifest.CustomDomainMessage != "" { |
| 103 | + os.Stdout.WriteString("\n") |
| 104 | + Info(manifest.CustomDomainMessage) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | + }, |
| 109 | +} |
| 110 | + |
| 111 | +func promptDomainReplaceApp(ctx context.Context, appID string, domainName string) (replaceApp string, err error) { |
| 112 | + domains, err := API().ListDomains(ctx, appID) |
| 113 | + if err != nil { |
| 114 | + return "", fmt.Errorf("failed list domain: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + appID = "" |
| 118 | + for _, d := range domains { |
| 119 | + if d.Domain.Domain == domainName { |
| 120 | + appID = d.AppID |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if appID == "" { |
| 125 | + return "", models.ErrDomainUsedName |
| 126 | + } |
| 127 | + |
| 128 | + label := fmt.Sprintf("Domain %q is in use by app %q; activates the domain anyways", domainName, appID) |
| 129 | + |
| 130 | + prompt := promptui.Prompt{Label: label, IsConfirm: true} |
| 131 | + _, err = prompt.Run() |
| 132 | + if err != nil { |
| 133 | + Info("Cancelled.") |
| 134 | + return "", ErrCancelled |
| 135 | + } |
| 136 | + |
| 137 | + return appID, nil |
| 138 | +} |
| 139 | + |
| 140 | +var domainsActivateCmd = &cobra.Command{ |
| 141 | + Use: "activate", |
| 142 | + Short: "Activate domain for the app", |
| 143 | + Args: cobra.ExactArgs(1), |
| 144 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 145 | + domainName := args[0] |
| 146 | + |
| 147 | + appID := viper.GetString("app") |
| 148 | + if appID == "" { |
| 149 | + appID = tryLoadAppID() |
| 150 | + } |
| 151 | + if appID == "" { |
| 152 | + return fmt.Errorf("app ID is not set") |
| 153 | + } |
| 154 | + |
| 155 | + app, err := API().GetApp(cmd.Context(), appID) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("failed to get app: %w", err) |
| 158 | + } |
| 159 | + if _, ok := app.Config.ResolveDomain(domainName); !ok { |
| 160 | + return fmt.Errorf("undefined domain") |
| 161 | + } |
| 162 | + |
| 163 | + _, err = API().CreateDomain(cmd.Context(), appID, domainName, "") |
| 164 | + if code, ok := api.ErrorStatusCode(err); ok && code == http.StatusConflict { |
| 165 | + var replaceApp string |
| 166 | + replaceApp, err = promptDomainReplaceApp(cmd.Context(), appID, domainName) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + _, err = API().CreateDomain(cmd.Context(), appID, domainName, replaceApp) |
| 171 | + } |
| 172 | + |
| 173 | + if err != nil { |
| 174 | + return fmt.Errorf("failed to create domain: %w", err) |
| 175 | + } |
| 176 | + |
| 177 | + Info("Domain %q activated.", domainName) |
| 178 | + return nil |
| 179 | + }, |
| 180 | +} |
| 181 | + |
| 182 | +var domainsDeactivateCmd = &cobra.Command{ |
| 183 | + Use: "deactivate", |
| 184 | + Short: "Deactivate domain for the app", |
| 185 | + Args: cobra.ExactArgs(1), |
| 186 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 187 | + domainName := args[0] |
| 188 | + |
| 189 | + appID := viper.GetString("app") |
| 190 | + if appID == "" { |
| 191 | + appID = tryLoadAppID() |
| 192 | + } |
| 193 | + if appID == "" { |
| 194 | + return fmt.Errorf("app ID is not set") |
| 195 | + } |
| 196 | + |
| 197 | + _, err := API().DeleteDomain(cmd.Context(), appID, domainName) |
| 198 | + if err != nil { |
| 199 | + return fmt.Errorf("failed to delete domain: %w", err) |
| 200 | + } |
| 201 | + |
| 202 | + Info("Domain %q deactivated.", domainName) |
| 203 | + return nil |
| 204 | + }, |
| 205 | +} |
0 commit comments