Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Add moniker support (#23)
Browse files Browse the repository at this point in the history
Add support for monikers using go.mod and go.sum files.
  • Loading branch information
efritz authored Sep 17, 2019
1 parent e23ba48 commit 242a4b5
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 298 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
/.vscode
/.idea
/lsif-go
/lsif-gomod
/data.lsif
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ A first draft specification can be found [here](https://github.com/Microsoft/lan

## Quickstart

1. Download and build this program via `go get github.com/sourcegraph/lsif-go`.
1. Download and build this program via `go get github.com/sourcegraph/lsif-go/cmd/lsif-go`.
2. The binary `lsif-go` should be installed into your `$GOPATH/bin` directory.
3. Make sure you have added `$GOPATH/bin` to your `$PATH` envrionment variable.
4. Go to a root directory of a Go project, then execute `lsif-go export`:
4. Go to a root directory of a Go project, then execute `lsif-go`:

```
➜ lsif-go export
➜ lsif-go --out data.lsif
4 package(s), 10 file(s), 597 def(s), 11521 element(s)
Processed in 770.817859ms
```

By default, the exporter dumps LSIF data to the file `data.lsif` in the working directory.
By default, the indexer will read the current directory as the root of the project.

Use `lsif-go -h` for more information
Use `lsif-go --help` for more information

## Try it out!

Expand Down
118 changes: 0 additions & 118 deletions cmd.go

This file was deleted.

100 changes: 100 additions & 0 deletions cmd/lsif-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// The program lsif-go is an LSIF exporter for Go.
package main

import (
"fmt"
"io"
"os"
"time"

"github.com/alecthomas/kingpin"
"github.com/sourcegraph/lsif-go/export"
"github.com/sourcegraph/lsif-go/log"
"github.com/sourcegraph/lsif-go/protocol"
)

const version = "0.4.1"
const versionString = version + ", protocol version " + protocol.Version

func main() {
if err := realMain(); err != nil {
fmt.Fprint(os.Stderr, fmt.Sprintf("error: %v\n", err))
os.Exit(1)
}
}

func realMain() error {
var (
debug bool
verbose bool
projectRoot string
noContents bool
outFile string
stdout bool
)

app := kingpin.New("lsif-go", "lsif-go is an LSIF exporter for Go.").Version(versionString)
app.Flag("debug", "Display debug information.").Default("false").BoolVar(&debug)
app.Flag("verbose", "Display verbose information.").Short('v').Default("false").BoolVar(&verbose)
app.Flag("projectRoot", "Specifies the project root. Defaults to the current working directory.").Default(".").StringVar(&projectRoot)
app.Flag("noContents", "File contents will not be embedded into the dump.").Default("false").BoolVar(&noContents)
app.Flag("out", "The output file the dump is save to.").StringVar(&outFile)
app.Flag("stdout", "Writes the dump to stdout.").Default("false").BoolVar(&stdout)

_, err := app.Parse(os.Args[1:])
if err != nil {
return err
}

if outFile == "" && !stdout {
return fmt.Errorf("either an output file using --out or --stdout must be specified")
}

if debug {
log.SetLevel(log.Debug)
}

if verbose {
log.SetLevel(log.Info)
}

if stdout && (verbose || debug) {
return fmt.Errorf("debug and verbose options cannot be enabled with --stdout")
}

start := time.Now()

var out io.Writer
if stdout {
out = os.Stdout
} else {
file, err := os.Create(outFile)
if err != nil {
return fmt.Errorf("create dump file: %v", err)
}

defer file.Close()
out = file
}

s, err := export.Export(
projectRoot,
noContents,
out,
protocol.ToolInfo{
Name: "lsif-go",
Version: version,
Args: os.Args[1:],
},
)
if err != nil {
return fmt.Errorf("export: %v", err)
}

if !stdout {
log.Printf("%d package(s), %d file(s), %d def(s), %d element(s)", s.NumPkgs, s.NumFiles, s.NumDefs, s.NumElements)
log.Println("Processed in", time.Since(start))
}

return nil
}
80 changes: 80 additions & 0 deletions cmd/lsif-gomod/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// The program lsif-gomod adds gomod moniker support to lsif-go output.
package main

import (
"fmt"
"io"
"os"

"github.com/alecthomas/kingpin"
"github.com/sourcegraph/lsif-go/gomod"
"github.com/sourcegraph/lsif-go/protocol"
)

const version = "0.4.1"
const versionString = version + ", protocol version " + protocol.Version

func main() {
if err := realMain(); err != nil {
fmt.Fprint(os.Stderr, fmt.Sprintf("error: %v\n", err))
os.Exit(1)
}
}

func realMain() error {
var (
projectRoot string
inFile string
stdin bool
outFile string
stdout bool
)

app := kingpin.New("lsif-gomod", "lsif-gomod decorates lsif-go output with gomod monikers.").Version(versionString)
app.Flag("projectRoot", "Specifies the project root. Defaults to the current working directory.").Default(".").StringVar(&projectRoot)
app.Flag("in", "Specifies the file that contains a LSIF dump.").StringVar(&inFile)
app.Flag("stdin", "Reads the dump from stdin.").Default("false").BoolVar(&stdin)
app.Flag("out", "The output file the converted dump is saved to.").StringVar(&outFile)
app.Flag("stdout", "Writes the dump to stdout.").Default("false").BoolVar(&stdout)

_, err := app.Parse(os.Args[1:])
if err != nil {
return err
}

if inFile == "" && !stdin {
return fmt.Errorf("either an input file using --in or --stdin must be specified")
}

if outFile == "" && !stdout {
return fmt.Errorf("either an output file using --out or --stdout must be specified")
}

var in io.Reader
if stdin {
in = os.Stdin
} else {
file, err := os.Open(inFile)
if err != nil {
return fmt.Errorf("open dump file: %v", err)
}

defer file.Close()
in = file
}

var out io.Writer
if stdout {
out = os.Stdout
} else {
file, err := os.Create(outFile)
if err != nil {
return fmt.Errorf("create dump file: %v", err)
}

defer file.Close()
out = file
}

return gomod.Decorate(in, out, projectRoot)
}
Loading

0 comments on commit 242a4b5

Please sign in to comment.