-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
104 lines (87 loc) · 2.82 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
// 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"
"log"
"os"
"path/filepath"
"github.com/jdbaldry/go-language-server-protocol/jsonrpc2"
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
)
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).
-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"))
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" {
if i == len(os.Args)-1 {
fmt.Printf("Expected value for option %s but found none.\n", arg)
fmt.Println()
printHelp(os.Stdout)
os.Exit(1)
}
jpaths = append([]string{os.Args[i+1]}, jpaths...)
}
}
log.Println("Starting the language server")
ctx := context.Background()
stream := jsonrpc2.NewHeaderStream(stdio{})
conn := jsonrpc2.NewConn(stream)
client := protocol.ClientDispatcher(conn)
s, err := newServer(client, jpaths)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
conn.Go(ctx, protocol.Handlers(
protocol.ServerHandler(s, jsonrpc2.MethodNotFound)))
<-conn.Done()
if err := conn.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}