fix: added some missing loggig code

main
Antonio De Lucreziis 2 years ago
parent 719022a75f
commit 746de81d59

@ -1,6 +1,10 @@
package operation package operation
import "github.com/aziis98/cabret" import (
"log"
"github.com/aziis98/cabret"
)
func init() { func init() {
registerType("chunk", &Chunk{}) registerType("chunk", &Chunk{})
@ -8,13 +12,13 @@ func init() {
// Chunk is a list operation that will group incoming items in groups of the given size // Chunk is a list operation that will group incoming items in groups of the given size
type Chunk struct { type Chunk struct {
Count int Size int
SkipRemaining bool SkipRemaining bool
} }
func (op *Chunk) Configure(options map[string]any) error { func (op *Chunk) Configure(options map[string]any) error {
var err error var err error
op.Count, err = getKey[int](options, "size") op.Size, err = getKey[int](options, "size")
if err != nil { if err != nil {
return err return err
} }
@ -27,16 +31,29 @@ func (op *Chunk) Configure(options map[string]any) error {
} }
func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) { func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
totalPages := len(items) / op.Count totalChunks := len(items) / op.Size
chunks := make([][]cabret.Content, totalPages, totalPages+1) chunks := make([][]cabret.Content, totalChunks, totalChunks+1)
for i := 0; i < totalPages; i++ { for i := 0; i < totalChunks; i++ {
chunks = append(chunks, items[i*op.Count:(i+1)*op.Count]) chunks = append(chunks, items[i*op.Size:(i+1)*op.Size])
} }
if !op.SkipRemaining { if !op.SkipRemaining {
chunks = append(chunks, items[totalPages*op.Count:]) if len(items)%op.Size != 0 {
lastExtraChunk := items[totalChunks*op.Size:]
chunks = append(chunks, lastExtraChunk)
if len(lastExtraChunk) != len(items)%op.Size {
panic("ehm last chunk and modulus should be equal")
}
log.Printf(`[operation.Chunk] chunked items in %d chunk(s), last with %d items`, totalChunks+1, len(lastExtraChunk))
} else {
log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks)
}
} else {
log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks)
} }
result := make([]cabret.Content, len(chunks)) result := make([]cabret.Content, len(chunks))
@ -44,8 +61,8 @@ func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
result[i] = cabret.Content{ result[i] = cabret.Content{
Type: cabret.MetadataOnly, Type: cabret.MetadataOnly,
Metadata: cabret.Map{ Metadata: cabret.Map{
"Page": i + 1, "Index": i,
"TotalPages": totalPages, "Total": totalChunks,
"Items": chunk, "Items": chunk,
}, },
} }

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"os/exec" "os/exec"
"github.com/aziis98/cabret" "github.com/aziis98/cabret"
@ -117,6 +118,8 @@ func (op *Program) ProcessItem(item cabret.Content) (*cabret.Content, error) {
return nil, err return nil, err
} }
log.Printf(`[operation.Program] running external program "%s" with "%s" input`, op.ShellCommand, op.IOFormat)
cmd := exec.Command("sh", "-c", op.ShellCommand) cmd := exec.Command("sh", "-c", op.ShellCommand)
cmd.Stdin = r cmd.Stdin = r
var buf bytes.Buffer var buf bytes.Buffer

@ -1,6 +1,8 @@
package operation package operation
import ( import (
"log"
"github.com/aziis98/cabret" "github.com/aziis98/cabret"
) )
@ -42,9 +44,13 @@ func (op *Slice) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
to = to + len(items) + 1 to = to + len(items) + 1
} }
if from < 0 { if from < 0 {
reverse(items)
from = from + len(items) + 1 from = from + len(items) + 1
from, to = to, from from, to = to, from
reverse(items)
log.Printf(`[operation.Slice] slicing and reversing items from %d to %d`, from, to)
} else {
log.Printf(`[operation.Slice] slicing items from %d to %d`, from, to)
} }
return items[from:to], nil return items[from:to], nil

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"log"
"github.com/aziis98/cabret" "github.com/aziis98/cabret"
) )
@ -41,6 +42,8 @@ func (op *Template) ProcessList(items []cabret.Content) ([]cabret.Content, error
tmpl := t.String() tmpl := t.String()
log.Printf(`[operation.Template] rendering template`)
var data bytes.Buffer var data bytes.Buffer
switch op.Engine { switch op.Engine {
case "html": case "html":

Loading…
Cancel
Save