fix(cmd): handle errors and improve output formatting
* update error handling in various commands to use blank identifier * enhance output formatting for better readability * add golangci-lint to Makefile for linting checks
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
func cmdConfig(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
for _, a := range args {
|
||||
if a == "-h" || a == "--help" {
|
||||
fmt.Fprintln(stdout, "pgtidy config — print effective configuration resolved from .pgtidy.yaml")
|
||||
_, _ = fmt.Fprintln(stdout, "pgtidy config — print effective configuration resolved from .pgtidy.yaml")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -21,14 +21,14 @@ func cmdConfig(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
st, err := config.Load(wd)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
fmt.Fprintf(stdout, "indent: %q\n", st.Indent)
|
||||
fmt.Fprintf(stdout, "newline: %q\n", st.Newline)
|
||||
fmt.Fprintf(stdout, "keyword_case: %s\n", st.KeywordCase)
|
||||
fmt.Fprintf(stdout, "ident_case: %s\n", st.IdentCase)
|
||||
fmt.Fprintf(stdout, "type_case: %s\n", st.TypeCase)
|
||||
fmt.Fprintf(stdout, "commas: %s\n", st.Commas)
|
||||
_, _ = fmt.Fprintf(stdout, "indent: %q\n", st.Indent)
|
||||
_, _ = fmt.Fprintf(stdout, "newline: %q\n", st.Newline)
|
||||
_, _ = fmt.Fprintf(stdout, "keyword_case: %s\n", st.KeywordCase)
|
||||
_, _ = fmt.Fprintf(stdout, "ident_case: %s\n", st.IdentCase)
|
||||
_, _ = fmt.Fprintf(stdout, "type_case: %s\n", st.TypeCase)
|
||||
_, _ = fmt.Fprintf(stdout, "commas: %s\n", st.Commas)
|
||||
return 0
|
||||
}
|
||||
|
||||
+10
-10
@@ -37,7 +37,7 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return 0
|
||||
default:
|
||||
if len(a) > 1 && a[0] == '-' {
|
||||
fmt.Fprintf(stderr, "pgtidy fmt: unknown flag %q\n", a)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy fmt: unknown flag %q\n", a)
|
||||
return 2
|
||||
}
|
||||
files = append(files, a)
|
||||
@@ -51,7 +51,7 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
st, err := config.Load(wd)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "%v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "%v\n", err)
|
||||
return 2
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
if len(files) == 0 {
|
||||
src, err := io.ReadAll(stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: reading stdin: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: reading stdin: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
out := format.File(parser.Parse(string(src)), st)
|
||||
@@ -69,9 +69,9 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
return 1
|
||||
}
|
||||
case diff:
|
||||
io.WriteString(stdout, unifiedDiff(string(src), out, "stdin"))
|
||||
_, _ = io.WriteString(stdout, unifiedDiff(string(src), out, "stdin"))
|
||||
default:
|
||||
io.WriteString(stdout, out)
|
||||
_, _ = io.WriteString(stdout, out)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
for _, path := range files {
|
||||
src, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
exit = 2
|
||||
continue
|
||||
}
|
||||
@@ -94,22 +94,22 @@ func cmdFmt(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
case write:
|
||||
if changed {
|
||||
if err := os.WriteFile(path, []byte(out), 0o644); err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: writing %s: %v\n", path, err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: writing %s: %v\n", path, err)
|
||||
exit = 2
|
||||
}
|
||||
}
|
||||
case list:
|
||||
if changed {
|
||||
fmt.Fprintln(stdout, path)
|
||||
_, _ = fmt.Fprintln(stdout, path)
|
||||
}
|
||||
case diff:
|
||||
if changed {
|
||||
io.WriteString(stdout, unifiedDiff(string(src), out, path))
|
||||
_, _ = io.WriteString(stdout, unifiedDiff(string(src), out, path))
|
||||
}
|
||||
case check:
|
||||
// handled after loop via anyDiff
|
||||
default:
|
||||
io.WriteString(stdout, out)
|
||||
_, _ = io.WriteString(stdout, out)
|
||||
}
|
||||
}
|
||||
if check && anyDiff && exit == 0 {
|
||||
|
||||
+10
-10
@@ -37,7 +37,7 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
}
|
||||
case len(a) > 1 && a[0] == '-':
|
||||
fmt.Fprintf(stderr, "pgtidy lint: unknown flag %q\n", a)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy lint: unknown flag %q\n", a)
|
||||
return 2
|
||||
default:
|
||||
files = append(files, a)
|
||||
@@ -75,7 +75,7 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
if loc == "" {
|
||||
loc = "stdin"
|
||||
}
|
||||
fmt.Fprintf(stdout, "%s: [%s] %s: %s\n", loc, d.RuleID, d.Severity, d.Message)
|
||||
_, _ = fmt.Fprintf(stdout, "%s: [%s] %s: %s\n", loc, d.RuleID, d.Severity, d.Message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,18 +84,18 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
if len(files) == 0 {
|
||||
src, err := io.ReadAll(stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: reading stdin: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: reading stdin: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
diags, err := check(string(src), "")
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
if fix {
|
||||
fixed := lint.ApplyFixes(string(src), diags)
|
||||
if fixed != string(src) {
|
||||
io.WriteString(stdout, fixed)
|
||||
_, _ = io.WriteString(stdout, fixed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -107,25 +107,25 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
for _, path := range files {
|
||||
src, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
diags, err := check(string(src), path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
if fix {
|
||||
fixed := lint.ApplyFixes(string(src), diags)
|
||||
if fixed != string(src) {
|
||||
if err := os.WriteFile(path, []byte(fixed), 0o644); err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: writing %s: %v\n", path, err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: writing %s: %v\n", path, err)
|
||||
return 2
|
||||
}
|
||||
// Re-check to report any remaining unfixed diagnostics.
|
||||
diags, err = check(fixed, path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func cmdLint(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
}
|
||||
|
||||
func lintUsage(w io.Writer) {
|
||||
fmt.Fprint(w, `Usage: pgtidy lint [flags] [file ...]
|
||||
_, _ = fmt.Fprint(w, `Usage: pgtidy lint [flags] [file ...]
|
||||
|
||||
Read SQL from files (or stdin) and report lint findings.
|
||||
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ import (
|
||||
func cmdLsp(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
for _, a := range args {
|
||||
if a == "-h" || a == "--help" {
|
||||
fmt.Fprintln(stdout, "pgtidy lsp — start the Language Server Protocol server (stdio transport)")
|
||||
_, _ = fmt.Fprintln(stdout, "pgtidy lsp — start the Language Server Protocol server (stdio transport)")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func cmdLsp(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
wd = "."
|
||||
}
|
||||
if err := lsp.Serve(context.Background(), stdin, stdout, wd); err != nil {
|
||||
fmt.Fprintf(stderr, "pgtidy lsp: %v\n", err)
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy lsp: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
|
||||
+3
-3
@@ -29,20 +29,20 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||
case "config":
|
||||
return cmdConfig(args[1:], stdin, stdout, stderr)
|
||||
case "version", "--version", "-v":
|
||||
fmt.Fprintf(stdout, "pgtidy %s\n", version)
|
||||
_, _ = fmt.Fprintf(stdout, "pgtidy %s\n", version)
|
||||
return 0
|
||||
case "help", "-h", "--help":
|
||||
usage(stdout)
|
||||
return 0
|
||||
default:
|
||||
fmt.Fprintf(stderr, "pgtidy: unknown command %q\n", args[0])
|
||||
_, _ = fmt.Fprintf(stderr, "pgtidy: unknown command %q\n", args[0])
|
||||
usage(stderr)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func usage(w io.Writer) {
|
||||
fmt.Fprint(w, `pgtidy — PostgreSQL formatter and linter
|
||||
_, _ = fmt.Fprint(w, `pgtidy — PostgreSQL formatter and linter
|
||||
|
||||
Usage:
|
||||
pgtidy fmt [flags] [files...] Format SQL/PL-pgSQL (stdin if no files)
|
||||
|
||||
Reference in New Issue
Block a user