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

Allow printing SCIP index as JSON #147

Merged
merged 9 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func convertCommand() cli.Command {
Name: "to",
Usage: "Output path for LSIF index",
Destination: &convertFlags.to,
Value: "dump.lsif",
Value: "dump.lsif",
},
},
Action: func(c *cli.Context) error {
Expand Down
56 changes: 56 additions & 0 deletions cmd/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"io"

"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/urfave/cli/v2"
"google.golang.org/protobuf/encoding/protojson"
)

type jsonFlags struct {
from string
pretty bool
}

func jsonCommand() cli.Command {
var jsonFlags jsonFlags

json := cli.Command{
Name: "json",
Usage: "Print SCIP index as JSON",
Flags: []cli.Flag{
fromFlag(&jsonFlags.from),
keynmol marked this conversation as resolved.
Show resolved Hide resolved
&cli.BoolFlag{
Name: "pretty",
Usage: "Pretty print (multiline, with indentation)",
Aliases: []string{"p"},
Destination: &jsonFlags.pretty,
},
},
Action: func(c *cli.Context) error {
if jsonFlags.from == "" {
return errors.New("missing argument for path to SCIP index")
}
return jsonMain(jsonFlags, c.App.Writer)
},
}

return json
}

func jsonMain(flags jsonFlags, out io.Writer) error {
scipIndex, err := readFromOption(flags.from)
if err != nil {
return err
}

options := protojson.MarshalOptions{
Multiline: flags.pretty,
}

jsonBytes, _ := options.Marshal(scipIndex)
out.Write(jsonBytes)

return nil
}
69 changes: 69 additions & 0 deletions cmd/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"testing"

"github.com/sourcegraph/scip/bindings/go/scip"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestJSONCommand(t *testing.T) {
app := scipApp()
// Redirect CLI writer to a buffer
var buff bytes.Buffer
app.Writer = io.Writer(&buff)
idx := makeIndex([]string{"a"}, stringMap{"f": {"b"}}, stringMap{"f": {"a", "b"}})

idx.Metadata = &scip.Metadata{ProjectRoot: "howdy"}

// Serialise SCIP index
indexBytes, err := proto.Marshal(idx)

// Write SCIP index to a temp file
dir := os.TempDir()
file, err := ioutil.TempFile(dir, "scip-cli-json-test*.scip")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())

_, fErr := file.Write(indexBytes)
if fErr != nil {
log.Fatal(fErr)
}

// Run the JSON command with the temporary file
runErr := app.Run([]string{"scip", "json", "--from", file.Name()})
if runErr != nil {
log.Fatal(runErr)
}

type JsonIndex struct {
Metadata struct {
ProjectRoot string `json:"projectRoot"`
}
Documents []struct {
RelativePath string `json:"relativePath"`
} `json:"documents"`
}

var roundtripResult JsonIndex

bytes := buff.Bytes()

jsonErr := json.Unmarshal(bytes, &roundtripResult)
if jsonErr != nil {
log.Fatal(jsonErr)
}

require.Equal(t, roundtripResult.Metadata.ProjectRoot, "howdy")
require.Equal(t, len(roundtripResult.Documents), 1)
require.Equal(t, roundtripResult.Documents[0].RelativePath, "f")
}
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func commands() []*cli.Command {
print := printCommand()
snapshot := snapshotCommand()
stats := statsCommand()
return []*cli.Command{&convert, &lint, &print, &snapshot, &stats}
json := jsonCommand()
return []*cli.Command{&convert, &lint, &print, &snapshot, &stats, &json}
}

//go:embed version.txt
Expand Down
35 changes: 33 additions & 2 deletions docs/CLI.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# SCIP CLI Reference

<!--toc:start-->

- [SCIP CLI Reference](#scip-cli-reference)
- [`scip convert`](#scip-convert)
- [`scip lint`](#scip-lint)
- [`scip print`](#scip-print)
- [`scip snapshot`](#scip-snapshot)
- [`scip stats`](#scip-stats)
- [`scip json`](#scip-json)
keynmol marked this conversation as resolved.
Show resolved Hide resolved
<!--toc:end-->

```
NAME:
scip - SCIP Code Intelligence Protocol CLI
Expand All @@ -21,6 +32,7 @@ COMMANDS:
print Print a SCIP index in a human-readable format for debugging
snapshot Generate snapshot files for golden testing
stats Output useful statistics about a SCIP index
json Print SCIP index as JSON
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
Expand Down Expand Up @@ -96,8 +108,12 @@ DESCRIPTION:
and symbol information.

OPTIONS:
--from value Path to SCIP index file (default: index.scip)
--to value Path to output directory for snapshot files (default: scip-snapshot)
--comment-syntax value Comment syntax to use for snapshot files (default: "//")
--from value Path to SCIP index file (default: "index.scip")
--help, -h show help (default: false)
--project-root value Override project root in the SCIP file. For example, this can be helpful when the SCIP index was created inside a Docker image or created on another computer
--strict If true, fail fast on errors (default: true)
--to value Path to output directory for snapshot files (default: "scip-snapshot")
Comment on lines -99 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why is this PR changing these lines? I thought the earlier PR that verified the documentation should've fixed this... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests only verify that the global CLI help text is valid, not for any of the subcommands :(

It won't be much of an issue to add extra checks in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the unrelated changes from this PR? I can submit a PR tomorrow making the test more robust and fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the unrelated changes from this PR?

IMO, this is not a codebase with ridiculous velocity where unrelated changes are a problem - ongoing small improvements that aren't at odds with main purpose of the PR shouldn't be penalised by extra effort of reverting and resubmitting.

There are many small things that can be improved for which there is no plan and no dedicated effort - with a small number of contributors we should aim to fix things as we go.

You are free to follow up with a test for this (or I can do this) but updating and fixing small things in the docs should be a welcome addition to any PR.

Copy link
Contributor

@varungandhi-src varungandhi-src May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, my comment was not a blocking one.

ongoing small improvements that aren't at odds with main purpose of the PR shouldn't be penalised by extra effort of reverting and resubmitting.

I'm not trying to "penalise" something here... Ideally, these would not be part of the same PR (or commit) anyways. Normal commit hygiene IMO dictates that you'd commit related stuff in a single commit and move unrelated commit into separate PRs before submitting PRs. Even it got bundled into the same PR, if it were in different commits, it'd be easy to revert and push.

That's regardless of codebase velocity etc. My point isn't about velocity, it's about completeness. With drive-by improvements that get mixed in with other PRs, it's less clear what still needs fixing vs what doesn't. E.g. here, if I hadn't flagged this, then the point about the test missing might not have gone unnoticed.

That said, I don't think it's a big deal to leave it in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a process worth discussing separately :) meanwhile, I added an issue so we don't forget: #152

```

## `scip stats`
Expand All @@ -112,3 +128,18 @@ USAGE:
OPTIONS:
--from value Path to SCIP index file (default: index.scip)
```

## `scip json`

```
NAME:
scip json - Print SCIP index as JSON

USAGE:
scip json [command options] [arguments...]

OPTIONS:
--from value Path to SCIP index file (default: "index.scip")
--help, -h show help (default: false)
--pretty, -p Pretty print (multiline, with indentation) (default: false)
```