-
Notifications
You must be signed in to change notification settings - Fork 45
Allow printing SCIP index as JSON #147
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
Changes from 5 commits
2d37d33
0721741
1b8d465
796df20
aece669
d7fd6f4
9ece64b
4f044fd
e705756
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"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), | ||
&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 { | ||
|
||
fmt.Println(flags) | ||
|
||
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 | ||
} |
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") | ||
} |
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.
Outdated
Show resolved
Hide resolved
|
||
<!--toc:end--> | ||
|
||
``` | ||
NAME: | ||
scip - SCIP Code Intelligence Protocol CLI | ||
|
@@ -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: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First of all, my comment was not a blocking one.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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` | ||
|
@@ -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) | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.