-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
- Loading branch information
Showing
10 changed files
with
468 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/tensorchord/envd/pkg/lang/lsp" | ||
"github.com/tensorchord/envd/pkg/version" | ||
cli "github.com/urfave/cli/v2" | ||
"go.lsp.dev/protocol" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// go lsp server uses zap, thus we have to keep two loggers (logrus and zap) in envd. | ||
// zap is only used in lsp subcommand. | ||
var logLevel = zap.NewAtomicLevelAt(zapcore.WarnLevel) | ||
|
||
func startLSP(clicontext *cli.Context) error { | ||
logger, cleanup := newzapLogger() | ||
defer cleanup() | ||
ctx := protocol.WithLogger(clicontext.Context, logger) | ||
|
||
s := lsp.New() | ||
err := s.Start(ctx, clicontext.String("address")) | ||
return err | ||
} | ||
|
||
func newzapLogger() (logger *zap.Logger, cleanup func()) { | ||
cfg := zap.NewDevelopmentConfig() | ||
cfg.Level = logLevel | ||
cfg.Development = false | ||
logger, err := cfg.Build() | ||
if err != nil { | ||
panic(fmt.Errorf("failed to initialize logger: %v", err)) | ||
} | ||
|
||
cleanup = func() { | ||
_ = logger.Sync() | ||
} | ||
return logger, cleanup | ||
} | ||
|
||
func main() { | ||
cli.VersionPrinter = func(c *cli.Context) { | ||
fmt.Println(c.App.Name, version.Package, version.GetVersion().String()) | ||
} | ||
|
||
app := cli.NewApp() | ||
app.Name = "envd-lsp" | ||
app.Usage = "language server for envd" | ||
app.Version = version.GetVersion().String() | ||
app.Flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "address", | ||
Usage: "Address (hostname:port) to listen on", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "debug", | ||
Usage: "Enable debug logging", | ||
}, | ||
} | ||
|
||
// Deal with debug flag. | ||
var debugEnabled bool | ||
|
||
app.Before = func(context *cli.Context) error { | ||
debugEnabled = context.Bool("debug") | ||
|
||
if debugEnabled { | ||
logLevel.SetLevel(zapcore.DebugLevel) | ||
} | ||
return nil | ||
} | ||
|
||
app.Action = startLSP | ||
handleErr(debugEnabled, app.Run(os.Args)) | ||
} | ||
|
||
func handleErr(debug bool, err error) { | ||
if err == nil { | ||
return | ||
} | ||
if debug { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
} else { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
} | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package envd | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
//go:embed api/*.py api/**/*.py | ||
var api embed.FS | ||
|
||
const autoGeneratedHeader = `### AUTO-GENERATED ### | ||
# To make changes, modify the following file in the Tilt repository: | ||
# envd/%s | ||
` | ||
|
||
func ApiStubs() fs.FS { | ||
f, err := fs.Sub(api, "api") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return f | ||
} | ||
|
||
func WalkApiStubs(fn fs.WalkDirFunc) error { | ||
return fs.WalkDir(api, "api", fn) | ||
} | ||
|
||
func DumpApiStubs(dir string, callback func(string, error)) error { | ||
return WalkApiStubs(func(path string, d fs.DirEntry, e error) error { | ||
if e != nil { | ||
return e | ||
} | ||
var err error | ||
dest := filepath.Join(dir, path) | ||
if d.IsDir() { | ||
err = os.MkdirAll(dest, 0755) | ||
} else { | ||
buf := bytes.NewBufferString(fmt.Sprintf(autoGeneratedHeader, path)) | ||
var bytes []byte | ||
bytes, err = api.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
buf.Write(bytes) | ||
err = os.WriteFile(dest, buf.Bytes(), 0644) | ||
} | ||
callback(path, err) | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.