-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (79 loc) · 2.03 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
package main
import (
"fmt"
"main/lsportal"
"os/exec"
"regexp"
"sync"
"github.com/spf13/cobra"
"github.com/tliron/commonlog"
_ "github.com/tliron/commonlog/simple"
)
type Config struct {
regex string
exclusionRegex string
extension string
lsCmd string
lsArgs []string
debug bool
}
var config Config
var rootCmd = &cobra.Command{
Use: "lsportal <extension> <regex> <cmd> [-- lsArgs...]",
Short: "LSPortal is a language server portal",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
config.extension = args[0]
config.regex = args[1]
config.lsCmd = args[2]
// Find the index of "--" separator
sepIndex := cmd.ArgsLenAtDash()
if sepIndex != -1 {
config.lsArgs = args[sepIndex:]
}
if config.debug {
commonlog.Initialize(3, "./lsportalLog.log")
}
err := validateInputs(&config)
if err != nil {
panic(err)
}
fromClient, fromInclusion := lsportal.InitForwarders(config.debug, config.regex, config.exclusionRegex, config.extension)
readWrite, err := lsportal.StartLanguageServer(config.lsCmd, config.lsArgs)
if err != nil {
panic(fmt.Errorf("error starting language server: %v", err))
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
fromClient.RunStdio()
}()
go func() {
defer wg.Done()
fromInclusion.ServeStream(readWrite, commonlog.GetLogger("fromInclusion"))
}()
wg.Wait()
},
}
func init() {
rootCmd.Flags().StringVar(&config.exclusionRegex, "exclusion", `;([\s\S]*);`, "Regular expression for exclusion")
rootCmd.Flags().BoolVar(&config.debug, "debug", false, "enable debugg logging")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
return
}
}
func validateInputs(config *Config) error {
// Validate regex
if _, err := regexp.Compile(config.regex); err != nil {
return fmt.Errorf("Invalid regex: %v\n", err)
}
// Validate cmd
if _, err := exec.LookPath(config.lsCmd); err != nil {
return fmt.Errorf("Command not found: %v\n", err)
}
return nil
}