Skip to content

Commit

Permalink
Added a command to accumulate listed gog updates in update file
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnitus- committed Nov 30, 2024
1 parent 9c5906f commit a3270b4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cmd/cmd-update-accumulate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"gogcli/gameupdates"

"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
)

func generateUpdateAccumulateCmd() *cobra.Command {
var prev *gameupdates.Updates
var concurrency int
var pause int
var updateFile string
var terminalOutput bool

updateAccumulateCmd := &cobra.Command{
Use: "accumulate",
Short: "Add to the content of an existing update file based on what is new or got updated in GOG.com",
PreRun: func(cmd *cobra.Command, args []string) {
bs, err := ioutil.ReadFile(updateFile)
if err != nil {
fmt.Println("Could not load the update file: ", err)
os.Exit(1)
}

prev = &gameupdates.Updates{}
err = json.Unmarshal(bs, prev)
if err != nil {
fmt.Println("Update file doesn't appear to contain valid json: ", err)
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
u, errs := sdkPtr.GetUpdates(concurrency, pause)
processErrors(errs)
u.Merge(prev)
processSerializableOutput(u, []error{}, terminalOutput, updateFile)
},
}

updateAccumulateCmd.Flags().IntVarP(&concurrency, "concurrency", "r", 4, "Maximum number of concurrent requests that will be made on the GOG api")
updateAccumulateCmd.Flags().IntVarP(&pause, "pause", "s", 200, "Number of milliseconds to wait between batches of api calls")
updateAccumulateCmd.Flags().StringVarP(&updateFile, "update-file", "f", "updates.json", "File to add updates to")
updateAccumulateCmd.Flags().BoolVarP(&terminalOutput, "terminal", "t", false, "If set to true, the updates will be output on the terminal instead of the file")

return updateAccumulateCmd
}
1 change: 1 addition & 0 deletions cmd/cmd-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func generateUpdateCmd() *cobra.Command {
}

updateCmd.AddCommand(generateUpdateGenerateCmd())
updateCmd.AddCommand(generateUpdateAccumulateCmd())

return updateCmd
}
14 changes: 14 additions & 0 deletions gameupdates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ func (u *Updates) AddNewGame(gameId int64) {
func (u *Updates) AddUpdatedGame(gameId int64) {
(*u).UpdatedGames = append((*u).UpdatedGames, gameId)
}

func (u *Updates) Merge(other *Updates) {
for _, newGame := range other.NewGames {
if !contains(u.NewGames, newGame) {
(*u).NewGames = append((*u).NewGames, newGame)
}
}

for _, updatedGame := range other.UpdatedGames {
if !contains(u.UpdatedGames, updatedGame) {
(*u).UpdatedGames = append((*u).UpdatedGames, updatedGame)
}
}
}
18 changes: 18 additions & 0 deletions gameupdates/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gameupdates

func min(x int, y int) int {
if x < y {
return x
}
return y
}

func contains(list []int64, elem int64) bool {
for _, val := range list {
if val == elem {
return true
}
}

return false
}

0 comments on commit a3270b4

Please sign in to comment.