diff --git a/Makefile b/Makefile index f208980..cabb0b8 100644 --- a/Makefile +++ b/Makefile @@ -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 \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 \ No newline at end of file diff --git a/cmd/actionExecute.go b/cmd/actionExecute.go index 6037d59..d68633f 100644 --- a/cmd/actionExecute.go +++ b/cmd/actionExecute.go @@ -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) diff --git a/cmd/feed.go b/cmd/feed.go index be7239b..c312f86 100644 --- a/cmd/feed.go +++ b/cmd/feed.go @@ -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) } } diff --git a/cmd/itemAdd.go b/cmd/itemAdd.go index 25374a3..64f17d2 100644 --- a/cmd/itemAdd.go +++ b/cmd/itemAdd.go @@ -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) } diff --git a/test/test_items.sh b/test/test_items.sh index 2e946b7..c14310d 100755 --- a/test/test_items.sh +++ b/test/test_items.sh @@ -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}'