summaryrefslogtreecommitdiffstats
path: root/cmd/ship/host/set_domain.go
blob: fed3b312393ef60c9e9e25fc258eabf4bf633071 (plain)
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
package host

import (
	"fmt"

	"github.com/bdw/ship/internal/state"
	"github.com/spf13/cobra"
)

var setDomainCmd = &cobra.Command{
	Use:   "set-domain [domain]",
	Short: "Set base domain for auto-generated subdomains",
	Long: `Set the base domain used to auto-generate subdomains for deployments.

When a base domain is configured (e.g., apps.example.com), every deployment
will automatically get a subdomain ({name}.apps.example.com).

Examples:
  ship host set-domain apps.example.com    # Set base domain
  ship host set-domain --clear             # Remove base domain`,
	RunE: runSetDomain,
}

func init() {
	setDomainCmd.Flags().Bool("clear", false, "Clear the base domain")
}

func runSetDomain(cmd *cobra.Command, args []string) error {
	st, err := state.Load()
	if err != nil {
		return fmt.Errorf("error loading state: %w", err)
	}

	host, _ := cmd.Flags().GetString("host")
	if host == "" {
		host = st.GetDefaultHost()
	}

	if host == "" {
		return fmt.Errorf("--host is required")
	}

	clear, _ := cmd.Flags().GetBool("clear")

	if !clear && len(args) == 0 {
		// Show current base domain
		hostState := st.GetHost(host)
		if hostState.BaseDomain == "" {
			fmt.Printf("No base domain configured for %s\n", host)
		} else {
			fmt.Printf("Base domain for %s: %s\n", host, hostState.BaseDomain)
		}
		return nil
	}

	hostState := st.GetHost(host)

	if clear {
		hostState.BaseDomain = ""
		if err := st.Save(); err != nil {
			return fmt.Errorf("error saving state: %w", err)
		}
		fmt.Printf("Cleared base domain for %s\n", host)
		return nil
	}

	hostState.BaseDomain = args[0]
	if err := st.Save(); err != nil {
		return fmt.Errorf("error saving state: %w", err)
	}

	fmt.Printf("Set base domain for %s: %s\n", host, args[0])
	fmt.Println("\nNew deployments will automatically use subdomains like:")
	fmt.Printf("  myapp.%s\n", args[0])
	return nil
}