mirror of
https://github.com/zellyn/diskii.git
synced 2024-11-29 01:49:23 +00:00
f57c3a0c06
- Added a generic `Operator` registry and implemented Operators for dos3.3 and NakeOS/Super-Mon disks. - Currently Operators implement only the `Catalog` command.
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
// Copyright © 2016 Zellyn Hunter <zellyn@gmail.com>
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/zellyn/diskii/lib/disk"
|
|
_ "github.com/zellyn/diskii/lib/dos33"
|
|
_ "github.com/zellyn/diskii/lib/supermon"
|
|
)
|
|
|
|
// catCmd represents the cat command, used to catalog a disk or
|
|
// directory.
|
|
var catCmd = &cobra.Command{
|
|
Use: "cat",
|
|
Short: "print a list of files",
|
|
Long: `Catalog a disk or subdirectory.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if err := runCat(args); err != nil {
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
os.Exit(-1)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(catCmd)
|
|
}
|
|
|
|
// runCat performs the actual catalog logic.
|
|
func runCat(args []string) error {
|
|
if len(args) < 1 || len(args) > 2 {
|
|
return fmt.Errorf("cat expects a disk image filename, and an optional subdirectory")
|
|
}
|
|
sd, err := disk.Open(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
op, err := disk.OperatorFor(sd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
subdir := ""
|
|
if len(args) == 2 {
|
|
if !op.HasSubdirs() {
|
|
return fmt.Errorf("Disks of type %q cannot have subdirectories", op.Name())
|
|
}
|
|
subdir = args[1]
|
|
}
|
|
fds, err := op.Catalog(subdir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, fd := range fds {
|
|
fmt.Println(fd.Name)
|
|
}
|
|
return nil
|
|
}
|