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
|
//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 {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
defer d.Close()
|
defer d.Close()
|
||||||
for {
|
for {
|
||||||
name, err := d.Next()
|
name, err := d.Next()
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
break
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
|
||||||
fmt.Printf("diff: %q\n", name)
|
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(out, "diff: %q\n", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user