Files
yatm/internal/executor/tape.go
T
Samuel Cui 4fef653b71 Prepare YATM v1.0.0-alpha.2
Unify file queries and Scan execution, retain indexed Identical results,
improve Job and Preview workflows, and verify release packaging.
2026-10-05 19:08:49 +08:00

39 lines
1.1 KiB
Go

package executor
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"github.com/samuelncui/yatm/internal/tools"
"github.com/sirupsen/logrus"
)
// ReadTapeBarcode returns an empty value when the probe explicitly reports no barcode.
// Read/write Sessions reject that result; read-only inspection may use its documented fallback.
func (e *Executor) ReadTapeBarcode(ctx context.Context, device string) (string, error) {
// Run the configured device probe and decode its stable JSON boundary.
cmd := exec.CommandContext(ctx, e.scripts.ReadInfo)
cmd.Env = append(cmd.Env, fmt.Sprintf("DEVICE=%s", device))
data, err := tools.RunCmdWithReturn(logrus.StandardLogger(), cmd)
if err != nil {
return "", fmt.Errorf("read tape info failed, %w", err)
}
var info struct {
Barcode string `json:"barcode"`
}
if err := json.Unmarshal(data, &info); err != nil {
return "", fmt.Errorf("decode tape info failed, %w", err)
}
if strings.TrimSpace(info.Barcode) == "" {
return "", nil
}
barcode, err := NormalizeTapeBarcode(info.Barcode)
if err != nil {
return "", fmt.Errorf("invalid tape barcode, %w", err)
}
return barcode, nil
}