action execute respects item action support

This commit is contained in:
Tim Van Baak 2025-01-29 09:13:48 -08:00
parent 7ca6ccfaf3
commit 680d8db6bb
5 changed files with 45 additions and 13 deletions

View File

@ -1,7 +1,10 @@
.PHONY: help serve
.PHONY: help serve test-data
help: ## display this help
@awk 'BEGIN{FS = ":.*##"; printf "\033[1m\nUsage\n \033[1;92m make\033[0;36m <target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } ' $(MAKEFILE_LIST)
serve: ## Run "intake serve" with live reload
@air -build.cmd "go build -o tmp/intake" -build.bin tmp/intake -build.args_bin serve,--data-dir,tmp
test-data: ## Recreate test data in tmp/
@test/test_items.sh

View File

@ -16,6 +16,9 @@ var actionExecuteCmd = &cobra.Command{
Short: "Run a source action for an item",
Long: fmt.Sprintf(`Execute a source action for an item.
The item must declare support for the action by having the action's name
in its "action" field. Use --force to execute the action anyway.
The "fetch" action is special and does not execute for any specific item.
Use "intake source fetch" to run the fetch action.
@ -33,6 +36,7 @@ var actionExecuteItem string
var actionExecuteFormat string
var actionExecuteDryRun bool
var actionExecuteDiff bool
var actionExecuteForce bool
func init() {
actionCmd.AddCommand(actionExecuteCmd)
@ -46,10 +50,12 @@ func init() {
actionExecuteCmd.PersistentFlags().StringVarP(&actionExecuteAction, "action", "a", "", "Action to run")
actionExecuteCmd.MarkFlagRequired("action")
actionExecuteCmd.Flags().StringVarP(&actionExecuteFormat, "format", "f", "headlines", "Feed format for returned items.")
actionExecuteCmd.Flags().StringVarP(&actionExecuteFormat, "format", "f", "headlines", "Feed format for returned items")
actionExecuteCmd.Flags().BoolVar(&actionExecuteDryRun, "dry-run", false, "Instead of updating the item, print it")
actionExecuteCmd.Flags().BoolVar(&actionExecuteDiff, "diff", false, "Show which fields of the item changed")
actionExecuteCmd.Flags().BoolVar(&actionExecuteForce, "force", false, "Execute the action even if the item does not support it")
}
func actionExecute() {
@ -67,16 +73,24 @@ func actionExecute() {
db := openAndMigrateDb()
argv, err := core.GetArgvForAction(db, actionExecuteSource, actionExecuteAction)
if err != nil {
log.Fatalf("error: failed to get action: %v", err)
}
item, err := core.GetItem(db, actionExecuteSource, actionExecuteItem)
if err != nil {
log.Fatalf("error: failed to get item: %v", err)
}
if item.Action[actionExecuteAction] == nil {
if actionExecuteForce {
log.Printf("warning: force-executing %s on %s/%s", actionExecuteAction, actionExecuteSource, actionExecuteItem)
} else {
log.Fatalf("error: %s/%s does not support %s", actionExecuteSource, actionExecuteItem, actionExecuteAction)
}
}
argv, err := core.GetArgvForAction(db, actionExecuteSource, actionExecuteAction)
if err != nil {
log.Fatalf("error: failed to get action: %v", err)
}
itemJson, err := json.Marshal(item)
if err != nil {
log.Fatalf("error: failed to serialize item: %v", err)

View File

@ -49,7 +49,7 @@ func feed() {
items, err = core.GetActiveItemsForSource(db, feedSource)
}
if err != nil {
log.Fatalf("error: failed to fetch items from %s", feedSource)
log.Fatalf("error: failed to fetch items from %s:, %v", feedSource, err)
}
} else if feedChannel != "" {
log.Fatal("error: unimplemented")
@ -60,7 +60,7 @@ func feed() {
items, err = core.GetAllActiveItems(db)
}
if err != nil {
log.Fatal("error: failed to fetch items")
log.Fatalf("error: failed to fetch items: %v", err)
}
}

View File

@ -3,7 +3,7 @@ package cmd
import (
"crypto/rand"
"encoding/hex"
"fmt"
"encoding/json"
"log"
"github.com/Jaculabilis/intake/core"
@ -30,6 +30,7 @@ var addItemAuthor string
var addItemBody string
var addItemLink string
var addItemTime int
var addItemActions string
func init() {
itemCmd.AddCommand(itemAddCmd)
@ -41,6 +42,7 @@ func init() {
itemAddCmd.Flags().StringVarP(&addItemBody, "body", "b", "", "Item body")
itemAddCmd.Flags().StringVarP(&addItemLink, "link", "l", "", "Item link")
itemAddCmd.Flags().IntVarP(&addItemTime, "time", "m", 0, "Item time as a Unix timestamp")
itemAddCmd.Flags().StringVarP(&addItemActions, "action", "x", "", "Item time as a Unix timestamp")
}
func itemAdd() {
@ -52,11 +54,18 @@ func itemAdd() {
if addItemId == "" {
bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil {
log.Fatal("Failed to generate id")
log.Fatalf("error: failed to generate id: %v", err)
}
addItemId = hex.EncodeToString(bytes)
}
var actions core.Actions
if addItemActions != "" {
if err := json.Unmarshal([]byte(addItemActions), &actions); err != nil {
log.Fatalf("error: could not parse actions: %v", err)
}
}
db := openAndMigrateDb()
if err := core.AddItems(db, []core.Item{{
@ -67,9 +76,10 @@ func itemAdd() {
Body: addItemBody,
Link: addItemLink,
Time: addItemTime,
Action: actions,
}}); err != nil {
log.Fatalf("Failed to add item: %s", err)
log.Fatalf("error: failed to add item: %s", err)
}
fmt.Printf("Added %s/%s\n", addItemSource, addItemId)
log.Printf("Added %s/%s\n", addItemSource, addItemId)
}

View File

@ -5,6 +5,7 @@ go build -o tmp/intake
rm tmp/intake.db* || true
export INTAKE_DATA_DIR="tmp"
tmp/intake migrate
tmp/intake source add -s feedtest
tmp/intake item add -s feedtest --id "this-item-has-no-title"
tmp/intake item add -s feedtest --title "This item has only a title"
@ -18,3 +19,7 @@ tmp/intake item add -s feedtest --title "Title, time" --time 1737780324
tmp/intake item add -s feedtest --title "Title, author, body" --author "Authorname" --body "Hello body!"
tmp/intake item add -s feedtest --title "Title, author, time, body" --author "Authorname" --time 1700000000 --body "Hello body!"
tmp/intake item add -s feedtest --title "Title, time, body" --time 1737780324 --body "Hello, body!"
tmp/intake source add -s spook
tmp/intake action add -s spook -a spookier -- jq -c '.title = .title + "o"'
tmp/intake item add -s spook --id boo --title "Boo" --action '{"spookier": true}'