refactor(nativeprobe): return errors instead of panicking

Move the probe logic into run and report failures on stderr with a
non-zero exit status, as recommended by Effective Go. Group standard
library imports separately and add a package doc comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-12-03 18:07:48 +01:00
1 parent 8fe60a4f8f
commit 7413b9a43a
1 file changed
+38 -23
+38 -23
View File
@@ -1,15 +1,20 @@
//go:build ceph && cgo //go:build ceph && cgo
// Command nativeprobe exercises the libcephfs binding against a lab cluster:
// it stats a path and optionally prints the snapdiff between two snapshots.
// It is only built with the ceph build tag and cgo.
package main package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
"io" "io"
"os" "os"
"git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
) )
func main() { func main() {
@@ -19,39 +24,49 @@ func main() {
a := flag.String("from", "", "previous snapshot") a := flag.String("from", "", "previous snapshot")
b := flag.String("to", "", "current snapshot") b := flag.String("to", "", "current snapshot")
flag.Parse() flag.Parse()
data, err := os.ReadFile(*config) if err := run(os.Stdout, *config, *root, *rel, *a, *b); err != nil {
fmt.Fprintln(os.Stderr, "nativeprobe:", err)
os.Exit(1)
}
}
func run(out io.Writer, config, root, rel, from, to string) error {
data, err := os.ReadFile(config)
if err != nil { if err != nil {
panic(err) return err
} }
var c struct{ Source storage.Options } var c struct{ Source storage.Options }
if err = json.Unmarshal(data, &c); err != nil { if err = json.Unmarshal(data, &c); err != nil {
panic(err) return fmt.Errorf("config: %w", err)
} }
fs, err := cephfsnative.Open(c.Source) fs, err := cephfsnative.Open(c.Source)
if err != nil { if err != nil {
panic(err) return err
} }
defer fs.Close() defer fs.Close()
n, err := fs.Stat(*root) n, err := fs.Stat(root)
if err != nil { if err != nil {
panic(err) return err
} }
json.NewEncoder(os.Stdout).Encode(n) if err = json.NewEncoder(out).Encode(n); err != nil {
if *a != "" { return err
d, err := fs.SnapDiff(*root, *rel, *a, *b) }
if from == "" {
return nil
}
d, err := fs.SnapDiff(root, rel, from, to)
if err != nil {
return err
}
defer d.Close()
for {
name, err := d.Next()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil { if err != nil {
panic(err) return err
}
defer d.Close()
for {
name, err := d.Next()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("diff: %q\n", name)
} }
fmt.Fprintf(out, "diff: %q\n", name)
} }
} }