feat(hyperguild): brain write subcommand
Reads markdown from stdin, POSTs to the brain's /write endpoint with type + slug, prints the resulting path. Pairs with 'brain query' for shell-friendly read/write access to the brain HTTP REST API. Tests cover success, missing args, backend error propagation, and empty stdin (which produces an empty content payload — the brain server's responsibility to validate).
This commit is contained in:
+18
-4
@@ -52,8 +52,22 @@ func runBrainQuery(ctx context.Context, args []string, _ io.Reader, stdout, stde
|
||||
return nil
|
||||
}
|
||||
|
||||
// runBrainWrite is implemented in Task 5; stub now returns an explicit error
|
||||
// so the router compiles and tests for runBrainQuery can run.
|
||||
func runBrainWrite(_ context.Context, _ []string, _ io.Reader, _, _ io.Writer) error {
|
||||
return errors.New("brain write: not implemented (Task 5)")
|
||||
func runBrainWrite(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
|
||||
fs := flag.NewFlagSet("brain write", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return fmt.Errorf("parse flags: %w", err)
|
||||
}
|
||||
if fs.NArg() < 2 {
|
||||
return errors.New("brain write: type and slug required (e.g. brain write knowledge my-slug)")
|
||||
}
|
||||
kind := fs.Arg(0)
|
||||
slug := fs.Arg(1)
|
||||
|
||||
res, err := newBrainClient().Write(ctx, kind, slug, stdin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(stdout, res.Path) //nolint:errcheck
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user