Skip to content

Commit

Permalink
add --interval and --on-change support
Browse files Browse the repository at this point in the history
  • Loading branch information
bjesus committed Sep 2, 2024
1 parent 76d1f0c commit 3cd1eb4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
55 changes: 42 additions & 13 deletions cmd/pipet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -38,7 +42,7 @@ func main() {
},
&cli.IntFlag{
Name: "interval",
Value: 3,
Value: 0,
Usage: "Maximum number of pages to scrape",
},
&cli.StringFlag{
Expand All @@ -64,7 +68,9 @@ func runPipet(c *cli.Context, specFile string) error {
jsonOutput := c.Bool("json")
separators := c.StringSlice("separator")
templateFile := c.String("template")
onChange := c.String("on-change")
maxPages := c.Int("max-pages")
interval := c.Int("interval")

pipet := &common.PipetApp{
MaxPages: maxPages,
Expand All @@ -77,18 +83,41 @@ func runPipet(c *cli.Context, specFile string) error {
return fmt.Errorf("error parsing spec file: %w", err)
}

log.Println("Executing blocks")
err = app.ExecuteBlocks(pipet)
if err != nil {
return fmt.Errorf("error executing blocks: %w", err)
}
iterate := true
previousValue := ""

for iterate {
newValue := ""
log.Println("Executing blocks")
err = app.ExecuteBlocks(pipet)
if err != nil {
return fmt.Errorf("error executing blocks: %w", err)
}

log.Println("Generating output")
if jsonOutput {
return outputs.OutputJSON(pipet)
} else if templateFile != "" {
return outputs.OutputTemplate(pipet, templateFile)
} else {
return outputs.OutputText(pipet)
log.Println("Generating output")
if jsonOutput {
newValue = outputs.OutputJSON(pipet)
} else if templateFile != "" {
newValue = outputs.OutputTemplate(pipet, templateFile)
} else {
newValue = outputs.OutputText(pipet)
}

fmt.Print(newValue)

if interval > 0 {
if onChange != "" && previousValue != newValue {
command := strings.ReplaceAll(onChange, "{}", strconv.Quote(newValue))
log.Println("Executing on change command: " + command)
cmd := exec.Command("bash", "-c", command)
cmd.Output()
previousValue = newValue
}
pipet.Data = []interface{}{}
time.Sleep(time.Duration(interval) * time.Second)
} else {
iterate = false
}
}
return nil
}
12 changes: 4 additions & 8 deletions outputs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package outputs

import (
"encoding/json"
"log"

"github.com/bjesus/pipet/common"
)

func OutputJSON(app *common.PipetApp) error {
jsonData, err := json.MarshalIndent(app.Data, "", " ")
if err != nil {
return err
}
log.Println(string(jsonData))
return nil
func OutputJSON(app *common.PipetApp) string {
jsonData, _ := json.MarshalIndent(app.Data, "", " ")
return string(jsonData)

}
14 changes: 6 additions & 8 deletions outputs/template.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package outputs

import (
"bytes"
"html/template"
"os"

"github.com/bjesus/pipet/common"
)

func OutputTemplate(app *common.PipetApp, templateFile string) error {
tmpl, err := template.ParseFiles(templateFile)
if err != nil {
return err
}

return tmpl.Execute(os.Stdout, app.Data)
func OutputTemplate(app *common.PipetApp, templateFile string) string {
tmpl, _ := template.ParseFiles(templateFile)
var doc bytes.Buffer
tmpl.Execute(&doc, app.Data)
return doc.String()
}
7 changes: 3 additions & 4 deletions outputs/text.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package outputs

import (
"log"
"strings"

"github.com/bjesus/pipet/common"
"github.com/bjesus/pipet/utils"
)

func OutputText(app *common.PipetApp) error {
func OutputText(app *common.PipetApp) string {
flattenedData := utils.FlattenData(app.Data, 0)
log.Println(strings.Join(flattenedData, utils.GetSeparator(app, 0)))
return nil
return (strings.Join(flattenedData, utils.GetSeparator(app, 0)))

}

0 comments on commit 3cd1eb4

Please sign in to comment.