>> make many small corrections

>> rename context attributes
This commit is contained in:
Thomas Lindop 2025-05-23 22:46:17 +01:00
parent 94ab6bd815
commit 18fede0d0d

26
main.go
View file

@ -40,8 +40,8 @@ type runeset = [][2]rune
// Unrelated to the standard library's "context". // Unrelated to the standard library's "context".
type context = struct { type context = struct {
FileSpecs []string FileList []string
Directories []string RecurseDirs []string
DryRun bool DryRun bool
Strategy string Strategy string
DoHelp bool DoHelp bool
@ -141,10 +141,10 @@ func kaput(err error) {
func parseCLIArgs(args []string) (context, error) { func parseCLIArgs(args []string) (context, error) {
// Set defaults // Set defaults
result := context{ result := context{
FileSpecs: []string{}, FileList: []string{},
Directories: []string{}, RecurseDirs: []string{},
DryRun: false, DryRun: false,
Strategy: "fat", Strategy: "represent",
DoHelp: false, DoHelp: false,
DoVersion: false, DoVersion: false,
} }
@ -155,7 +155,7 @@ func parseCLIArgs(args []string) (context, error) {
for index < len(args) { for index < len(args) {
switch arg := args[index]; arg { switch arg := args[index]; arg {
case "-d", "--directory": case "-d", "--directory":
result.Directories = append(result.Directories, arg) result.RecurseDirs = append(result.RecurseDirs, arg)
index++ index++
case "-n", "--dry-run": case "-n", "--dry-run":
result.DryRun = true result.DryRun = true
@ -173,7 +173,7 @@ func parseCLIArgs(args []string) (context, error) {
arg, arg,
)) ))
} else { } else {
result.FileSpecs = append(result.FileSpecs, arg) result.FileList = append(result.FileList, arg)
} }
} }
index++ index++
@ -188,7 +188,7 @@ func printHelp() {
Usage: Usage:
owl [options] FILES owl [options] FILES
Rename FILES such that all characters that invalid in FAT file systems Rename FILES such that all characters that are invalid in FAT file systems
(?,\,*,etc.) are removed. (?,\,*,etc.) are removed.
Options: Options:
@ -212,24 +212,22 @@ func main() {
OwlVersion, OwlVersion,
) )
} else { } else {
for _, file := range ctx.FileSpecs { for _, file := range ctx.FileList {
if _, err := os.Stat(file); err != nil { if _, err := os.Stat(file); err != nil {
warn("File <<%s>> does not exist!", file) warn("File <<%s>> does not exist!", file)
continue continue
} }
oldName := fpath.Base(file) oldName := fpath.Base(file)
dirName := fpath.Dir(file) dirName := fpath.Dir(file)
newPath := fpath.Join(dirName, restrictRuneset(oldName, ctx.Strategy))
if ctx.DryRun { if ctx.DryRun {
fmt.Printf( fmt.Printf(
"%s -> <<%s>>\n", "%s -> <<%s>>\n",
file, file,
restrictRuneset(oldName, "represent"), newPath,
) )
} else { } else {
os.Rename(file,fpath.Join( os.Rename(file, newPath)
dirName,
restrictRuneset(oldName, "represent"),
))
} }
} }
} }