-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
daemon.go
78 lines (72 loc) · 1.85 KB
/
daemon.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
// Copyright 2020-2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a GPL-3.0
// license that can be found in the LICENSE file.
package cmd
import (
"context"
"fmt"
"log"
"os"
"changkun.de/x/midgard/api/daemon"
"changkun.de/x/midgard/internal/service"
"changkun.de/x/midgard/internal/types/proto"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
)
// daemonCmd runs the midgard's daemon process.
var daemonCmd = &cobra.Command{
Use: "daemon [install|uninstall|start|stop|run|ls]",
Short: "Interact with the midgard daemon(s)",
Long: `Interact with the midgard daemon(s)`,
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
s, err := service.NewService(
"midgard-daemon",
"midgard daemon",
"the Midgard daemon process",
[]string{"daemon", "run"},
)
if err != nil {
log.Printf("failed to start daemon, err: %v", err)
return
}
defer func() {
if err != nil {
log.Printf("failed to %s, err: %v", args[0], err)
return
}
if args[0] != "ls" {
log.Printf("%s action is done.", args[0])
}
}()
switch args[0] {
case "install":
err = s.Install()
case "uninstall":
err = s.Remove()
case "start":
err = s.Start()
case "stop":
err = s.Stop()
case "run":
m := daemon.NewDaemon()
onStart, onStop := m.Run(context.Background())
err = s.Run(onStart, onStop)
if err == nil {
os.Exit(0) // this closes clipboard NSApplication on darwin
}
case "ls":
daemon.Connect(func(ctx context.Context, c proto.MidgardClient) {
out, err := c.ListDaemons(ctx, &proto.ListDaemonsInput{})
if err != nil {
log.Println("cannot list daemons:", status.Convert(err).Message())
return
}
log.Println("active daemons:")
fmt.Println(out.Daemons)
})
default:
err = fmt.Errorf("%s is not a valid action", args[0])
}
},
}