Refactor main
This commit is contained in:
parent
876564d261
commit
1722e7b23a
322
main.go
322
main.go
|
@ -50,149 +50,209 @@ func main() {
|
||||||
|
|
||||||
switch command {
|
switch command {
|
||||||
case "ls":
|
case "ls":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
ls(fileName, pathName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
pathName = strings.ToUpper(pathName)
|
|
||||||
volumeHeader, _, fileEntries, err := prodos.ReadDirectory(file, pathName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: %s", err)
|
|
||||||
}
|
|
||||||
if len(pathName) == 0 {
|
|
||||||
pathName = "/" + volumeHeader.VolumeName
|
|
||||||
}
|
|
||||||
volumeBitmap, err := prodos.ReadVolumeBitmap(file)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
freeBlocks := prodos.GetFreeBlockCount(volumeBitmap, volumeHeader.TotalBlocks)
|
|
||||||
prodos.DumpDirectory(freeBlocks, volumeHeader.TotalBlocks, pathName, fileEntries)
|
|
||||||
case "get":
|
case "get":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
get(fileName, pathName, outFileName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
if len(pathName) == 0 {
|
|
||||||
fmt.Println("Missing pathname")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
getFile, err := prodos.LoadFile(file, pathName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to read file %s: %s\n", pathName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if len(outFileName) == 0 {
|
|
||||||
_, outFileName = prodos.GetDirectoryAndFileNameFromPath(pathName)
|
|
||||||
}
|
|
||||||
outFile, err := os.Create(outFileName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to create output file %s: %s\n", outFileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(strings.ToLower(outFileName), ".bas") {
|
|
||||||
fmt.Fprintf(outFile, prodos.ConvertBasicToText(getFile))
|
|
||||||
} else {
|
|
||||||
outFile.Write(getFile)
|
|
||||||
}
|
|
||||||
case "put":
|
case "put":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
put(fileName, pathName, fileType, auxType, inFileName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
fileInfo, err := os.Stat(fileName)
|
|
||||||
|
|
||||||
err = prodos.WriteFileFromFile(file, pathName, fileType, auxType, fileInfo.ModTime(), inFileName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to write file %s", err)
|
|
||||||
}
|
|
||||||
case "readblock":
|
case "readblock":
|
||||||
fmt.Printf("Reading block 0x%04X (%d):\n\n", blockNumber, blockNumber)
|
readBlock(blockNumber, fileName)
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
block, err := prodos.ReadBlock(file, blockNumber)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
prodos.DumpBlock(block)
|
|
||||||
case "writeblock":
|
case "writeblock":
|
||||||
fmt.Printf("Writing block 0x%04X (%d):\n\n", blockNumber, blockNumber)
|
writeBlock(blockNumber, fileName, inFileName)
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
inFile, err := os.ReadFile(inFileName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open input file %s: %s", inFileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
prodos.WriteBlock(file, blockNumber, inFile)
|
|
||||||
case "create":
|
case "create":
|
||||||
file, err := os.Create(fileName)
|
create(fileName, volumeName, volumeSize)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to create file: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
prodos.CreateVolume(file, volumeName, volumeSize)
|
|
||||||
case "putall":
|
case "putall":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
putall(fileName, inFileName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to create file: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
err = prodos.AddFilesFromHostDirectory(file, inFileName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to add host files: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case "rm":
|
case "rm":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
rm(fileName, pathName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
prodos.DeleteFile(file, pathName)
|
|
||||||
case "mkdir":
|
case "mkdir":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
mkdir(fileName, pathName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
err = prodos.CreateDirectory(file, pathName)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to create directory %s: %s\n", pathName, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case "dumpfile":
|
case "dumpfile":
|
||||||
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
dumpFile(fileName, pathName)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
fileEntry, err := prodos.GetFileEntry(file, pathName)
|
|
||||||
prodos.DumpFileEntry(fileEntry)
|
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Invalid command: %s\n\n", command)
|
fmt.Printf("Invalid command: %s\n\n", command)
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dumpFile(fileName string, pathName string) {
|
||||||
|
checkPathName(pathName)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
fileEntry, err := prodos.GetFileEntry(file, pathName)
|
||||||
|
prodos.DumpFileEntry(fileEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkdir(fileName string, pathName string) {
|
||||||
|
checkPathName(pathName)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
err = prodos.CreateDirectory(file, pathName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to create directory %s: %s\n", pathName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rm(fileName string, pathName string) {
|
||||||
|
checkPathName(pathName)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
prodos.DeleteFile(file, pathName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func putall(fileName string, inFileName string) {
|
||||||
|
if len(inFileName) == 0 {
|
||||||
|
inFileName = "."
|
||||||
|
}
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to create file: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
err = prodos.AddFilesFromHostDirectory(file, inFileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to add host files: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func create(fileName string, volumeName string, volumeSize int) {
|
||||||
|
file, err := os.Create(fileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to create file: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
prodos.CreateVolume(file, volumeName, volumeSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeBlock(blockNumber int, fileName string, inFileName string) {
|
||||||
|
checkInFileName(inFileName)
|
||||||
|
fmt.Printf("Writing block 0x%04X (%d):\n\n", blockNumber, blockNumber)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
inFile, err := os.ReadFile(inFileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open input file %s: %s", inFileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
prodos.WriteBlock(file, blockNumber, inFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readBlock(blockNumber int, fileName string) {
|
||||||
|
fmt.Printf("Reading block 0x%04X (%d):\n\n", blockNumber, blockNumber)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
block, err := prodos.ReadBlock(file, blockNumber)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
prodos.DumpBlock(block)
|
||||||
|
}
|
||||||
|
|
||||||
|
func put(fileName string, pathName string, fileType int, auxType int, inFileName string) {
|
||||||
|
checkPathName(pathName)
|
||||||
|
checkInFileName(inFileName)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
fileInfo, err := os.Stat(fileName)
|
||||||
|
|
||||||
|
err = prodos.WriteFileFromFile(file, pathName, fileType, auxType, fileInfo.ModTime(), inFileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to write file %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func get(fileName string, pathName string, outFileName string) {
|
||||||
|
checkPathName(pathName)
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
getFile, err := prodos.LoadFile(file, pathName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to read file %s: %s\n", pathName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if len(outFileName) == 0 {
|
||||||
|
_, outFileName = prodos.GetDirectoryAndFileNameFromPath(pathName)
|
||||||
|
}
|
||||||
|
outFile, err := os.Create(outFileName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to create output file %s: %s\n", outFileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(strings.ToLower(outFileName), ".bas") {
|
||||||
|
fmt.Fprintf(outFile, prodos.ConvertBasicToText(getFile))
|
||||||
|
} else {
|
||||||
|
outFile.Write(getFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ls(fileName string, pathName string) {
|
||||||
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
pathName = strings.ToUpper(pathName)
|
||||||
|
volumeHeader, _, fileEntries, err := prodos.ReadDirectory(file, pathName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s", err)
|
||||||
|
}
|
||||||
|
if len(pathName) == 0 {
|
||||||
|
pathName = "/" + volumeHeader.VolumeName
|
||||||
|
}
|
||||||
|
volumeBitmap, err := prodos.ReadVolumeBitmap(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to open drive image %s:\n %s", fileName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
freeBlocks := prodos.GetFreeBlockCount(volumeBitmap, volumeHeader.TotalBlocks)
|
||||||
|
prodos.DumpDirectory(freeBlocks, volumeHeader.TotalBlocks, pathName, fileEntries)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkPathName(pathName string) {
|
||||||
|
if len(pathName) == 0 {
|
||||||
|
fmt.Printf("Missing path name (use -p PATHNAME)\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkInFileName(inFileName string) {
|
||||||
|
if len(inFileName) == 0 {
|
||||||
|
fmt.Printf("Missing input file name (use -i FILENAME)\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue