|
| 1 | +package domain |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/fastly/cli/pkg/argparser" |
| 9 | + fsterr "github.com/fastly/cli/pkg/errors" |
| 10 | + "github.com/fastly/cli/pkg/global" |
| 11 | + "github.com/fastly/go-fastly/v10/fastly" |
| 12 | + "github.com/fastly/go-fastly/v10/fastly/domains/v1/tools/status" |
| 13 | +) |
| 14 | + |
| 15 | +// GetDomainStatusCommand calls the Fastly API to check the availability of a domain name. |
| 16 | +type GetDomainStatusCommand struct { |
| 17 | + argparser.Base |
| 18 | + argparser.JSONOutput |
| 19 | + // Required. |
| 20 | + domain string |
| 21 | + // Optional. |
| 22 | + scope argparser.OptionalString |
| 23 | +} |
| 24 | + |
| 25 | +// NewDomainStatusCommand returns a usable DomainStatusCommand registered under the parent. |
| 26 | +func NewDomainStatusCommand(parent argparser.Registerer, g *global.Data) *GetDomainStatusCommand { |
| 27 | + cmd := GetDomainStatusCommand{ |
| 28 | + Base: argparser.Base{ |
| 29 | + Globals: g, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + cmd.CmdClause = parent.Command("status", "Check domain name availability") |
| 34 | + // Required. |
| 35 | + cmd.CmdClause.Arg("domain", "Domain name to check").Required().StringVar(&cmd.domain) |
| 36 | + // Optional. |
| 37 | + cmd.RegisterFlagBool(cmd.JSONFlag()) |
| 38 | + cmd.CmdClause.Flag("scope", "Specify `--scope=estimate` to perform an “estimated” availability check, which checks the DNS and domain aftermarkets, not domain registries").Action(cmd.scope.Set).StringVar(&cmd.scope.Value) |
| 39 | + |
| 40 | + return &cmd |
| 41 | +} |
| 42 | + |
| 43 | +// Exec invokes the application logic for the command. |
| 44 | +func (g *GetDomainStatusCommand) Exec(_ io.Reader, out io.Writer) error { |
| 45 | + if g.Globals.Verbose() && g.JSONOutput.Enabled { |
| 46 | + return fsterr.ErrInvalidVerboseJSONCombo |
| 47 | + } |
| 48 | + |
| 49 | + input := &status.GetInput{ |
| 50 | + Domain: g.domain, |
| 51 | + } |
| 52 | + |
| 53 | + if g.scope.WasSet { |
| 54 | + scope := status.Scope(g.scope.Value) |
| 55 | + if scope != status.ScopeEstimate { |
| 56 | + return fsterr.RemediationError{ |
| 57 | + Inner: errors.New("invalid scope provided"), |
| 58 | + Remediation: "Use `--scope=estimate` for an estimated status check", |
| 59 | + } |
| 60 | + } |
| 61 | + input.Scope = fastly.ToPointer(scope) |
| 62 | + } |
| 63 | + |
| 64 | + fc, ok := g.Globals.APIClient.(*fastly.Client) |
| 65 | + if !ok { |
| 66 | + return errors.New("failed to acquire the Fastly API client") |
| 67 | + } |
| 68 | + |
| 69 | + st, err := status.Get(fc, input) |
| 70 | + if err != nil { |
| 71 | + g.Globals.ErrLog.Add(err) |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if ok, err := g.WriteJSON(out, st); ok { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + printStatusSummary(out, st) |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// printStatusSummary displays the information returned from the API in a summarized format. |
| 84 | +func printStatusSummary(w io.Writer, st *status.Status) { |
| 85 | + fmt.Fprintf(w, "Domain: %s\n", st.Domain) |
| 86 | + fmt.Fprintf(w, "Zone: %s\n", st.Zone) |
| 87 | + fmt.Fprintf(w, "Status: %s\n", st.Status) |
| 88 | + fmt.Fprintf(w, "Tags: %s\n", st.Tags) |
| 89 | + |
| 90 | + if st.Scope != nil { |
| 91 | + fmt.Fprintf(w, "Scope: %s\n", *st.Scope) |
| 92 | + } |
| 93 | + |
| 94 | + if len(st.Offers) > 0 { |
| 95 | + fmt.Fprintf(w, "Offers:\n") |
| 96 | + for _, o := range st.Offers { |
| 97 | + fmt.Fprintf(w, " - Vendor: %s\n", o.Vendor) |
| 98 | + fmt.Fprintf(w, " Currency: %s\n", o.Currency) |
| 99 | + fmt.Fprintf(w, " Price: %s\n", o.Price) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments