Add deactivate command
This commit is contained in:
parent
a67b21bf41
commit
6bd9449baf
53
cmd/deactivate.go
Normal file
53
cmd/deactivate.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/Jaculabilis/intake/core"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var deactivateCmd = &cobra.Command{
|
||||||
|
Use: "deactivate",
|
||||||
|
Aliases: []string{"deac"},
|
||||||
|
Short: "Deactivate items",
|
||||||
|
Long: `Deactivate items, hiding them from feeds and marking them for deletion.
|
||||||
|
|
||||||
|
Deactivation is idempotent.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
deactivate()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var deacSource string
|
||||||
|
var deacItem string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(deactivateCmd)
|
||||||
|
|
||||||
|
deactivateCmd.Flags().StringVarP(&deacSource, "source", "s", "", "Source of the item")
|
||||||
|
deactivateCmd.MarkFlagRequired("source")
|
||||||
|
deactivateCmd.Flags().StringVarP(&deacItem, "item", "i", "", "Item id")
|
||||||
|
deactivateCmd.MarkFlagRequired("item")
|
||||||
|
}
|
||||||
|
|
||||||
|
func deactivate() {
|
||||||
|
db, err := sql.Open("sqlite3", getDbPath())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open %s", dbPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
core.InitDatabase(db)
|
||||||
|
core.MigrateDatabase(db)
|
||||||
|
|
||||||
|
active, err := core.DeactivateItem(db, deacSource, deacItem)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to deactivate item: %s", err)
|
||||||
|
}
|
||||||
|
if active {
|
||||||
|
fmt.Printf("Deactivated %s/%s\n", deacSource, deacItem)
|
||||||
|
}
|
||||||
|
}
|
28
core/db.go
28
core/db.go
@ -3,6 +3,7 @@ package core
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@ -144,23 +145,28 @@ func AddItem(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeactivateItem(db *sql.DB, source string, id string) error {
|
// Deactivate an item, returning its previous active state.
|
||||||
res, err := db.Exec(`
|
func DeactivateItem(db *sql.DB, source string, id string) (bool, error) {
|
||||||
|
row := db.QueryRow(`
|
||||||
|
select active
|
||||||
|
from items
|
||||||
|
where source = ? and id = ?
|
||||||
|
`, source, id)
|
||||||
|
var active bool
|
||||||
|
err := row.Scan(&active)
|
||||||
|
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return false, fmt.Errorf("item %s/%s not found", source, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.Exec(`
|
||||||
update items
|
update items
|
||||||
set active = 0
|
set active = 0
|
||||||
where source = ? and id = ?
|
where source = ? and id = ?
|
||||||
`, source, id)
|
`, source, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
num, err := res.RowsAffected()
|
return active, nil
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if num == 0 {
|
|
||||||
return fmt.Errorf("item %s/%s not found", source, id)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetActiveItems(db *sql.DB, source string) ([]Item, error) {
|
func GetActiveItems(db *sql.DB, source string) ([]Item, error) {
|
||||||
|
@ -135,7 +135,7 @@ func TestAddItem(t *testing.T) {
|
|||||||
AssertItemIs(t, items[0], "test/one/true/////0")
|
AssertItemIs(t, items[0], "test/one/true/////0")
|
||||||
AssertItemIs(t, items[1], "test/two/true/title/author/body/link/123456")
|
AssertItemIs(t, items[1], "test/two/true/title/author/body/link/123456")
|
||||||
|
|
||||||
if err = DeactivateItem(db, "test", "one"); err != nil {
|
if _, err = DeactivateItem(db, "test", "one"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
items, err = GetActiveItems(db, "test")
|
items, err = GetActiveItems(db, "test")
|
||||||
|
Loading…
Reference in New Issue
Block a user