From befcc3376eb8b624e9ac49d7efea898ba4c8991d Mon Sep 17 00:00:00 2001 From: Felipe M Date: Tue, 16 Feb 2021 13:28:43 +0100 Subject: [PATCH] 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. --- pkg/cli/cli.go | 43 +++++++++++----------- pkg/games/{structs.go => types.go} | 7 ++++ pkg/providers/minecraft/minecraft.go | 2 +- pkg/providers/nintendo_switch/switch.go | 4 +- pkg/providers/playstation4/playstation4.go | 4 +- pkg/providers/retroarch/retroarch.go | 6 +-- pkg/providers/steam/steam.go | 4 +- 7 files changed, 39 insertions(+), 31 deletions(-) rename pkg/games/{structs.go => types.go} (84%) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 7a9188e..c19a310 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -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) } diff --git a/pkg/games/structs.go b/pkg/games/types.go similarity index 84% rename from pkg/games/structs.go rename to pkg/games/types.go index 9293bd1..a0116d4 100644 --- a/pkg/games/structs.go +++ b/pkg/games/types.go @@ -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 diff --git a/pkg/providers/minecraft/minecraft.go b/pkg/providers/minecraft/minecraft.go index ae34c0d..b736382 100644 --- a/pkg/providers/minecraft/minecraft.go +++ b/pkg/providers/minecraft/minecraft.go @@ -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"} diff --git a/pkg/providers/nintendo_switch/switch.go b/pkg/providers/nintendo_switch/switch.go index 427ee0b..6f0e163 100644 --- a/pkg/providers/nintendo_switch/switch.go +++ b/pkg/providers/nintendo_switch/switch.go @@ -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 diff --git a/pkg/providers/playstation4/playstation4.go b/pkg/providers/playstation4/playstation4.go index 78244be..cca9751 100644 --- a/pkg/providers/playstation4/playstation4.go +++ b/pkg/providers/playstation4/playstation4.go @@ -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 diff --git a/pkg/providers/retroarch/retroarch.go b/pkg/providers/retroarch/retroarch.go index ddb78ec..09c4823 100644 --- a/pkg/providers/retroarch/retroarch.go +++ b/pkg/providers/retroarch/retroarch.go @@ -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 { diff --git a/pkg/providers/steam/steam.go b/pkg/providers/steam/steam.go index 884e86a..04cd8f9 100644 --- a/pkg/providers/steam/steam.go +++ b/pkg/providers/steam/steam.go @@ -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"}