-
Notifications
You must be signed in to change notification settings - Fork 224
/
domains.go
166 lines (129 loc) · 4.48 KB
/
domains.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cmd
import (
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
)
func newDomainsCommand(client *client.Client) *Command {
domainsStrings := docstrings.Get("domains")
cmd := BuildCommandKS(nil, nil, domainsStrings, client, requireSession)
cmd.Hidden = true
listStrings := docstrings.Get("domains.list")
listCmd := BuildCommandKS(cmd, runDomainsList, listStrings, client, requireSession)
listCmd.Args = cobra.MaximumNArgs(1)
listCmd.AddBoolFlag(BoolFlagOpts{Name: "json", Shorthand: "j", Description: "JSON output"})
showCmd := BuildCommandKS(cmd, runDomainsShow, docstrings.Get("domains.show"), client, requireSession)
showCmd.Args = cobra.ExactArgs(1)
showCmd.AddBoolFlag(BoolFlagOpts{Name: "json", Shorthand: "j", Description: "JSON output"})
addCmd := BuildCommandKS(cmd, runDomainsCreate, docstrings.Get("domains.add"), client, requireSession)
addCmd.Args = cobra.MaximumNArgs(2)
registerCmd := BuildCommandKS(cmd, runDomainsRegister, docstrings.Get("domains.register"), client, requireSession)
registerCmd.Args = cobra.MaximumNArgs(2)
registerCmd.Hidden = true
return cmd
}
func runDomainsList(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
var orgSlug string
if len(cmdCtx.Args) == 0 {
org, err := selectOrganization(ctx, cmdCtx.Client.API(), "")
if err != nil {
return err
}
orgSlug = org.Slug
} else {
// TODO: Validity check on org
orgSlug = cmdCtx.Args[0]
}
domains, err := cmdCtx.Client.API().GetDomains(ctx, orgSlug)
if err != nil {
return err
}
if cmdCtx.OutputJSON() {
cmdCtx.WriteJSON(domains)
return nil
}
table := tablewriter.NewWriter(cmdCtx.Out)
table.SetHeader([]string{"Domain", "Registration Status", "DNS Status", "Created"})
for _, domain := range domains {
table.Append([]string{domain.Name, *domain.RegistrationStatus, *domain.DnsStatus, presenters.FormatRelativeTime(domain.CreatedAt)})
}
table.Render()
return nil
}
func runDomainsShow(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
name := cmdCtx.Args[0]
domain, err := cmdCtx.Client.API().GetDomain(ctx, name)
if err != nil {
return err
}
if cmdCtx.OutputJSON() {
cmdCtx.WriteJSON(domain)
return nil
}
cmdCtx.Statusf("domains", cmdctx.STITLE, "Domain\n")
fmtstring := "%-20s: %-20s\n"
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Name", domain.Name)
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Organization", domain.Organization.Slug)
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Registration Status", *domain.RegistrationStatus)
if *domain.RegistrationStatus == "REGISTERED" {
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Expires At", presenters.FormatTime(domain.ExpiresAt))
autorenew := ""
if *domain.AutoRenew {
autorenew = "Enabled"
} else {
autorenew = "Disabled"
}
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Auto Renew", autorenew)
}
cmdCtx.StatusLn()
cmdCtx.Statusf("domains", cmdctx.STITLE, "DNS\n")
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Status", *domain.DnsStatus)
if *domain.RegistrationStatus == "UNMANAGED" {
cmdCtx.Statusf("domains", cmdctx.SINFO, fmtstring, "Nameservers", strings.Join(*domain.ZoneNameservers, " "))
}
return nil
}
func runDomainsCreate(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
var org *api.Organization
var name string
var err error
if len(cmdCtx.Args) == 0 {
org, err = selectOrganization(ctx, cmdCtx.Client.API(), "")
if err != nil {
return err
}
prompt := &survey.Input{Message: "Domain name to add"}
err := survey.AskOne(prompt, &name)
checkErr(err)
// TODO: Add some domain validation here
} else if len(cmdCtx.Args) == 2 {
org, err = cmdCtx.Client.API().GetOrganizationBySlug(ctx, cmdCtx.Args[0])
if err != nil {
return err
}
name = cmdCtx.Args[1]
} else {
return errors.New("specify all arguments (or no arguments to be prompted)")
}
fmt.Printf("Creating domain %s in organization %s\n", name, org.Slug)
domain, err := cmdCtx.Client.API().CreateDomain(org.ID, name)
if err != nil {
return err
}
fmt.Println("Created domain", domain.Name)
return nil
}
func runDomainsRegister(_ *cmdctx.CmdContext) error {
return fmt.Errorf("This command is no longer supported.\n")
}