-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.go
188 lines (165 loc) · 3.98 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/sourcegraph/jsonrpc2"
"github.com/urfave/cli/v2"
"github.com/sqls-server/sqls/internal/config"
"github.com/sqls-server/sqls/internal/handler"
)
const name = "sqls"
const version = "0.2.28"
var revision = "HEAD"
func main() {
if err := realMain(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
os.Exit(0)
}
func realMain() error {
app := &cli.App{
Name: "sqls",
Version: fmt.Sprintf("Version:%s, Revision:%s\n", version, revision),
Usage: "An implementation of the Language Server Protocol for SQL.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log",
Aliases: []string{"l"},
Usage: "Also log to this file. (in addition to stderr)",
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Specifies an alternative per-user configuration file. If a configuration file is given on the command line, the workspace option (initializationOptions) will be ignored.",
},
&cli.BoolFlag{
Name: "trace",
Aliases: []string{"t"},
Usage: "Print all requests and responses.",
},
},
Commands: cli.Commands{
{
Name: "config",
Aliases: []string{"c"},
Usage: "edit config",
Action: func(c *cli.Context) error {
editorEnv := os.Getenv("EDITOR")
if editorEnv == "" {
editorEnv = "vim"
}
return openEditor(editorEnv, config.YamlConfigPath)
},
},
},
Action: func(c *cli.Context) error {
return serve(c)
},
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "Print version.",
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "Print help.",
}
err := app.Run(os.Args)
if err != nil {
return err
}
return nil
}
func serve(c *cli.Context) error {
logfile := c.String("log")
configFile := c.String("config")
trace := c.Bool("trace")
// Initialize log writer
var logWriter io.Writer
if logfile != "" {
f, err := os.OpenFile(logfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
if err != nil {
log.Fatal(err)
}
defer f.Close()
logWriter = io.MultiWriter(os.Stderr, f)
} else {
logWriter = io.MultiWriter(os.Stderr)
}
log.SetOutput(logWriter)
// Initialize language server
server := handler.NewServer()
defer func() {
if err := server.Stop(); err != nil {
log.Println(err)
}
}()
h := jsonrpc2.HandlerWithError(server.Handle)
// Load specific config
if configFile != "" {
cfg, err := config.GetConfig(configFile)
if err != nil {
return fmt.Errorf("cannot read specified config, %w", err)
}
server.SpecificFileCfg = cfg
} else {
// Load default config
cfg, err := config.GetDefaultConfig()
if err != nil && !errors.Is(config.ErrNotFoundConfig, err) {
return fmt.Errorf("cannot read default config, %w", err)
}
server.DefaultFileCfg = cfg
}
// Set connect option
var connOpt []jsonrpc2.ConnOpt
if trace {
connOpt = append(connOpt, jsonrpc2.LogMessages(log.New(logWriter, "", 0)))
}
// Start language server
log.Println("sqls: reading on stdin, writing on stdout")
<-jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}),
h,
connOpt...,
).DisconnectNotify()
log.Println("sqls: connections closed")
return nil
}
type stdrwc struct{}
func (stdrwc) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (stdrwc) Write(p []byte) (int, error) {
return os.Stdout.Write(p)
}
func (stdrwc) Close() error {
if err := os.Stdin.Close(); err != nil {
return err
}
return os.Stdout.Close()
}
func openEditor(program string, args ...string) error {
cmdargs := strings.Join(args, " ")
command := program + " " + cmdargs
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", command)
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}