ollie/tools/codeintel/cmd/code_rewrite/main.go

40 lines
809 B
Go

package main
import (
"encoding/json"
"fmt"
"ollie/tools/codeintel/internal/codeintel"
"os"
"path/filepath"
)
func main() {
type arguments struct {
Path string `json:"path"`
Language string `json:"language"`
Query string `json:"query"`
Replacement string `json:"replacement"`
DryRun bool `json:"dry_run"`
}
var a arguments
if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil {
fail(err)
}
p, err := filepath.Abs(a.Path)
if err != nil {
fail(err)
}
out, err := codeintel.ReplaceMatches(p, a.Language, a.Query, a.Replacement)
if err != nil {
fail(err)
}
if a.DryRun {
fmt.Print(string(out))
return
}
if err := os.WriteFile(p, out, 0644); err != nil {
fail(err)
}
}
func fail(e error) { fmt.Fprintln(os.Stderr, "error:", e); os.Exit(1) }