Skip to content

Commit a3270b4

Browse files
committed
Added a command to accumulate listed gog updates in update file
1 parent 9c5906f commit a3270b4

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

cmd/cmd-update-accumulate.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmd
2+
3+
import (
4+
"gogcli/gameupdates"
5+
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"os"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func generateUpdateAccumulateCmd() *cobra.Command {
14+
var prev *gameupdates.Updates
15+
var concurrency int
16+
var pause int
17+
var updateFile string
18+
var terminalOutput bool
19+
20+
updateAccumulateCmd := &cobra.Command{
21+
Use: "accumulate",
22+
Short: "Add to the content of an existing update file based on what is new or got updated in GOG.com",
23+
PreRun: func(cmd *cobra.Command, args []string) {
24+
bs, err := ioutil.ReadFile(updateFile)
25+
if err != nil {
26+
fmt.Println("Could not load the update file: ", err)
27+
os.Exit(1)
28+
}
29+
30+
prev = &gameupdates.Updates{}
31+
err = json.Unmarshal(bs, prev)
32+
if err != nil {
33+
fmt.Println("Update file doesn't appear to contain valid json: ", err)
34+
os.Exit(1)
35+
}
36+
},
37+
Run: func(cmd *cobra.Command, args []string) {
38+
u, errs := sdkPtr.GetUpdates(concurrency, pause)
39+
processErrors(errs)
40+
u.Merge(prev)
41+
processSerializableOutput(u, []error{}, terminalOutput, updateFile)
42+
},
43+
}
44+
45+
updateAccumulateCmd.Flags().IntVarP(&concurrency, "concurrency", "r", 4, "Maximum number of concurrent requests that will be made on the GOG api")
46+
updateAccumulateCmd.Flags().IntVarP(&pause, "pause", "s", 200, "Number of milliseconds to wait between batches of api calls")
47+
updateAccumulateCmd.Flags().StringVarP(&updateFile, "update-file", "f", "updates.json", "File to add updates to")
48+
updateAccumulateCmd.Flags().BoolVarP(&terminalOutput, "terminal", "t", false, "If set to true, the updates will be output on the terminal instead of the file")
49+
50+
return updateAccumulateCmd
51+
}

cmd/cmd-update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func generateUpdateCmd() *cobra.Command {
1414
}
1515

1616
updateCmd.AddCommand(generateUpdateGenerateCmd())
17+
updateCmd.AddCommand(generateUpdateAccumulateCmd())
1718

1819
return updateCmd
1920
}

gameupdates/updates.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ func (u *Updates) AddNewGame(gameId int64) {
1717
func (u *Updates) AddUpdatedGame(gameId int64) {
1818
(*u).UpdatedGames = append((*u).UpdatedGames, gameId)
1919
}
20+
21+
func (u *Updates) Merge(other *Updates) {
22+
for _, newGame := range other.NewGames {
23+
if !contains(u.NewGames, newGame) {
24+
(*u).NewGames = append((*u).NewGames, newGame)
25+
}
26+
}
27+
28+
for _, updatedGame := range other.UpdatedGames {
29+
if !contains(u.UpdatedGames, updatedGame) {
30+
(*u).UpdatedGames = append((*u).UpdatedGames, updatedGame)
31+
}
32+
}
33+
}

gameupdates/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package gameupdates
2+
3+
func min(x int, y int) int {
4+
if x < y {
5+
return x
6+
}
7+
return y
8+
}
9+
10+
func contains(list []int64, elem int64) bool {
11+
for _, val := range list {
12+
if val == elem {
13+
return true
14+
}
15+
}
16+
17+
return false
18+
}

0 commit comments

Comments
 (0)