-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.go
144 lines (118 loc) · 2.8 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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"github.com/sourcegraph/jsonrpc2"
"gopkg.in/yaml.v3"
"github.com/mattn/efm-langserver/langserver"
)
const (
name = "efm-langserver"
version = "0.0.53"
)
var revision = "HEAD"
func main() {
var yamlfile string
var logfile string
var loglevel int
var dump bool
var showVersion bool
var quiet bool
flag.StringVar(&yamlfile, "c", "", "path to config.yaml")
flag.StringVar(&logfile, "logfile", "", "logfile")
flag.IntVar(&loglevel, "loglevel", 1, "loglevel")
flag.BoolVar(&dump, "d", false, "dump configuration")
flag.BoolVar(&showVersion, "v", false, "Print the version")
flag.BoolVar(&quiet, "q", false, "Run quieter")
flag.Parse()
if showVersion {
fmt.Printf("%s %s (rev: %s/%s)\n", name, version, revision, runtime.Version())
return
}
if yamlfile == "" {
var configHome string
if runtime.GOOS == "windows" {
configHome = os.Getenv("APPDATA")
} else {
configHome = os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
configHome = filepath.Join(os.Getenv("HOME"), ".config")
}
}
dir := filepath.Join(configHome, "efm-langserver")
if err := os.MkdirAll(dir, 0o700); err != nil {
log.Fatal(err)
}
yamlfile = filepath.Join(dir, "config.yaml")
} else {
_, err := os.Stat(yamlfile)
if err != nil {
log.Fatal(err)
}
}
config, err := langserver.LoadConfig(yamlfile)
if err != nil {
log.Fatal(err)
}
if dump {
err = yaml.NewEncoder(os.Stdout).Encode(&config)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
if flag.NArg() != 0 {
flag.Usage()
os.Exit(1)
}
if quiet {
log.SetOutput(io.Discard)
}
log.Println("efm-langserver: reading on stdin, writing on stdout")
if logfile == "" {
logfile = config.LogFile
}
if config.LogLevel > 0 {
loglevel = config.LogLevel
}
var connOpt []jsonrpc2.ConnOpt
if logfile != "" {
f, err := os.OpenFile(logfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0o660)
if err != nil {
log.Fatal(err)
}
defer f.Close()
config.Logger = log.New(f, "", log.LstdFlags)
if loglevel >= 5 {
connOpt = append(connOpt, jsonrpc2.LogMessages(config.Logger))
}
}
if quiet && (logfile == "" || loglevel < 5) {
connOpt = append(connOpt, jsonrpc2.LogMessages(log.New(io.Discard, "", 0)))
}
handler := langserver.NewHandler(config)
<-jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}),
handler, connOpt...).DisconnectNotify()
log.Println("efm-langserver: connections closed")
}
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()
}