summaryrefslogtreecommitdiffstats
path: root/cmd/ship/restart.go
blob: 404624ae45b8030aeeb375d26e731ea09694b50b (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
package main

import (
	"fmt"

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

var restartCmd = &cobra.Command{
	Use:   "restart <app>",
	Short: "Restart a deployment",
	Args:  cobra.ExactArgs(1),
	RunE:  runRestart,
}

func runRestart(cmd *cobra.Command, args []string) error {
	name := args[0]

	st, err := state.Load()
	if err != nil {
		return fmt.Errorf("error loading state: %w", err)
	}

	host := hostFlag
	if host == "" {
		host = st.GetDefaultHost()
	}

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

	app, err := st.GetApp(host, name)
	if err != nil {
		return err
	}

	if app.Type != "app" && app.Type != "git-app" {
		return fmt.Errorf("restart is only available for apps, not static sites")
	}

	client, err := ssh.Connect(host)
	if err != nil {
		return fmt.Errorf("error connecting to VPS: %w", err)
	}
	defer client.Close()

	fmt.Printf("Restarting %s...\n", name)
	if _, err := client.RunSudo(fmt.Sprintf("systemctl restart %s", name)); err != nil {
		return fmt.Errorf("error restarting service: %w", err)
	}

	fmt.Println("Service restarted successfully")
	return nil
}