-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
133 lines (114 loc) · 3.75 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
// jsonnet-language-server: A Language Server Protocol server for Jsonnet.
// Copyright (C) 2021 Jack Baldry
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/grafana/jsonnet-language-server/pkg/server"
"github.com/grafana/jsonnet-language-server/pkg/utils"
"github.com/jdbaldry/go-language-server-protocol/jsonrpc2"
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
log "github.com/sirupsen/logrus"
)
const (
name = "jsonnet-language-server"
)
var (
// Set with `-ldflags="-X 'main.version=<version>'"`
version = "dev"
)
// printVersion prints version text to the provided writer.
func printVersion(w io.Writer) {
fmt.Fprintf(w, "%s version %s\n", name, version)
}
// printVersion prints help text to the provided writer.
func printHelp(w io.Writer) {
printVersion(w)
fmt.Fprintf(w, `
Options:
-h / --help Print this help message.
-J / --jpath <dir> Specify an additional library search dir
(right-most wins).
-t / --tanka Create the jsonnet VM with Tanka (finds jpath automatically).
-l / --log-level Set the log level (default: info).
--eval-diags Try to evaluate files to find errors and warnings.
--lint Enable linting.
-v / --version Print version.
Environment variables:
JSONNET_PATH is a %[2]q separated list of directories
added in reverse order before the paths specified by --jpath
These are equivalent:
JSONNET_PATH=a%[2]cb %[1]s -J c -J d
JSONNET_PATH=d%[2]cc%[2]ca%[2]cb %[1]s\n
%[1]s -J b -J a -J c -J d
`, name, filepath.ListSeparator)
}
func main() {
jpaths := filepath.SplitList(os.Getenv("JSONNET_PATH"))
tankaMode := false
lint := false
evalDiags := false
log.SetLevel(log.InfoLevel)
for i, arg := range os.Args {
if arg == "-h" || arg == "--help" {
printHelp(os.Stdout)
os.Exit(0)
} else if arg == "-v" || arg == "--version" {
printVersion(os.Stdout)
os.Exit(0)
} else if arg == "-J" || arg == "--jpath" {
jpaths = append([]string{getArgValue(i)}, jpaths...)
} else if arg == "-t" || arg == "--tanka" {
tankaMode = true
} else if arg == "-l" || arg == "--log-level" {
logLevel, err := log.ParseLevel(getArgValue(i))
if err != nil {
log.Fatalf("Invalid log level: %s", err)
}
log.SetLevel(logLevel)
} else if arg == "--lint" {
lint = true
} else if arg == "--eval-diags" {
evalDiags = true
}
}
log.Infoln("Starting the language server")
ctx := context.Background()
stream := jsonrpc2.NewHeaderStream(utils.Stdio{})
conn := jsonrpc2.NewConn(stream)
client := protocol.ClientDispatcher(conn)
s := server.NewServer(name, version, client)
if tankaMode {
s = s.WithTankaVM(jpaths)
} else {
s = s.WithStaticVM(jpaths)
}
s.LintDiags = lint
s.EvalDiags = evalDiags
conn.Go(ctx, protocol.Handlers(
protocol.ServerHandler(s, jsonrpc2.MethodNotFound)))
<-conn.Done()
if err := conn.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func getArgValue(i int) string {
if i == len(os.Args)-1 {
printHelp(os.Stdout)
log.Fatalf("Expected value for option %s but found none.", os.Args[i])
}
return os.Args[i+1]
}