2016-11-30 03:53:40 +00:00
|
|
|
// Copyright © 2016 Zellyn Hunter <zellyn@gmail.com>
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-01 02:42:42 +00:00
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
"github.com/zellyn/diskii/disk"
|
|
|
|
"github.com/zellyn/diskii/helpers"
|
|
|
|
"github.com/zellyn/diskii/types"
|
|
|
|
)
|
2016-11-30 03:53:40 +00:00
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
type PutCmd struct {
|
|
|
|
Order types.DiskOrder `kong:"default='auto',enum='auto,do,po',help='Logical-to-physical sector order.'"`
|
|
|
|
System string `kong:"default='auto',enum='auto,dos3',help='DOS system used for image.'"`
|
|
|
|
FiletypeName string `kong:"default='B',help='Type of file (“diskii filetypes” to list).'"`
|
|
|
|
Overwrite bool `kong:"short='f',help='Overwrite existing file?'"`
|
|
|
|
|
|
|
|
DiskImage string `kong:"arg,required,type='existingfile',help='Disk image to modify.'"`
|
|
|
|
TargetFilename string `kong:"arg,required,help='Filename to use on disk.'"`
|
|
|
|
SourceFilename string `kong:"arg,required,type='existingfile',help='Name of file containing data to put.'"`
|
2016-11-30 03:53:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
func (p PutCmd) Help() string {
|
|
|
|
return `Examples:
|
|
|
|
# Put file gremlins.o onto disk image games.dsk, using the filename GREMLINS.
|
|
|
|
diskii put games.dsk GREMLINS gremlins.o`
|
2016-11-30 03:53:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
func (p *PutCmd) Run(globals *types.Globals) error {
|
|
|
|
if p.DiskImage == "-" {
|
|
|
|
if p.SourceFilename == "-" {
|
|
|
|
return fmt.Errorf("cannot get both disk image and file contents from stdin")
|
|
|
|
}
|
2016-11-30 03:53:40 +00:00
|
|
|
}
|
2021-08-01 02:10:44 +00:00
|
|
|
op, order, err := disk.OpenFilename(p.DiskImage, p.Order, p.System, globals.DiskOperatorFactories, globals.Debug)
|
2016-11-30 03:53:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-01 02:10:44 +00:00
|
|
|
|
|
|
|
contents, err := helpers.FileContentsOrStdIn(p.SourceFilename)
|
2016-11-30 03:53:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:10:44 +00:00
|
|
|
filetype, err := types.FiletypeForName(p.FiletypeName)
|
2016-12-01 02:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-01 02:10:44 +00:00
|
|
|
fileInfo := types.FileInfo{
|
|
|
|
Descriptor: types.Descriptor{
|
|
|
|
Name: p.TargetFilename,
|
2016-12-01 02:42:42 +00:00
|
|
|
Length: len(contents),
|
|
|
|
Type: filetype,
|
|
|
|
},
|
|
|
|
Data: contents,
|
|
|
|
}
|
2021-08-01 02:10:44 +00:00
|
|
|
_, err = op.PutFile(fileInfo, p.Overwrite)
|
2016-12-01 02:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-01 02:10:44 +00:00
|
|
|
|
|
|
|
return disk.WriteBack(p.DiskImage, op, order, true)
|
2016-11-30 03:53:40 +00:00
|
|
|
}
|