Add --diff to action execute

This commit is contained in:
Tim Van Baak 2025-01-23 13:50:45 -08:00
parent 4a75e8e814
commit 1fb9e5853c

View File

@ -32,6 +32,7 @@ var actionExecuteAction string
var actionExecuteItem string
var actionExecuteFormat string
var actionExecuteDryRun bool
var actionExecuteDiff bool
func init() {
actionCmd.AddCommand(actionExecuteCmd)
@ -47,6 +48,8 @@ func init() {
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")
}
func actionExecute() {
@ -79,16 +82,40 @@ func actionExecute() {
log.Fatalf("error: failed to serialize item: %v", err)
}
newItem, err := core.Execute(actionExecuteSource, argv, nil, string(itemJson), time.Minute)
res, err := core.Execute(actionExecuteSource, argv, nil, string(itemJson), time.Minute)
if err != nil {
log.Fatalf("error: failed to execute action: %v", err)
}
if len(newItem) != 1 {
log.Fatalf("error: expected action to produce exactly one item, got %d", len(newItem))
if len(res) != 1 {
log.Fatalf("error: expected action to produce exactly one item, got %d", len(res))
}
newItem := res[0]
newItem.Active = item.Active // These fields can't
newItem.Created = item.Created // be updated by actions
if actionExecuteDiff {
if item.Title != newItem.Title {
log.Printf("title: %s => %s", item.Title, newItem.Title)
}
if item.Author != newItem.Author {
log.Printf("author: %s => %s", item.Author, newItem.Author)
}
if item.Body != newItem.Body {
log.Printf("body: %s => %s", item.Body, newItem.Body)
}
if item.Link != newItem.Link {
log.Printf("link: %s => %s", item.Link, newItem.Link)
}
if item.Time != newItem.Time {
log.Printf("time: %d => %d", item.Time, newItem.Time)
}
if item == newItem {
log.Printf("no changes\n")
}
}
if actionExecuteDryRun {
fmt.Println(formatter(newItem[0]))
fmt.Println(formatter(res[0]))
return
}