diff --git a/cmd/stbak/cmd/drive_eject.go b/cmd/stbak/cmd/drive_eject.go index feb02e6..4ebb023 100644 --- a/cmd/stbak/cmd/drive_eject.go +++ b/cmd/stbak/cmd/drive_eject.go @@ -1,9 +1,7 @@ package cmd import ( - "os" - - "github.com/pojntfx/stfs/internal/controllers" + "github.com/pojntfx/stfs/pkg/hardware" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/volatiletech/sqlboiler/v4/boil" @@ -21,13 +19,11 @@ var driveEjectCmd = &cobra.Command{ boil.DebugMode = true } - f, err := os.OpenFile(viper.GetString(driveFlag), os.O_RDONLY, os.ModeCharDevice) - if err != nil { - return err - } - defer f.Close() - - return controllers.EjectTape(f) + return hardware.Eject( + hardware.DriveConfig{ + Drive: viper.GetString(driveFlag), + }, + ) }, } diff --git a/cmd/stbak/cmd/drive_tell.go b/cmd/stbak/cmd/drive_tell.go index 694623c..d0666c9 100644 --- a/cmd/stbak/cmd/drive_tell.go +++ b/cmd/stbak/cmd/drive_tell.go @@ -2,9 +2,8 @@ package cmd import ( "fmt" - "os" - "github.com/pojntfx/stfs/internal/controllers" + "github.com/pojntfx/stfs/pkg/hardware" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/volatiletech/sqlboiler/v4/boil" @@ -22,13 +21,11 @@ var driveTellCmd = &cobra.Command{ boil.DebugMode = true } - f, err := os.OpenFile(viper.GetString(driveFlag), os.O_RDONLY, os.ModeCharDevice) - if err != nil { - return err - } - defer f.Close() - - currentRecord, err := controllers.GetCurrentRecordFromTape(f) + currentRecord, err := hardware.Tell( + hardware.DriveConfig{ + Drive: viper.GetString(driveFlag), + }, + ) if err != nil { return err } diff --git a/pkg/hardware/config.go b/pkg/hardware/config.go new file mode 100644 index 0000000..8d0bf19 --- /dev/null +++ b/pkg/hardware/config.go @@ -0,0 +1,5 @@ +package hardware + +type DriveConfig struct { + Drive string +} diff --git a/pkg/hardware/eject.go b/pkg/hardware/eject.go new file mode 100644 index 0000000..f195ed1 --- /dev/null +++ b/pkg/hardware/eject.go @@ -0,0 +1,19 @@ +package hardware + +import ( + "os" + + "github.com/pojntfx/stfs/internal/controllers" +) + +func Eject( + state DriveConfig, +) error { + f, err := os.OpenFile(state.Drive, os.O_RDONLY, os.ModeCharDevice) + if err != nil { + return err + } + defer f.Close() + + return controllers.EjectTape(f) +} diff --git a/pkg/hardware/hardware.go b/pkg/hardware/hardware.go deleted file mode 100644 index b21270c..0000000 --- a/pkg/hardware/hardware.go +++ /dev/null @@ -1,13 +0,0 @@ -package hardware - -type DriveConfig struct { - Drive string -} - -func Eject( - state DriveConfig, -) error - -func Tell( - state DriveConfig, -) (int, error) diff --git a/pkg/hardware/tell.go b/pkg/hardware/tell.go new file mode 100644 index 0000000..7e4df5a --- /dev/null +++ b/pkg/hardware/tell.go @@ -0,0 +1,19 @@ +package hardware + +import ( + "os" + + "github.com/pojntfx/stfs/internal/controllers" +) + +func Tell( + state DriveConfig, +) (int64, error) { + f, err := os.OpenFile(state.Drive, os.O_RDONLY, os.ModeCharDevice) + if err != nil { + return -1, err + } + defer f.Close() + + return controllers.GetCurrentRecordFromTape(f) +}