|
| 1 | +package domains |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/signal" |
| 7 | + "syscall" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pterm/pterm" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | + |
| 13 | + "github.com/NodeOps-app/createos-cli/internal/api" |
| 14 | +) |
| 15 | + |
| 16 | +func newDomainsVerifyCommand() *cli.Command { |
| 17 | + return &cli.Command{ |
| 18 | + Name: "verify", |
| 19 | + Usage: "Check DNS propagation and wait for domain verification", |
| 20 | + ArgsUsage: "<project-id> <domain-id>", |
| 21 | + Flags: []cli.Flag{ |
| 22 | + &cli.BoolFlag{ |
| 23 | + Name: "no-wait", |
| 24 | + Usage: "Check once and exit instead of polling", |
| 25 | + }, |
| 26 | + }, |
| 27 | + Action: func(c *cli.Context) error { |
| 28 | + if c.NArg() < 2 { |
| 29 | + return fmt.Errorf("please provide a project ID and domain ID\n\n Example:\n createos domains verify <project-id> <domain-id>") |
| 30 | + } |
| 31 | + |
| 32 | + client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient) |
| 33 | + if !ok { |
| 34 | + return fmt.Errorf("you're not signed in — run 'createos login' to get started") |
| 35 | + } |
| 36 | + |
| 37 | + projectID := c.Args().Get(0) |
| 38 | + domainID := c.Args().Get(1) |
| 39 | + |
| 40 | + // Trigger a refresh first |
| 41 | + if err := client.RefreshDomain(projectID, domainID); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + // Check status |
| 46 | + domains, err := client.ListDomains(projectID) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + var domain *api.Domain |
| 52 | + for i := range domains { |
| 53 | + if domains[i].ID == domainID { |
| 54 | + domain = &domains[i] |
| 55 | + break |
| 56 | + } |
| 57 | + } |
| 58 | + if domain == nil { |
| 59 | + return fmt.Errorf("domain %s not found", domainID) |
| 60 | + } |
| 61 | + |
| 62 | + if domain.Status == "verified" || domain.Status == "active" { |
| 63 | + pterm.Success.Printf("Domain %s is verified!\n", domain.Name) |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + if c.Bool("no-wait") { |
| 68 | + pterm.Warning.Printf("Domain %s status: %s\n", domain.Name, domain.Status) |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + // Poll until verified |
| 73 | + pterm.Info.Printf("Waiting for DNS verification of %s...\n", domain.Name) |
| 74 | + |
| 75 | + ticker := time.NewTicker(10 * time.Second) |
| 76 | + defer ticker.Stop() |
| 77 | + |
| 78 | + sigCh := make(chan os.Signal, 1) |
| 79 | + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 80 | + defer signal.Stop(sigCh) |
| 81 | + |
| 82 | + attempts := 0 |
| 83 | + maxAttempts := 30 // 5 minutes |
| 84 | + |
| 85 | + for { |
| 86 | + select { |
| 87 | + case <-sigCh: |
| 88 | + fmt.Println() |
| 89 | + pterm.Info.Println("Verification stopped. You can check again later with:") |
| 90 | + pterm.Println(pterm.Gray(" createos domains verify " + projectID + " " + domainID)) |
| 91 | + return nil |
| 92 | + case <-ticker.C: |
| 93 | + attempts++ |
| 94 | + if attempts > maxAttempts { |
| 95 | + pterm.Warning.Println("DNS propagation is taking longer than expected.") |
| 96 | + pterm.Println(pterm.Gray(" DNS changes can take up to 48 hours. Try again later:")) |
| 97 | + pterm.Println(pterm.Gray(" createos domains verify " + projectID + " " + domainID)) |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + _ = client.RefreshDomain(projectID, domainID) |
| 102 | + domains, err := client.ListDomains(projectID) |
| 103 | + if err != nil { |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + for _, d := range domains { |
| 108 | + if d.ID == domainID { |
| 109 | + if d.Status == "verified" || d.Status == "active" { |
| 110 | + pterm.Success.Printf("Domain %s is verified!\n", d.Name) |
| 111 | + return nil |
| 112 | + } |
| 113 | + fmt.Printf(" ⏳ Checking DNS... %s\n", d.Status) |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + }, |
| 120 | + } |
| 121 | +} |
0 commit comments