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:
1 parent
8fe60a4f8f
commit
7413b9a43a
1 file changed
+32
-17
@@ -1,15 +1,20 @@
|
||||
//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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative"
|
||||
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative"
|
||||
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -19,39 +24,49 @@ func main() {
|
||||
a := flag.String("from", "", "previous snapshot")
|
||||
b := flag.String("to", "", "current snapshot")
|
||||
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 {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
var c struct{ Source storage.Options }
|
||||
if err = json.Unmarshal(data, &c); err != nil {
|
||||
panic(err)
|
||||
return fmt.Errorf("config: %w", err)
|
||||
}
|
||||
fs, err := cephfsnative.Open(c.Source)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
defer fs.Close()
|
||||
n, err := fs.Stat(*root)
|
||||
n, err := fs.Stat(root)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(n)
|
||||
if *a != "" {
|
||||
d, err := fs.SnapDiff(*root, *rel, *a, *b)
|
||||
if err = json.NewEncoder(out).Encode(n); err != nil {
|
||||
return err
|
||||
}
|
||||
if from == "" {
|
||||
return nil
|
||||
}
|
||||
d, err := fs.SnapDiff(root, rel, from, to)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
defer d.Close()
|
||||
for {
|
||||
name, err := d.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("diff: %q\n", name)
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "diff: %q\n", name)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user