/
main.go
119 lines (97 loc) · 3.01 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
package main
import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
oldLog "log"
"os"
"strings"
"github.com/juliosueiras/terraform-lsp/langserver"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var Version string
var GitCommit string
var Date string
func init() {
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
serveCmd.Bool("tcp", false, "Use TCP instead of Stdio(which is default)")
serveCmd.Int("port", 9900, "Port for TCP Server")
serveCmd.String("address", "127.0.0.1", "Address for TCP Server")
serveCmd.String("log-location", "", "Location of the lsp log")
serveCmd.String("log-jrpc2-location", "", "Location of the lsp log for jrpc2")
serveCmd.Bool("debug", false, "Enable debug output")
serveCmd.Bool("debug-jrpc2", false, "Enable debug output for jrpc2")
serveCmd.Bool("enable-log-file", false, "Enable log file")
serveCmd.Bool("enable-log-jrpc2-file", false, "Enable log file for JRPC2")
serveCmd.Bool("version", false, "Show version")
// Load config from file
configViper(serveCmd)
}
func main() {
oldLog.SetOutput(ioutil.Discard)
oldLog.SetFlags(0)
version := viper.GetBool("version")
if version {
fmt.Printf("v%s, commit: %s, build on: %s\n", strings.Trim(Version, "v"), GitCommit, Date)
return
}
debug := viper.GetBool("debug")
log.Infof("Log Level is Debug: %t", debug)
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
ForceColors: true,
DisableLevelTruncation: true,
})
if debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
enableLogFile := viper.GetBool("enable-log-file")
if enableLogFile {
location := viper.GetString("log-location")
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp.log", location), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
var oldLogInstance *oldLog.Logger
debugJRPC2 := viper.GetBool("debug-jrpc2")
tcp := viper.GetBool("tcp")
enableLogFileJRPC2 := viper.GetBool("enable-log-jrpc2-file")
if debugJRPC2 {
if !tcp && !enableLogFileJRPC2 {
log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio")
}
oldLogInstance = oldLog.New(os.Stdout, "", 0)
if enableLogFileJRPC2 {
locationJRPC2 := viper.GetString("log-jrpc2-location")
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
oldLogInstance.SetOutput(f)
}
}
langserver.InitializeServiceMap()
if tcp {
address := viper.GetString("address")
port := viper.GetInt("port")
langserver.RunTCPServer(address, port, oldLogInstance)
} else {
langserver.RunStdioServer(oldLogInstance)
}
}
func configViper(serveCmd *flag.FlagSet) {
// Accept CLI arguments
pflag.CommandLine.AddGoFlagSet(serveCmd)
if len(os.Args) >= 2 && os.Args[1] == "serve" {
serveCmd.Parse(os.Args[2:])
}
viper.BindPFlags(pflag.CommandLine)
}