cobra init

This commit is contained in:
Tim Van Baak 2025-01-16 07:55:38 -08:00
parent a47c1f1bfb
commit 390f972b0e
6 changed files with 114 additions and 35 deletions

25
cmd/root.go Normal file
View File

@ -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})
}

69
cmd/update.go Normal file
View File

@ -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
}

View File

@ -29,6 +29,7 @@
pkgs.gopls
pkgs.go-tools
pkgs.gotools
pkgs.cobra-cli
];
};
};

7
go.mod
View File

@ -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
)

10
go.sum Normal file
View File

@ -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=

37
main.go
View File

@ -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()
}