Add new queue subcommand, fixes #2

This commit is contained in:
Adora Laura Kalb 2023-07-11 09:57:29 +02:00
parent 03720e7e30
commit 9b7e2f4278
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
11 changed files with 186 additions and 7 deletions

View file

@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- cli option `queue show` to list currently queued bundles [#2](https://git.lauka.net/lauralani/humble-bot/issues/2)
- cli option `queue fill` to query humblebundle.com for new bundles and then fill the queue [#2](https://git.lauka.net/lauralani/humble-bot/issues/2)
- cli option `queue remove` to remove a queued up bundle [#2](https://git.lauka.net/lauralani/humble-bot/issues/2)
### Fixed
- [[BUG] Error parsing mastodon response field time](https://git.lauka.net/lauralani/humble-bot/issues/1)

57
app/cli-queue.go Normal file
View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 Laura Kalb <dev@lauka.net>
* The code of this project is available under the MIT license. See the LICENSE file for more info.
*
*/
package app
import (
"codeberg.org/lauralani/humble-bot/db"
"codeberg.org/lauralani/humble-bot/models"
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"os"
"strconv"
)
func RunQueueShow(cmd *cobra.Command, args []string) {
items := db.GetAllQueuedItems()
if len(items) == 0 {
fmt.Println("The queue is empty.")
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "Short Name", "URL"})
for _, item := range items {
t.AppendRow(table.Row{item.ID, item.Name, item.URL})
}
t.Render()
}
func RunQueueRemove(cmd *cobra.Command, args []string) {
id, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Error().Str("parameter", args[0]).Msg("Parameter is not a valid number!")
os.Exit(1)
}
var bundle models.QueueItem
bundle.ID = id
err = db.Dequeue(bundle)
if err != nil {
log.Error().Int64("parameter", id).Msgf("Couldn't remove item from queue: %q", err)
os.Exit(1)
}
log.Info().Msgf("Deleted item with ID %v from queue", id)
}
func RunQueueFill(cmd *cobra.Command, args []string) {
UpdateBundles()
}

View file

@ -14,10 +14,6 @@ import (
"time"
)
func RunRoot(cmd *cobra.Command, args []string) {
UpdateBundles()
}
func RunDaemon(cmd *cobra.Command, args []string) {
StartCronJobs()
}

23
cmd/queue-fill.go Normal file
View file

@ -0,0 +1,23 @@
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
The code of this project is available under the MIT license. See the LICENSE file for more info.
*/
package cmd
import (
"codeberg.org/lauralani/humble-bot/app"
"github.com/spf13/cobra"
)
// queueshowCmd represents the queue command
var queuefillCmd = &cobra.Command{
Use: "fill",
Short: "Fill the queue with new Bundles",
Run: app.RunQueueFill,
Args: cobra.ExactArgs(0),
}
func init() {
queueCmd.AddCommand(queuefillCmd)
}

24
cmd/queue-remove.go Normal file
View file

@ -0,0 +1,24 @@
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
The code of this project is available under the MIT license. See the LICENSE file for more info.
*/
package cmd
import (
"codeberg.org/lauralani/humble-bot/app"
"github.com/spf13/cobra"
)
// queueCmd represents the queue command
var queueremoveCmd = &cobra.Command{
Use: "remove QUEUEID",
Short: "Delete single queue item",
Run: app.RunQueueRemove,
Args: cobra.ExactArgs(1),
Example: "humble-bot queue remove 25",
}
func init() {
queueCmd.AddCommand(queueremoveCmd)
}

23
cmd/queue-show.go Normal file
View file

@ -0,0 +1,23 @@
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
The code of this project is available under the MIT license. See the LICENSE file for more info.
*/
package cmd
import (
"codeberg.org/lauralani/humble-bot/app"
"github.com/spf13/cobra"
)
// queueshowCmd represents the queue command
var queueshowCmd = &cobra.Command{
Use: "show",
Short: "Show items in queue",
Run: app.RunQueueShow,
Args: cobra.ExactArgs(0),
}
func init() {
queueCmd.AddCommand(queueshowCmd)
}

29
cmd/queue.go Normal file
View file

@ -0,0 +1,29 @@
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
The code of this project is available under the MIT license. See the LICENSE file for more info.
*/
package cmd
import (
"github.com/spf13/cobra"
)
// queueCmd represents the queue command
var queueCmd = &cobra.Command{
Use: "queue",
Short: "Queue operations",
}
func init() {
rootCmd.AddCommand(queueCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// queueCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// queueCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -7,7 +7,6 @@
package cmd
import (
"codeberg.org/lauralani/humble-bot/app"
"codeberg.org/lauralani/humble-bot/constants"
"codeberg.org/lauralani/humble-bot/db"
"codeberg.org/lauralani/humble-bot/log"
@ -23,9 +22,8 @@ var cfgFile string
var rootCmd = &cobra.Command{
Use: "humble-bot",
Short: "A simple daemon posting new Humble Bundles on Mastodon",
Short: "A simple CLI app getting and posting new Humble Bundles on Mastodon",
Long: `This app regularly checks for new Humble Bundles and posts updates on Mastodon`,
Run: app.RunRoot,
}
// Execute adds all child commands to the root command and sets flags appropriately.

View file

@ -109,3 +109,15 @@ func IsANewBundle(bundle models.Bundle) (bool, error) {
}
return true, nil
}
func GetAllQueuedItems() []models.QueueItem {
var items []models.QueueItem
err := models.DB.NewSelect().Model(&items).Scan(context.Background())
if err != nil {
log.Error().Str("func", "GetAllQueuedItems").
Msgf("DB error: %v", err.Error())
os.Exit(1)
}
return items
}

3
go.mod
View file

@ -22,12 +22,15 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect

9
go.sum
View file

@ -140,6 +140,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
@ -163,6 +165,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@ -171,10 +175,13 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@ -207,6 +214,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
@ -380,6 +388,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=