diff --git a/cmd/sbutil.go b/cmd/sbutil.go index ae3e083..e97b718 100644 --- a/cmd/sbutil.go +++ b/cmd/sbutil.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "strings" + "errors" + "io/fs" "github.com/alexflint/go-arg" @@ -17,7 +19,9 @@ type Arguments struct { } type ArgPack struct { - Input string `arg:"positional,required"` + Input string `arg:"positional,required"` + Force bool `arg:"--force"` + Output string `arg:"--output,-o"` } type ArgUnPack struct { @@ -73,9 +77,17 @@ func pack(args *ArgPack) error { // TODO: put this in the json file? - outname := args.Input[:len(args.Input)-len(".json")]+".studybox" - fmt.Println(outname) - err = sb.Write(outname) + if args.Output != "" { + sb.Filename = args.Output + } + + // outname := args.Input[:len(args.Input)-len(".json")]+".studybox" + if exists(sb.Filename) && !args.Force { + return fmt.Errorf("%s already exists or cannot be written to", sb.Filename) + } + + fmt.Println(sb.Filename) + err = sb.Write(sb.Filename) if err != nil { return err } @@ -117,6 +129,19 @@ func unpack(args *ArgUnPack) error { return nil } +func exists(filename string) bool { + _, err := os.Stat(filename) + if err == nil { + return true + } + + if errors.Is(err, fs.ErrNotExist) { + return false + } + + return true +} + //func main_old() { // if len(os.Args) < 3 { // fmt.Println("Missing command") diff --git a/rom/import.go b/rom/import.go index a044b2a..34ca2a1 100644 --- a/rom/import.go +++ b/rom/import.go @@ -16,7 +16,7 @@ func Import(filename string) (*StudyBox, error) { return nil, err } - sbj := &StudyBoxJson{} + sbj := &StudyBoxJson{ Filename: filename } err = json.Unmarshal(raw, sbj) if err != nil { return nil, fmt.Errorf("Unable to unmarshal json: %v", err) @@ -28,6 +28,7 @@ func Import(filename string) (*StudyBox, error) { } sb := &StudyBox{ + Filename: sbj.Filename, Data: &TapeData{Pages: []*Page{}}, Audio: audio, } diff --git a/rom/rom.go b/rom/rom.go index bb611c7..7ff7bf0 100644 --- a/rom/rom.go +++ b/rom/rom.go @@ -8,6 +8,7 @@ import ( ) type StudyBox struct { + Filename string Data *TapeData Audio *TapeAudio }