forked from authzed/spicedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp.go
65 lines (55 loc) · 1.77 KB
/
lsp.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
package cmd
import (
"context"
"time"
"github.com/go-logr/zerologr"
"github.com/jzelinskie/cobrautil/v2"
"github.com/jzelinskie/cobrautil/v2/cobrazerolog"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/internal/lsp"
"github.com/authzed/spicedb/pkg/cmd/termination"
"github.com/authzed/spicedb/pkg/releases"
)
// LSPConfig is the configuration for the LSP command.
type LSPConfig struct {
// Addr is the address to listen on to serve the language server protocol.
Addr string
Stdio bool
}
// Complete adapts the LSPConfig into a usable LSP server.
func (c *LSPConfig) Complete(ctx context.Context) (*lsp.Server, error) {
return lsp.NewServer(), nil
}
func RegisterLSPFlags(cmd *cobra.Command, config *LSPConfig) error {
cmd.Flags().StringVar(&config.Addr, "addr", "-", "address to listen on to serve LSP")
cmd.Flags().BoolVar(&config.Stdio, "stdio", true, "enable stdio mode for LSP")
return nil
}
func NewLSPCommand(programName string, config *LSPConfig) *cobra.Command {
return &cobra.Command{
Use: "lsp",
Short: "serve language server protocol",
PreRunE: cobrautil.CommandStack(
cobrautil.SyncViperDotEnvPreRunE(programName, "spicedb.env", zerologr.New(&logging.Logger)),
cobrazerolog.New(
cobrazerolog.WithTarget(func(logger zerolog.Logger) {
logging.SetGlobalLogger(logger)
}),
).RunE(),
releases.CheckAndLogRunE(),
),
RunE: termination.PublishError(func(cmd *cobra.Command, args []string) error {
srv, err := config.Complete(cmd.Context())
if err != nil {
return err
}
signalctx := SignalContextWithGracePeriod(
context.Background(),
time.Second*0, // No grace period
)
return srv.Run(signalctx, config.Addr, config.Stdio)
}),
}
}