intake/cmd/sourceFetch.go

72 lines
1.6 KiB
Go
Raw Normal View History

2025-01-16 15:55:38 +00:00
package cmd
import (
"bufio"
"fmt"
"os/exec"
"strings"
"github.com/spf13/cobra"
)
2025-01-21 16:42:59 +00:00
var sourceFetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch items for a source and update the feed",
2025-01-16 15:55:38 +00:00
Long: `Fetch items from a feed source using the configured "fetch" action.
Items returned by a successful fetch will be used to update the source.
2025-01-21 16:42:59 +00:00
A fetch is successful if all items output by the fetch are parsed successfully
and the exit code is 0. No changes will be made to the source if the fetch
does not succeed.`,
2025-01-16 15:55:38 +00:00
Run: func(cmd *cobra.Command, args []string) {
2025-01-21 16:42:59 +00:00
sourceFetch()
2025-01-16 15:55:38 +00:00
},
}
var source string
var dryRun bool
func init() {
2025-01-21 16:42:59 +00:00
sourceCmd.AddCommand(sourceFetchCmd)
2025-01-16 15:55:38 +00:00
2025-01-21 16:42:59 +00:00
sourceFetchCmd.Flags().StringVarP(&source, "source", "s", "", "Source name to fetch (required)")
sourceFetchCmd.MarkFlagRequired("source")
2025-01-16 15:55:38 +00:00
2025-01-21 16:42:59 +00:00
sourceFetchCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Instead of updating the source, print the fetched items")
2025-01-16 15:55:38 +00:00
}
2025-01-21 16:42:59 +00:00
func sourceFetch() {
2025-01-16 15:55:38 +00:00
fmt.Printf("Hello %s\n", source)
if dryRun {
return
}
cmd := exec.Command("sh", "-c", "echo Wout; sleep 1; echo 1>&2 Werr; sleep 1; echo Wout2; sleep 1; echo 1>&2 Werr2")
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
cout := make(chan int)
go func() {
scanout := bufio.NewScanner(stdout)
for scanout.Scan() {
text := strings.TrimSpace(scanout.Text())
fmt.Printf("[stdout] %s\n", text)
}
cout <- 1
}()
cerr := make(chan int)
go func() {
scanerr := bufio.NewScanner(stderr)
for scanerr.Scan() {
text := strings.TrimSpace(scanerr.Text())
fmt.Printf("[stderr] %s\n", text)
}
cerr <- 1
}()
cmd.Start()
<-cout
<-cerr
}