diskii/cmd/catalog.go

60 lines
1.7 KiB
Go
Raw Normal View History

// Copyright © 2016 Zellyn Hunter <zellyn@gmail.com>
package cmd
import (
"fmt"
"os"
2021-06-26 02:33:11 +00:00
"github.com/zellyn/diskii/disk"
"github.com/zellyn/diskii/types"
)
2021-08-01 03:49:22 +00:00
// LsCmd is the kong `ls` command.
2021-06-26 02:33:11 +00:00
type LsCmd struct {
Order types.DiskOrder `kong:"default='auto',enum='auto,do,po',help='Logical-to-physical sector order.'"`
2021-08-01 02:44:04 +00:00
System string `kong:"default='auto',enum='auto,dos3,prodos,nakedos',help='DOS system used for image.'"`
2021-06-26 02:33:11 +00:00
ShortNames bool `kong:"short='s',help='Whether to print short filenames (only makes a difference on Super-Mon disks).'"`
Image *os.File `kong:"arg,required,help='Disk/device image to read.'"`
Directory string `kong:"arg,optional,help='Directory to list (ProDOS only).'"`
}
2021-08-01 03:49:22 +00:00
// Help displays extended help and examples.
2021-08-01 02:44:04 +00:00
func (l LsCmd) Help() string {
return `Examples:
# Simple ls of a disk image
diskii ls games.dsk
# Get really explicit about disk order and system
diskii ls --order do --system nakedos Super-Mon-2.0.dsk`
}
2021-08-01 03:49:22 +00:00
// Run the `ls` command.
2021-06-26 02:33:11 +00:00
func (l *LsCmd) Run(globals *types.Globals) error {
op, order, err := disk.OpenFile(l.Image, l.Order, l.System, globals.DiskOperatorFactories, globals.Debug)
if err != nil {
return fmt.Errorf("%w: %s", err, l.Image.Name())
}
if globals.Debug > 0 {
2021-06-26 02:33:11 +00:00
fmt.Fprintf(os.Stderr, "Opened disk with order %q, system %q\n", order, op.Name())
2018-06-07 02:27:15 +00:00
}
2021-06-26 02:33:11 +00:00
if l.Directory != "" {
if !op.HasSubdirs() {
2021-08-01 03:14:55 +00:00
return fmt.Errorf("disks of type %q cannot have subdirectories", op.Name())
}
}
2021-06-26 02:33:11 +00:00
fds, err := op.Catalog(l.Directory)
if err != nil {
return err
}
for _, fd := range fds {
if !l.ShortNames && fd.Fullname != "" {
fmt.Println(fd.Fullname)
} else {
fmt.Println(fd.Name)
}
}
return nil
}