-
Notifications
You must be signed in to change notification settings - Fork 18
/
serve.go
52 lines (39 loc) · 1.02 KB
/
serve.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
// +build !windows
package commands
import (
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/internal/evaluator"
"github.com/spf13/cobra"
"net"
"os"
)
var ErrServe = errors.New("serve failed")
var address string
func serve(cmd *cobra.Command, args []string) error {
// https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true
lis, err := net.Listen("tcp", address)
if err != nil {
return fmt.Errorf("%w: %v", ErrServe, err)
}
fmt.Printf("listening on %s\n", lis.Addr().String())
if err := evaluator.Serve(cmd.Context(), lis); err != nil {
return fmt.Errorf("%w: %v", ErrServe, err)
}
return nil
}
func newServeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "serve [flags]",
Short: "Run RPC server that evaluates YAML and Starlark configurations",
RunE: serve,
Hidden: true,
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
cmd.PersistentFlags().StringVarP(&address, "listen", "l", fmt.Sprintf(":%s", port), "address to listen on")
return cmd
}