-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.go
More file actions
88 lines (74 loc) · 2.39 KB
/
Copy pathcreate.go
File metadata and controls
88 lines (74 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package domains
import (
"fmt"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/NodeOps-app/createos-cli/internal/api"
"github.com/NodeOps-app/createos-cli/internal/cmdutil"
"github.com/NodeOps-app/createos-cli/internal/output"
)
func newDomainsCreateCommand() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "Add a custom domain to a project",
Flags: []cli.Flag{
&cli.StringFlag{Name: "project", Usage: "Project ID"},
&cli.StringFlag{Name: "name", Usage: "Domain name to add (e.g. example.com)"},
&cli.StringFlag{Name: "environment", Usage: "Environment ID to link the domain to"},
},
Action: func(c *cli.Context) error {
client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient)
if !ok {
return fmt.Errorf("you're not signed in — run 'createos login' to get started")
}
projectID, err := cmdutil.ResolveProjectID(c.String("project"))
if err != nil {
return err
}
name := c.String("name")
if name == "" {
return fmt.Errorf("please provide a domain name with --name (e.g. --name example.com)")
}
environmentID, err := resolveEnvironmentForDomain(c, client, projectID)
if err != nil {
return err
}
id, err := client.AddDomain(projectID, name, environmentID)
if err != nil {
return err
}
pterm.Success.Printf("Domain %q added successfully.\n", name)
fmt.Println()
// Fetch domain to get DNS records
domains, err := client.ListDomains(projectID)
if err == nil {
for _, d := range domains {
if d.ID == id {
printDNSRecords(d)
return nil
}
}
}
// Fallback if records not yet available
fmt.Println(" DNS records are being generated. Run 'createos domains verify' to check status.")
return nil
},
}
}
func printDNSRecords(d api.Domain) {
if d.Records == nil || (len(d.Records.ARecords) == 0 && len(d.Records.TXTRecords) == 0) {
fmt.Println(" DNS records are being generated. Run verify to check status.")
return
}
fmt.Println(" Configure your DNS with the following records:")
fmt.Println()
tableData := pterm.TableData{{"Type", "Name", "Value"}}
for _, a := range d.Records.ARecords {
tableData = append(tableData, []string{"A", d.Name, a})
}
for _, txt := range d.Records.TXTRecords {
tableData = append(tableData, []string{"TXT", txt.Name + "." + d.Name, txt.Value})
}
_ = output.RenderTable(tableData) //nolint:errcheck
fmt.Println()
}