refactor: Decompose tell and eject funcs

This commit is contained in:
Felix Pojtinger
2021-12-06 22:43:37 +01:00
parent 01c75dfa65
commit 4f8a6a12a4
6 changed files with 55 additions and 32 deletions

View File

@@ -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),
},
)
},
}

View File

@@ -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
}

5
pkg/hardware/config.go Normal file
View File

@@ -0,0 +1,5 @@
package hardware
type DriveConfig struct {
Drive string
}

19
pkg/hardware/eject.go Normal file
View File

@@ -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)
}

View File

@@ -1,13 +0,0 @@
package hardware
type DriveConfig struct {
Drive string
}
func Eject(
state DriveConfig,
) error
func Tell(
state DriveConfig,
) (int, error)

19
pkg/hardware/tell.go Normal file
View File

@@ -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)
}