summaryrefslogtreecommitdiffstats
path: root/cmd/ship/host
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ship/host')
-rw-r--r--cmd/ship/host/host.go3
-rw-r--r--cmd/ship/host/init.go8
-rw-r--r--cmd/ship/host/set_domain.go76
3 files changed, 85 insertions, 2 deletions
diff --git a/cmd/ship/host/host.go b/cmd/ship/host/host.go
index 603a946..81403f9 100644
--- a/cmd/ship/host/host.go
+++ b/cmd/ship/host/host.go
@@ -15,4 +15,7 @@ func init() {
15 Cmd.AddCommand(statusCmd) 15 Cmd.AddCommand(statusCmd)
16 Cmd.AddCommand(updateCmd) 16 Cmd.AddCommand(updateCmd)
17 Cmd.AddCommand(sshCmd) 17 Cmd.AddCommand(sshCmd)
18 Cmd.AddCommand(setDomainCmd)
19
20 initCmd.Flags().String("base-domain", "", "Base domain for auto-generated subdomains (e.g., apps.example.com)")
18} 21}
diff --git a/cmd/ship/host/init.go b/cmd/ship/host/init.go
index ea25922..27f67af 100644
--- a/cmd/ship/host/init.go
+++ b/cmd/ship/host/init.go
@@ -26,6 +26,7 @@ func runInit(cmd *cobra.Command, args []string) error {
26 if host == "" { 26 if host == "" {
27 host = st.GetDefaultHost() 27 host = st.GetDefaultHost()
28 } 28 }
29 baseDomain, _ := cmd.Flags().GetString("base-domain")
29 30
30 if host == "" { 31 if host == "" {
31 return fmt.Errorf("--host is required") 32 return fmt.Errorf("--host is required")
@@ -64,7 +65,6 @@ func runInit(cmd *cobra.Command, args []string) error {
64 65
65 fmt.Println("-> Configuring Caddy...") 66 fmt.Println("-> Configuring Caddy...")
66 caddyfile := `{ 67 caddyfile := `{
67 email admin@example.com
68} 68}
69 69
70import /etc/caddy/sites-enabled/* 70import /etc/caddy/sites-enabled/*
@@ -100,7 +100,11 @@ import /etc/caddy/sites-enabled/*
100 fmt.Println(" Caddy is active") 100 fmt.Println(" Caddy is active")
101 } 101 }
102 102
103 st.GetHost(host) 103 hostState := st.GetHost(host)
104 if baseDomain != "" {
105 hostState.BaseDomain = baseDomain
106 fmt.Printf(" Base domain: %s\n", baseDomain)
107 }
104 if st.GetDefaultHost() == "" { 108 if st.GetDefaultHost() == "" {
105 st.SetDefaultHost(host) 109 st.SetDefaultHost(host)
106 fmt.Printf(" Set %s as default host\n", host) 110 fmt.Printf(" Set %s as default host\n", host)
diff --git a/cmd/ship/host/set_domain.go b/cmd/ship/host/set_domain.go
new file mode 100644
index 0000000..fed3b31
--- /dev/null
+++ b/cmd/ship/host/set_domain.go
@@ -0,0 +1,76 @@
1package host
2
3import (
4 "fmt"
5
6 "github.com/bdw/ship/internal/state"
7 "github.com/spf13/cobra"
8)
9
10var setDomainCmd = &cobra.Command{
11 Use: "set-domain [domain]",
12 Short: "Set base domain for auto-generated subdomains",
13 Long: `Set the base domain used to auto-generate subdomains for deployments.
14
15When a base domain is configured (e.g., apps.example.com), every deployment
16will automatically get a subdomain ({name}.apps.example.com).
17
18Examples:
19 ship host set-domain apps.example.com # Set base domain
20 ship host set-domain --clear # Remove base domain`,
21 RunE: runSetDomain,
22}
23
24func init() {
25 setDomainCmd.Flags().Bool("clear", false, "Clear the base domain")
26}
27
28func runSetDomain(cmd *cobra.Command, args []string) error {
29 st, err := state.Load()
30 if err != nil {
31 return fmt.Errorf("error loading state: %w", err)
32 }
33
34 host, _ := cmd.Flags().GetString("host")
35 if host == "" {
36 host = st.GetDefaultHost()
37 }
38
39 if host == "" {
40 return fmt.Errorf("--host is required")
41 }
42
43 clear, _ := cmd.Flags().GetBool("clear")
44
45 if !clear && len(args) == 0 {
46 // Show current base domain
47 hostState := st.GetHost(host)
48 if hostState.BaseDomain == "" {
49 fmt.Printf("No base domain configured for %s\n", host)
50 } else {
51 fmt.Printf("Base domain for %s: %s\n", host, hostState.BaseDomain)
52 }
53 return nil
54 }
55
56 hostState := st.GetHost(host)
57
58 if clear {
59 hostState.BaseDomain = ""
60 if err := st.Save(); err != nil {
61 return fmt.Errorf("error saving state: %w", err)
62 }
63 fmt.Printf("Cleared base domain for %s\n", host)
64 return nil
65 }
66
67 hostState.BaseDomain = args[0]
68 if err := st.Save(); err != nil {
69 return fmt.Errorf("error saving state: %w", err)
70 }
71
72 fmt.Printf("Set base domain for %s: %s\n", host, args[0])
73 fmt.Println("\nNew deployments will automatically use subdomains like:")
74 fmt.Printf(" myapp.%s\n", args[0])
75 return nil
76}