Skip to content

Commit

Permalink
Added template print output.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa committed Dec 31, 2021
1 parent 2a46460 commit 07d4377
Show file tree
Hide file tree
Showing 39 changed files with 397 additions and 272 deletions.
2 changes: 1 addition & 1 deletion actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strconv"

"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/objects"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/types"
)

Expand Down
2 changes: 1 addition & 1 deletion actions/batch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package actions

import (
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/types"
)

Expand Down
71 changes: 0 additions & 71 deletions actions/hash.go

This file was deleted.

2 changes: 1 addition & 1 deletion actions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strconv"

"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/objects"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/types"
)

Expand Down
27 changes: 27 additions & 0 deletions actions/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package actions

import (
"github.com/rakshasa/rbedit/data/templates"
"github.com/rakshasa/rbedit/types"
)

func NewTemplateExecuteAction(output types.Output, templateText string) types.InputResultFunc {
return func(metadata types.IOMetadata, object interface{}) error {
value, err := templates.ExecuteTemplate(metadata, templateText)
if err != nil {
return err
}

if err := output.Execute(metadata, value); err != nil {
return err
}

return nil
}
}

func NewTemplateExecute(templateText string) ActionFunc {
return func(output types.Output) types.InputResultFunc {
return NewTemplateExecuteAction(output, templateText)
}
}
4 changes: 2 additions & 2 deletions actions/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func NewListValueAction(output types.Output, value []interface{}) types.InputResultFunc {
return func(metadata types.IOMetadata, rootObj interface{}) error {
return func(metadata types.IOMetadata, object interface{}) error {
if err := output.Execute(metadata, value); err != nil {
return err
}
Expand All @@ -21,7 +21,7 @@ func NewListValue(value []interface{}) ActionFunc {
}

func NewStringValueAction(output types.Output, value string) types.InputResultFunc {
return func(metadata types.IOMetadata, rootObj interface{}) error {
return func(metadata types.IOMetadata, object interface{}) error {
if err := output.Execute(metadata, value); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion actions/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package actions
import (
"fmt"

"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/objects"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/types"
)

Expand Down
7 changes: 4 additions & 3 deletions cmd/common/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package common

import (
"github.com/rakshasa/rbedit/actions"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/encodings"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func newAnnounceGetCommand() *cobra.Command {
func announceGetCmdRun(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodePrint(), outputs.NewStdOutput()),
WithDefaultOutput(encodings.NewEncodePrint(), outputs.NewStandardOutput()),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down Expand Up @@ -85,7 +86,7 @@ func newAnnouncePutCommand() *cobra.Command {
func announcePutCmdRun(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodeTorrentBencode(), nil),
WithDefaultOutput(encodings.NewEncodeTorrentBencode(), nil),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down
13 changes: 7 additions & 6 deletions cmd/common/announce_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"

"github.com/rakshasa/rbedit/actions"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/encodings"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/types"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func announceListAppendTrackerCmdRun(cmd *cobra.Command, args []string) {

metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodeTorrentBencode(), nil),
WithDefaultOutput(encodings.NewEncodeTorrentBencode(), nil),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand All @@ -89,7 +90,7 @@ func newAnnounceListClearCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodeTorrentBencode(), nil),
WithDefaultOutput(encodings.NewEncodeTorrentBencode(), nil),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down Expand Up @@ -121,7 +122,7 @@ func newAnnounceListClearCategoryCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodeTorrentBencode(), nil),
WithDefaultOutput(encodings.NewEncodeTorrentBencode(), nil),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down Expand Up @@ -160,7 +161,7 @@ func newAnnounceListGetCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodePrintAsListOfLists(), outputs.NewStdOutput()),
WithDefaultOutput(encodings.NewEncodePrintAsListOfLists(), outputs.NewStandardOutput()),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down Expand Up @@ -196,7 +197,7 @@ func newAnnounceListGetCategoryCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodePrintList(), outputs.NewStdOutput()),
WithDefaultOutput(encodings.NewEncodePrintList(), outputs.NewStandardOutput()),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down
9 changes: 6 additions & 3 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const (
inputFlagName = "input"
inputBatchFlagName = "batch"
inputNotTorrentFlagName = "not-torrent"
outputFlagName = "output"
outputInplaceFlagName = "inplace"
outputTemplateFlagName = "output-template"

outputFlagName = "output"
outputInplaceFlagName = "inplace"
outputTemplateFlagName = "output-template"
printTemplateFlagName = "print-template"

bencodeValueFlagName = "bencode"
integerValueFlagName = "int"
Expand All @@ -31,6 +33,7 @@ func addInputFlags(cmd *cobra.Command) {
cmd.Flags().VarP(&nonEmptyString{}, inputFlagName, "i", "Input filename")
cmd.Flags().Bool(inputBatchFlagName, false, "Input as batch of filenames")
cmd.Flags().Bool(inputNotTorrentFlagName, false, "Disable torrent verification on input")
cmd.Flags().Var(&nonEmptyString{}, printTemplateFlagName, "Print template output")
}

func addDataOutputFlags(cmd *cobra.Command) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/common/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package common

import (
"github.com/rakshasa/rbedit/actions"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/encodings"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/spf13/cobra"
)

Expand All @@ -29,7 +30,7 @@ func getCmdRun(cmd *cobra.Command, args []string) {

metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodePrint(), outputs.NewStdOutput()),
WithDefaultOutput(encodings.NewEncodePrint(), outputs.NewStandardOutput()),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down
11 changes: 5 additions & 6 deletions cmd/common/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package common

import (
"github.com/rakshasa/rbedit/actions"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/encodings"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,17 +39,15 @@ func newHashInfoCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
metadata, input, output, err := metadataFromCommand(cmd,
WithDefaultInput(),
WithDefaultOutput(outputs.NewEncodeAsHexString(), outputs.NewStdOutput()),
WithDefaultOutput(encodings.NewEncodeAsHexString(), outputs.NewStandardOutput()),
)
if err != nil {
printCommandErrorAndExit(cmd, err)
}

batch := actions.NewBatch()
batch.Append(actions.NewCalculateInfoHash())
batch.Append(actions.NewCachedInfoHash())

// batch.Append(actions.NewSHA1(infoHashKeys, types.ObjectResultTarget))
// TODO: Allow both Bytes and Hash alone.
batch.Append(actions.NewTemplateExecute("{{ .Input.TorrentInfo.Hash.Bytes }}"))

if err := input.Execute(metadata, batch.CreateFunction(output)); err != nil {
printCommandErrorAndExit(cmd, err)
Expand Down
29 changes: 22 additions & 7 deletions cmd/common/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"strings"

"github.com/rakshasa/rbedit/inputs"
"github.com/rakshasa/rbedit/outputs"
"github.com/rakshasa/rbedit/data/encodings"
"github.com/rakshasa/rbedit/data/inputs"
"github.com/rakshasa/rbedit/data/outputs"
"github.com/rakshasa/rbedit/types"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -32,6 +33,7 @@ const (
fileOutputTypeName outputType = "file-output"
fileOutputWithInplaceTypeName outputType = "inplace-output"
fileOutputWithTemplateFilenameTypeName outputType = "template-output"
printTemplateTypeName outputType = "print-template"
)

func WithDefaultInput() metadataOpFunction {
Expand Down Expand Up @@ -140,9 +142,9 @@ func inputSourceAndTypeFromFlagSet(flagSet *flag.FlagSet, opts *metadataOptions)
}

if inputIsTorrent {
decodeFn = inputs.NewDecodeTorrentBencode()
decodeFn = encodings.NewDecodeTorrentBencode()
} else {
decodeFn = inputs.NewDecodeGenericBencode()
decodeFn = encodings.NewDecodeGenericBencode()
}

switch inputType {
Expand All @@ -159,6 +161,7 @@ func inputSourceAndTypeFromFlagSet(flagSet *flag.FlagSet, opts *metadataOptions)

func outputDestinatinoAndTypeFromFlagSet(flagSet *flag.FlagSet, opts *metadataOptions) (types.Output, error) {
var outputValue string
var encodeValue string
var outputType outputType

outputFlags := []string{}
Expand All @@ -167,14 +170,19 @@ func outputDestinatinoAndTypeFromFlagSet(flagSet *flag.FlagSet, opts *metadataOp
outputValue = v
outputFlags = append(outputFlags, outputFlagName)
}
if flag := getChangedTrue(flagSet, outputInplaceFlagName); flag {
outputType = fileOutputWithInplaceTypeName
outputFlags = append(outputFlags, outputInplaceFlagName)
}
if v, ok := getChangedString(flagSet, outputTemplateFlagName); ok {
outputValue = v
outputType = fileOutputWithTemplateFilenameTypeName
outputFlags = append(outputFlags, outputTemplateFlagName)
}
if flag := getChangedTrue(flagSet, outputInplaceFlagName); flag {
outputType = fileOutputWithInplaceTypeName
outputFlags = append(outputFlags, outputInplaceFlagName)
if v, ok := getChangedString(flagSet, printTemplateFlagName); ok {
encodeValue = v
outputType = printTemplateTypeName
outputFlags = append(outputFlags, printTemplateFlagName)
}

if len(outputFlags) > 1 {
Expand All @@ -199,6 +207,13 @@ func outputDestinatinoAndTypeFromFlagSet(flagSet *flag.FlagSet, opts *metadataOp

return outputs.NewSingleOutput(opts.defaultEncodeFn, outputs.NewFileOutputWithTemplateFilename(outputValue)), nil

case printTemplateTypeName:
if len(encodeValue) == 0 {
return nil, fmt.Errorf("missing valid print template")
}

return outputs.NewSingleOutput(encodings.NewEncodePrintTemplate(encodeValue), outputs.NewStandardOutput()), nil

case "":
if opts.defaultOutputFn == nil {
return nil, fmt.Errorf("unexpected missing default output")
Expand Down
Loading

0 comments on commit 07d4377

Please sign in to comment.