ollie/tools/filetools/main.go

48 lines
990 B
Go

// Multicall binary for file tools.
// Invoked via symlinks: file_read, file_write, file_edit, file_grep, file_glob.
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"ollie/tools/filetools/internal/filetools"
)
func main() {
name := filepath.Base(os.Args[0])
var fn func(map[string]any) (string, error)
switch name {
case "file_read":
fn = filetools.Read
case "file_write":
fn = filetools.Write
case "file_edit":
fn = filetools.Edit
case "file_grep":
fn = filetools.Grep
case "file_glob":
fn = filetools.Glob
default:
fmt.Fprintf(os.Stderr, "filetools: unknown tool %q\n", name)
os.Exit(1)
}
var m map[string]any
if err := json.NewDecoder(os.Stdin).Decode(&m); err != nil {
fail("invalid JSON input: %v", err)
}
out, err := fn(m)
if err != nil {
if out != "" {
fmt.Fprintln(os.Stdout, out)
}
fail("%v", err)
}
fmt.Println(out)
}
func fail(f string, a ...any) { fmt.Fprintf(os.Stderr, "error: "+f+"\n", a...); os.Exit(1) }