Windows: Using slugify names on errors

On windows filesystems no special characters can be used on path routes,
so when we fail to create (MkdirAll) the path for a certain
game and a failure happens, we use a slugified version of the name
instead.
This commit is contained in:
Felipe M 2021-01-05 16:55:31 +01:00
parent ae6f7372a2
commit 0b33e24472
3 changed files with 14 additions and 2 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/fmartingr/games-screenshot-mananger
go 1.15
require github.com/gosimple/slug v1.9.0

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs=
github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=

View File

@ -14,6 +14,7 @@ import (
"github.com/fmartingr/games-screenshot-mananger/pkg/providers/minecraft"
"github.com/fmartingr/games-screenshot-mananger/pkg/providers/nintendo_switch"
"github.com/fmartingr/games-screenshot-mananger/pkg/providers/steam"
"github.com/gosimple/slug"
)
var allowedProviders = [...]string{"steam", "minecraft", "nintendo-switch"}
@ -51,7 +52,7 @@ func getGamesFromProvider(provider string, inputPath string, downloadCovers bool
case "minecraft":
games = append(games, minecraft.GetGames()...)
case "nintendo-switch":
games = append(games, nintendo_switch.GetGames(inputPath)...)
games = append(games, nintendo_switch.GetGames(inputPath, downloadCovers)...)
}
return games
}
@ -68,7 +69,12 @@ func processGames(games []games.Game, outputPath string, dryRun bool, downloadCo
// Check if folder exists
if _, err := os.Stat(destinationPath); os.IsNotExist(err) && !dryRun {
os.MkdirAll(destinationPath, 0711)
mkdirErr := os.MkdirAll(destinationPath, 0711)
if mkdirErr != nil {
log.Printf("[ERROR] Couldn't create directory with name %s, falling back to %s", game.Name, slug.Make(game.Name))
destinationPath = filepath.Join(helpers.ExpandUser(outputPath), game.Platform, slug.Make(game.Name))
os.MkdirAll(destinationPath, 0711)
}
}
if downloadCovers && !dryRun {