Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version option #62

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ builds:
- amd64
- arm64
- 386
ldflags:
- -X main.version={{ .Version }}
- -X main.revision={{ .ShortCommit }}

archives:
- replacements:
Expand Down
76 changes: 76 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,91 @@ package main

import (
"context"
"flag"
"fmt"
"os"
"runtime"
"runtime/debug"

"github.com/kitagry/regols/langserver"
"github.com/sourcegraph/jsonrpc2"
)

const (
name = "regols"
)

var (
version = "v0.0.0"
revision = ""
)

type exitCode int

const (
exitCodeOK exitCode = iota
exitCodeErr
)

func main() {
os.Exit(int(run(os.Args[1:])))
}

func run(args []string) exitCode {
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.SetOutput(os.Stderr)
fs.Usage = func() {
fs.SetOutput(os.Stdout)
fmt.Printf(`%[1]s - OPA rego language server

Version: %s (rev: %s/%s)

You can use your favorite lsp client.
`, name, version, getRevision(), runtime.Version())
fs.PrintDefaults()
}

var showVersion bool
fs.BoolVar(&showVersion, "version", false, "print version")
if err := fs.Parse(args); err != nil {
if err == flag.ErrHelp {
return exitCodeOK
}
return exitCodeErr
}

if showVersion {
fmt.Printf("%s %s (rev: %s/%s)\n", name, version, getRevision(), runtime.Version())
return exitCodeOK
}

handler := langserver.NewHandler()
<-jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}), handler).DisconnectNotify()
return exitCodeOK
}

func getRevision() string {
if revision != "" {
return revision
}
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
var revision string
var modified string
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
revision = s.Value
case "vcs.modified":
modified = s.Value
}
}
if modified == "true" {
revision += "(modified)"
}
return revision
}

type stdrwc struct{}
Expand Down