feat(datagrip): migrate from LSP4IJ to native CLI integration
This commit is contained in:
+30
-8
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -18,9 +19,10 @@ import (
|
||||
// file:line:col: [RULEID] severity: message
|
||||
func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
var (
|
||||
only []string // --only=ID,ID rule filter
|
||||
fix bool
|
||||
files []string
|
||||
only []string // --only=ID,ID rule filter
|
||||
fix bool
|
||||
jsonOutput bool
|
||||
files []string
|
||||
)
|
||||
for _, a := range args {
|
||||
switch {
|
||||
@@ -29,6 +31,8 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return 0
|
||||
case a == "--fix":
|
||||
fix = true
|
||||
case a == "--json":
|
||||
jsonOutput = true
|
||||
case strings.HasPrefix(a, "--only="):
|
||||
ids := strings.Split(strings.TrimPrefix(a, "--only="), ",")
|
||||
for _, id := range ids {
|
||||
@@ -66,7 +70,13 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return diags, nil
|
||||
}
|
||||
|
||||
printDiags := func(diags []diagnostics.Diagnostic) {
|
||||
var jsonDiags []diagnostics.Diagnostic
|
||||
|
||||
reportDiags := func(diags []diagnostics.Diagnostic) {
|
||||
if jsonOutput {
|
||||
jsonDiags = append(jsonDiags, diags...)
|
||||
return
|
||||
}
|
||||
for _, d := range diags {
|
||||
loc := d.File
|
||||
if d.Line > 0 {
|
||||
@@ -92,14 +102,14 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
if fix {
|
||||
if fix && !jsonOutput {
|
||||
fixed := lint.ApplyFixes(string(src), diags)
|
||||
if fixed != string(src) {
|
||||
_, _ = io.WriteString(stdout, fixed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
printDiags(diags)
|
||||
reportDiags(diags)
|
||||
if len(diags) > 0 {
|
||||
found = true
|
||||
}
|
||||
@@ -115,7 +125,7 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
if fix {
|
||||
if fix && !jsonOutput {
|
||||
fixed := lint.ApplyFixes(string(src), diags)
|
||||
if fixed != string(src) {
|
||||
if err := os.WriteFile(path, []byte(fixed), 0o644); err != nil {
|
||||
@@ -130,13 +140,24 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
}
|
||||
}
|
||||
printDiags(diags)
|
||||
reportDiags(diags)
|
||||
if len(diags) > 0 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
if jsonDiags == nil {
|
||||
jsonDiags = []diagnostics.Diagnostic{}
|
||||
}
|
||||
enc := json.NewEncoder(stdout)
|
||||
if err := enc.Encode(jsonDiags); err != nil {
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: encoding json: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
return 1
|
||||
}
|
||||
@@ -150,6 +171,7 @@ Read SQL from files (or stdin) and report lint findings.
|
||||
|
||||
Flags:
|
||||
--fix Apply autofixes for fixable rules (MIG001, MIG003) and rewrite files
|
||||
--json Emit findings as a JSON array instead of text (ignores --fix)
|
||||
--only=ID,... comma-separated rule IDs to enable (default: all rules)
|
||||
-h, --help show this help
|
||||
|
||||
|
||||
Reference in New Issue
Block a user