|
| 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 | +} |
0 commit comments