Replaced arguments with CLIOptions struct

This is a way more convenient way for the underlying providers to
access the input in which the software was called. Having the input
path in providers that already discover path will allow users to
override paths easily, and this way we could also add more flags that
some providers would need and others ignore without changuing function calls.
This commit is contained in:
Felipe M 2021-02-16 13:28:43 +01:00
parent 18f006bafe
commit befcc3376e
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
7 changed files with 39 additions and 31 deletions

View File

@ -27,44 +27,45 @@ const defaultProvider string = "steam"
const defaultDryRun bool = false
const defaultDownloadCovers bool = false
// TODO: Set CLI options into an options struct
func Start() {
cliOptions := games.CLIOptions{
OutputPath: flag.String("output-path", defaultOutputPath, "The destination path of the screenshots"),
InputPath: flag.String("input-path", defaultInputPath, "Input path for the provider that requires it"),
DownloadCovers: flag.Bool("download-covers", defaultDownloadCovers, "use to enable the download of covers (if the provider supports it)"),
DryRun: flag.Bool("dry-run", defaultDryRun, "Use to disable write actions on filesystem"),
}
var provider = flag.String("provider", defaultProvider, "steam")
var outputPath = flag.String("output-path", defaultOutputPath, "The destination path of the screenshots")
var inputPath = flag.String("input-path", defaultInputPath, "Input path for the provider that requires it")
var downloadCovers = flag.Bool("download-covers", defaultDownloadCovers, "use to enable the download of covers (if the provider supports it)")
var dryRun = flag.Bool("dry-run", defaultDryRun, "Use to disable write actions on filesystem")
flag.Parse()
if helpers.SliceContainsString(allowedProviders[:], *provider, nil) {
games := getGamesFromProvider(*provider, *inputPath, *downloadCovers)
processGames(games, *outputPath, *dryRun, *downloadCovers)
games := getGamesFromProvider(*provider, cliOptions)
processGames(games, cliOptions)
} else {
log.Printf("Provider %s not found!", *provider)
}
}
func getGamesFromProvider(provider string, inputPath string, downloadCovers bool) []games.Game {
func getGamesFromProvider(provider string, cliOptions games.CLIOptions) []games.Game {
var games []games.Game
switch provider {
case "steam":
games = append(games, steam.GetGames(downloadCovers)...)
games = append(games, steam.GetGames(cliOptions)...)
case "minecraft":
games = append(games, minecraft.GetGames()...)
games = append(games, minecraft.GetGames(cliOptions)...)
case "nintendo-switch":
games = append(games, nintendo_switch.GetGames(inputPath)...)
games = append(games, nintendo_switch.GetGames(cliOptions)...)
case "playstation-4":
games = append(games, playstation4.GetGames(inputPath)...)
games = append(games, playstation4.GetGames(cliOptions)...)
case "retroarch":
games = append(games, retroarch.GetGames(inputPath, downloadCovers)...)
games = append(games, retroarch.GetGames(cliOptions)...)
}
return games
}
func processGames(games []games.Game, outputPath string, dryRun bool, downloadCovers bool) {
func processGames(games []games.Game, cliOptions games.CLIOptions) {
for _, game := range games {
destinationPath := filepath.Join(helpers.ExpandUser(outputPath), game.Platform)
destinationPath := filepath.Join(helpers.ExpandUser(*cliOptions.OutputPath), game.Platform)
if len(game.Name) > 0 {
destinationPath = filepath.Join(destinationPath, game.Name)
} else {
@ -78,16 +79,16 @@ func processGames(games []games.Game, outputPath string, dryRun bool, downloadCo
}
// Check if folder exists
if _, err := os.Stat(destinationPath); os.IsNotExist(err) && !dryRun {
if _, err := os.Stat(destinationPath); os.IsNotExist(err) && !*cliOptions.DryRun {
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))
destinationPath = filepath.Join(helpers.ExpandUser(*cliOptions.OutputPath), game.Platform, slug.Make(game.Name))
os.MkdirAll(destinationPath, 0711)
}
}
if downloadCovers && !dryRun && game.Cover.Path != "" {
if *cliOptions.DownloadCovers && !*cliOptions.DryRun && game.Cover.Path != "" {
destinationCoverPath := filepath.Join(destinationPath, game.Cover.DestinationName)
if _, err := os.Stat(destinationCoverPath); os.IsNotExist(err) {
@ -117,8 +118,8 @@ func processGames(games []games.Game, outputPath string, dryRun bool, downloadCo
}
} else {
if dryRun {
log.Println(filepath.Base(screenshot.Path), " -> ", strings.Replace(destinationPath, helpers.ExpandUser(outputPath), "", 1))
if *cliOptions.DryRun {
log.Println(filepath.Base(screenshot.Path), " -> ", strings.Replace(destinationPath, helpers.ExpandUser(*cliOptions.OutputPath), "", 1))
} else {
helpers.CopyFile(screenshot.Path, destinationPath)
}

View File

@ -8,6 +8,13 @@ import (
const DatetimeFormat = "2006-01-02_15-04-05"
type CLIOptions struct {
OutputPath *string
InputPath *string
DownloadCovers *bool
DryRun *bool
}
type Game struct {
ID string
Name string

View File

@ -28,7 +28,7 @@ func getScreenshotsFromPath(game *games.Game, path string) {
}
}
func GetGames() []games.Game {
func GetGames(cliOptions games.CLIOptions) []games.Game {
var result []games.Game
// Standalone minecraft
minecraftStandalone := games.Game{Name: "Minecraft", Platform: "PC", Notes: "Standalone"}

View File

@ -77,11 +77,11 @@ func addScreenshotToGame(userGames []games.Game, switchGame SwitchGame, screensh
return userGames
}
func GetGames(inputPath string) []games.Game {
func GetGames(cliOptions games.CLIOptions) []games.Game {
switchGames := getSwitchGameList()
var userGames []games.Game
err := filepath.Walk(inputPath,
err := filepath.Walk(*cliOptions.InputPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err

View File

@ -33,10 +33,10 @@ func addScreenshotToGame(userGames []games.Game, gameName string, screenshot gam
return userGames
}
func GetGames(inputPath string) []games.Game {
func GetGames(cliOptions games.CLIOptions) []games.Game {
var userGames []games.Game
err := filepath.Walk(inputPath,
err := filepath.Walk(*cliOptions.InputPath,
func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err

View File

@ -147,16 +147,16 @@ func findScreenshotsForGame(item RetroArchPlaylistItem) []games.Screenshot {
return result
}
func GetGames(inputPath string, downloadCovers bool) []games.Game {
func GetGames(cliOptions games.CLIOptions) []games.Game {
var userGames []games.Game
playlists := readPlaylists(inputPath)
playlists := readPlaylists(*cliOptions.InputPath)
for playlistName := range playlists {
for _, item := range playlists[playlistName].Items {
var cover games.Screenshot
if downloadCovers {
if *cliOptions.DownloadCovers {
coverURL := formatLibretroBoxartURL(playlistName, item.Label)
boxartPath, err := helpers.DownloadURLIntoTempFile(coverURL)
if err == nil {

View File

@ -156,7 +156,7 @@ func GetScreenshotsForGame(user string, game *games.Game) {
}
}
func GetGames(downloadCovers bool) []games.Game {
func GetGames(cliOptions games.CLIOptions) []games.Game {
var localGames []games.Game
c := make(chan SteamAppList)
go getSteamAppList(c)
@ -171,7 +171,7 @@ func GetGames(downloadCovers bool) []games.Game {
}
userGame := games.Game{ID: userGameID, Name: steamGame.Name, Provider: providerName, Platform: "PC"}
if downloadCovers {
if *cliOptions.DownloadCovers {
coverPath, err := downloadGameHeaderImage(userGameID)
if err == nil {
userGame.Cover = games.Screenshot{Path: coverPath, DestinationName: ".cover"}