2016-11-13 21:59:38 +00:00
|
|
|
// Copyright © 2016 Zellyn Hunter <zellyn@gmail.com>
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zellyn/diskii/lib/disk"
|
2016-11-17 02:42:18 +00:00
|
|
|
_ "github.com/zellyn/diskii/lib/dos3"
|
2016-11-13 21:59:38 +00:00
|
|
|
_ "github.com/zellyn/diskii/lib/supermon"
|
|
|
|
)
|
|
|
|
|
2016-11-15 03:54:57 +00:00
|
|
|
// catalogCmd represents the cat command, used to catalog a disk or
|
2016-11-13 21:59:38 +00:00
|
|
|
// directory.
|
2016-11-15 03:54:57 +00:00
|
|
|
var catalogCmd = &cobra.Command{
|
|
|
|
Use: "catalog",
|
|
|
|
Aliases: []string{"cat", "ls"},
|
|
|
|
Short: "print a list of files",
|
|
|
|
Long: `Catalog a disk or subdirectory.`,
|
2016-11-13 21:59:38 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if err := runCat(args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-11-15 03:54:57 +00:00
|
|
|
RootCmd.AddCommand(catalogCmd)
|
2016-11-13 21:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|