intake/cmd/itemAdd.go

101 lines
2.5 KiB
Go
Raw Normal View History

2025-01-17 05:46:49 +00:00
package cmd
import (
"encoding/json"
2025-01-17 05:46:49 +00:00
"log"
"github.com/Jaculabilis/intake/core"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
)
2025-01-21 16:42:59 +00:00
var itemAddCmd = &cobra.Command{
2025-01-17 05:46:49 +00:00
Use: "add",
Short: "Add an item",
Long: `Create an ad-hoc item in a source.
By default, the item is created in the "default" source, which is created
if it doesn't exist, with a random id.`,
Run: func(cmd *cobra.Command, args []string) {
2025-01-30 15:54:13 +00:00
itemAdd(
stringArg(cmd, "source"),
stringArg(cmd, "id"),
stringArg(cmd, "title"),
stringArg(cmd, "author"),
stringArg(cmd, "body"),
stringArg(cmd, "link"),
intArg(cmd, "time"),
2025-02-05 20:14:53 +00:00
intArg(cmd, "ttl"),
intArg(cmd, "ttd"),
intArg(cmd, "tts"),
2025-01-30 15:54:13 +00:00
stringArg(cmd, "action"),
)
2025-01-17 05:46:49 +00:00
},
}
func init() {
2025-01-21 16:42:59 +00:00
itemCmd.AddCommand(itemAddCmd)
2025-01-17 05:46:49 +00:00
2025-01-30 15:54:13 +00:00
itemAddCmd.Flags().StringP("source", "s", "", "Source in which to create the item (default: default)")
itemAddCmd.Flags().StringP("id", "i", "", "Item id (default: random hex)")
itemAddCmd.Flags().StringP("title", "t", "", "Item title")
itemAddCmd.Flags().StringP("author", "a", "", "Item author")
itemAddCmd.Flags().StringP("body", "b", "", "Item body")
itemAddCmd.Flags().StringP("link", "l", "", "Item link")
itemAddCmd.Flags().IntP("time", "m", 0, "Item time as a Unix timestamp")
2025-02-05 20:14:53 +00:00
itemAddCmd.Flags().Int("ttl", 0, "Item time-to-live in seconds, relative to item creation date")
itemAddCmd.Flags().Int("ttd", 0, "Item time-to-die in seconds, relative to item creation date")
itemAddCmd.Flags().Int("tts", 0, "Item time-to-show in seconds, relative to item creation date")
2025-01-30 15:54:13 +00:00
itemAddCmd.Flags().StringP("action", "x", "", "Item action data as JSON")
2025-01-17 05:46:49 +00:00
}
2025-01-30 15:54:13 +00:00
func itemAdd(
source string,
id string,
title string,
author string,
body string,
link string,
time int,
2025-02-05 20:14:53 +00:00
ttl int,
ttd int,
tts int,
2025-01-30 15:54:13 +00:00
actions string,
) {
2025-01-17 05:46:49 +00:00
// Default to "default" source
2025-01-30 15:54:13 +00:00
if source == "" {
source = "default"
2025-01-17 05:46:49 +00:00
}
// Default id to random hex string
2025-01-30 15:54:13 +00:00
if id == "" {
2025-02-10 14:36:13 +00:00
id = core.RandomHex(16)
2025-01-17 05:46:49 +00:00
}
2025-01-30 15:54:13 +00:00
var itemActions core.Actions
if actions != "" {
if err := json.Unmarshal([]byte(actions), &itemActions); err != nil {
log.Fatalf("error: could not parse actions: %v", err)
}
}
2025-01-23 16:36:25 +00:00
db := openAndMigrateDb()
2025-01-17 05:46:49 +00:00
2025-01-29 15:43:06 +00:00
if err := core.AddItems(db, []core.Item{{
2025-01-30 15:54:13 +00:00
Source: source,
Id: id,
Title: title,
Author: author,
Body: body,
Link: link,
Time: time,
2025-02-05 20:14:53 +00:00
Ttl: ttl,
Ttd: ttd,
Tts: tts,
2025-01-30 15:54:13 +00:00
Action: itemActions,
2025-01-29 15:43:06 +00:00
}}); err != nil {
log.Fatalf("error: failed to add item: %s", err)
2025-01-17 05:46:49 +00:00
}
2025-01-30 15:54:13 +00:00
log.Printf("Added %s/%s\n", source, id)
2025-01-17 05:46:49 +00:00
}