From 390f972b0ed7d422c342e65f9ab7d5242ccc6d1f Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 16 Jan 2025 07:55:38 -0800 Subject: [PATCH] cobra init --- cmd/root.go | 25 +++++++++++++++++++ cmd/update.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 1 + go.mod | 7 ++++++ go.sum | 10 ++++++++ main.go | 37 ++------------------------- 6 files changed, 114 insertions(+), 35 deletions(-) create mode 100644 cmd/root.go create mode 100644 cmd/update.go create mode 100644 go.sum diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..6b20036 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "intake", + Short: "Universal and extensible feed aggregator", + Long: `intake, the universal and extensible feed aggregator`, +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Disable the automatic help command + rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) +} diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..6232e2b --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,69 @@ +package cmd + +import ( + "bufio" + "fmt" + "os/exec" + "strings" + + "github.com/spf13/cobra" +) + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Fetch items for a source and update it", + 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. +No changes will be made to the source if the fetch does not succeed.`, + Run: func(cmd *cobra.Command, args []string) { + update() + }, +} + +var source string +var dryRun bool + +func init() { + rootCmd.AddCommand(updateCmd) + + updateCmd.Flags().StringVarP(&source, "source", "s", "", "Source name to fetch (required)") + updateCmd.MarkFlagRequired("source") + + updateCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Instead of updating the source, print the fetched items") +} + +func update() { + 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 +} diff --git a/flake.nix b/flake.nix index 16b40cd..2138377 100644 --- a/flake.nix +++ b/flake.nix @@ -29,6 +29,7 @@ pkgs.gopls pkgs.go-tools pkgs.gotools + pkgs.cobra-cli ]; }; }; diff --git a/go.mod b/go.mod index 720d631..30e6e5e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/Jaculabilis/intake go 1.23.4 + +require github.com/spf13/cobra v1.8.1 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..912390a --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 7268dce..80c6a48 100644 --- a/main.go +++ b/main.go @@ -1,40 +1,7 @@ package main -import ( - "bufio" - "fmt" - "os/exec" - "strings" -) +import "github.com/Jaculabilis/intake/cmd" func main() { - fmt.Printf("Hello\n") - - 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 + cmd.Execute() }