#23 WIP: Add output templating
Merged 4 years ago by qulogic. Opened 5 years ago by qulogic.
qulogic/golist template  into  master

file modified
+34 -4
@@ -6,10 +6,13 @@ 

  	"os"

  	"regexp"

  	"sort"

+ 	"strconv"

  	"strings"

+ 	"text/template"

  

- 	"pagure.io/golist/pkg/util"

  	"github.com/urfave/cli"

+ 

+ 	"pagure.io/golist/pkg/util"

  )

  

  // TODO(jchaloup):
@@ -74,6 +77,19 @@ 

  			Name:  "json",

  			Usage: "Output as JSON artefact",

  		},

+ 		cli.StringFlag{

+ 			Name:  "template",

+ 			Usage: "Template used to print output",

+ 			Value: `{{.}}\n`,

+ 		},

+ 	}

+ 

+ 	getTemplate := func(output_fmt string) (*template.Template, error) {

+ 		str, err := strconv.Unquote(`"` + output_fmt + `"`)

+ 		if err != nil {

+ 			return nil, fmt.Errorf("Unparseable template string: %v", err)

+ 		}

+ 		return template.New("output").Parse(str)

  	}

  

  	app.Action = func(c *cli.Context) error {
@@ -86,6 +102,11 @@ 

  			return fmt.Errorf("At least one of --provided, --imported, --to-install or --json must be set")

  		}

  

+ 		tmpl, err := getTemplate(c.String("template"))

+ 		if err != nil {

+ 			return err

+ 		}

+ 

  		ignore := &util.Ignore{}

  

  		for _, dir := range c.StringSlice("ignore-tree") {
@@ -129,7 +150,10 @@ 

  			}

  			sort.Strings(pkgs)

  			for _, item := range pkgs {

- 				fmt.Println(item)

+ 				err := tmpl.Execute(os.Stdout, item)

+ 				if err != nil {

+ 					return err

+ 				}

  			}

  			return nil

  		}
@@ -141,7 +165,10 @@ 

  			}

  			sort.Strings(pkgs)

  			for _, item := range pkgs {

- 				fmt.Println(item)

+ 				err := tmpl.Execute(os.Stdout, item)

+ 				if err != nil {

+ 					return err

+ 				}

  			}

  			return nil

  		}
@@ -153,7 +180,10 @@ 

  			}

  			sort.Strings(pkgs)

  			for _, item := range pkgs {

- 				fmt.Println(item)

+ 				err := tmpl.Execute(os.Stdout, item)

+ 				if err != nil {

+ 					return err

+ 				}

  			}

  			return nil

  		}

This adds an option --template that is used to determine how output is printed. The given format string is first unquoted (to allow for things like \n) and then passed to Go's builtin templater. This is pretty powerful, but for now, I've just passed the strings that would normally be printed. You can now do something like:

$ golist --package-path pagure.io/golist --imported \
    --template 'BuildRequires: golang({{.}})\n'
BuildRequires: golang(github.com/urfave/cli)
BuildRequires: golang(pagure.io/golist/pkg/util)

It may be possible to insert additional information there, if I pass something more featureful than just the name, but this is probably enough to fix #13.

rebased onto 4d28439

5 years ago

rebased onto 8964398

5 years ago

@qulogic Looks good (not tested). Not sure the \n is necessary for BuildRequires but having the option to put everything on one line may be useful for go test

On some further thought, there's not really any other information we have available for dependencies. So I'll just merge this as is, and we can investigate any additional replacements later.

Pull-Request has been merged by qulogic

4 years ago
Metadata