-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (90 loc) · 1.92 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"runtime/debug"
"github.com/kitagry/bqls/langserver"
"github.com/sourcegraph/jsonrpc2"
)
const (
name = "bqls"
)
var (
version = "v0.0.0"
revision = ""
)
type exitCode int
const (
exitCodeOK exitCode = iota
exitCodeErr
)
func main() {
os.Exit(int(run(os.Args[1:])))
}
func run(args []string) exitCode {
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.SetOutput(os.Stderr)
fs.Usage = func() {
fs.SetOutput(os.Stdout)
fmt.Printf(`%[1]s - BigQuery language server
Version: %s (rev: %s/%s)
You can use your favorite lsp client.
`, name, version, getRevision(), runtime.Version())
fs.PrintDefaults()
}
showVersion := fs.Bool("version", false, "print version")
isDebug := fs.Bool("debug", false, "log debug")
if err := fs.Parse(args); err != nil {
if err == flag.ErrHelp {
return exitCodeOK
}
return exitCodeErr
}
if *showVersion {
fmt.Printf("%s %s (rev: %s/%s)\n", name, version, getRevision(), runtime.Version())
return exitCodeOK
}
handler := langserver.NewHandler(*isDebug)
defer handler.Close()
<-jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}), handler).DisconnectNotify()
return exitCodeOK
}
func getRevision() string {
if revision != "" {
return revision
}
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
var revision string
var modified string
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
revision = s.Value
case "vcs.modified":
modified = s.Value
}
}
if modified == "true" {
revision += "(modified)"
}
return revision
}
type stdrwc struct{}
func (stdrwc) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (c stdrwc) Write(p []byte) (int, error) {
return os.Stdout.Write(p)
}
func (c stdrwc) Close() error {
if err := os.Stdin.Close(); err != nil {
return err
}
return os.Stdout.Close()
}