mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-07-27 13:57:46 +00:00
Support for Go-style templating in format for aptly * search. #254
This commit is contained in:
+28
@@ -2,12 +2,14 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/smira/aptly/aptly"
|
"github.com/smira/aptly/aptly"
|
||||||
"github.com/smira/aptly/deb"
|
"github.com/smira/aptly/deb"
|
||||||
"github.com/smira/commander"
|
"github.com/smira/commander"
|
||||||
"github.com/smira/flag"
|
"github.com/smira/flag"
|
||||||
"os"
|
"os"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,6 +36,32 @@ func ListPackagesRefList(reflist *deb.PackageRefList) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintPackageList shows package list with specified format or default representation
|
||||||
|
func PrintPackageList(result *deb.PackageList, format string) error {
|
||||||
|
if format == "" {
|
||||||
|
return result.ForEach(func(p *deb.Package) error {
|
||||||
|
context.Progress().Printf("%s\n", p)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
formatTemplate, err := template.New("format").Parse(format)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error parsing -format template: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ForEach(func(p *deb.Package) error {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
err = formatTemplate.Execute(b, p.ExtendedStanza())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error applying template: %s", err)
|
||||||
|
}
|
||||||
|
context.Progress().Printf("%s\n", b.String())
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// LookupOption checks boolean flag with default (usually config) and command-line
|
// LookupOption checks boolean flag with default (usually config) and command-line
|
||||||
// setting
|
// setting
|
||||||
func LookupOption(defaultValue bool, flags *flag.FlagSet, name string) (result bool) {
|
func LookupOption(defaultValue bool, flags *flag.FlagSet, name string) (result bool) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
||||||
|
cmd.Flag.String("format", "", "custom format for result printing")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/smira/aptly/deb"
|
|
||||||
"github.com/smira/aptly/query"
|
"github.com/smira/aptly/query"
|
||||||
"github.com/smira/commander"
|
"github.com/smira/commander"
|
||||||
"github.com/smira/flag"
|
"github.com/smira/flag"
|
||||||
@@ -25,10 +24,8 @@ func aptlyPackageSearch(cmd *commander.Command, args []string) error {
|
|||||||
return fmt.Errorf("no results")
|
return fmt.Errorf("no results")
|
||||||
}
|
}
|
||||||
|
|
||||||
result.ForEach(func(p *deb.Package) error {
|
format := context.Flags().Lookup("format").Value.String()
|
||||||
context.Progress().Printf("%s\n", p)
|
PrintPackageList(result, format)
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -48,5 +45,7 @@ Example:
|
|||||||
Flag: *flag.NewFlagSet("aptly-package-search", flag.ExitOnError),
|
Flag: *flag.NewFlagSet("aptly-package-search", flag.ExitOnError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.Flag.String("format", "", "custom format for result printing")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
||||||
|
cmd.Flag.String("format", "", "custom format for result printing")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,10 +100,8 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error
|
|||||||
return fmt.Errorf("no results")
|
return fmt.Errorf("no results")
|
||||||
}
|
}
|
||||||
|
|
||||||
result.ForEach(func(p *deb.Package) error {
|
format := context.Flags().Lookup("format").Value.String()
|
||||||
context.Progress().Printf("%s\n", p)
|
PrintPackageList(result, format)
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -124,6 +122,7 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
cmd.Flag.Bool("with-deps", false, "include dependencies into search results")
|
||||||
|
cmd.Flag.String("format", "", "custom format for result printing")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-3
@@ -170,14 +170,19 @@ func (p *Package) String() string {
|
|||||||
return fmt.Sprintf("%s_%s_%s", p.Name, p.Version, p.Architecture)
|
return fmt.Sprintf("%s_%s_%s", p.Name, p.Version, p.Architecture)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaller interface
|
// ExtendedStanza returns package stanza enhanced with aptly-specific fields
|
||||||
func (p *Package) MarshalJSON() ([]byte, error) {
|
func (p *Package) ExtendedStanza() Stanza {
|
||||||
stanza := p.Stanza()
|
stanza := p.Stanza()
|
||||||
stanza["FilesHash"] = fmt.Sprintf("%08x", p.FilesHash)
|
stanza["FilesHash"] = fmt.Sprintf("%08x", p.FilesHash)
|
||||||
stanza["Key"] = string(p.Key(""))
|
stanza["Key"] = string(p.Key(""))
|
||||||
stanza["ShortKey"] = string(p.ShortKey(""))
|
stanza["ShortKey"] = string(p.ShortKey(""))
|
||||||
|
|
||||||
return json.Marshal(stanza)
|
return stanza
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaller interface
|
||||||
|
func (p *Package) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(p.ExtendedStanza())
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetField returns fields from package
|
// GetField returns fields from package
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -34,3 +34,12 @@ class SearchMirror4Test(BaseTest):
|
|||||||
fixtureDB = True
|
fixtureDB = True
|
||||||
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
runCmd = "aptly mirror search -with-deps wheezy-main 'Name (nginx)'"
|
runCmd = "aptly mirror search -with-deps wheezy-main 'Name (nginx)'"
|
||||||
|
|
||||||
|
|
||||||
|
class SearchMirror5Test(BaseTest):
|
||||||
|
"""
|
||||||
|
search mirror: regular search
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
|
runCmd = "aptly mirror search -format='{{.Package}}#{{.Version}}' wheezy-main '$$Architecture (i386), Name (% *-dev)'"
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -47,3 +47,13 @@ class SearchSnapshot5Test(BaseTest):
|
|||||||
fixtureCmds = ["aptly snapshot create wheezy-main from mirror wheezy-main"]
|
fixtureCmds = ["aptly snapshot create wheezy-main from mirror wheezy-main"]
|
||||||
runCmd = "aptly snapshot search -with-deps wheezy-main 'Name (no-such-package)'"
|
runCmd = "aptly snapshot search -with-deps wheezy-main 'Name (no-such-package)'"
|
||||||
expectedCode = 1
|
expectedCode = 1
|
||||||
|
|
||||||
|
|
||||||
|
class SearchSnapshot6Test(BaseTest):
|
||||||
|
"""
|
||||||
|
search snapshot: with format
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
|
fixtureCmds = ["aptly snapshot create wheezy-main from mirror wheezy-main"]
|
||||||
|
runCmd = "aptly snapshot search -format='{{.Package}}#{{.Version}}' wheezy-main '$$Architecture (i386), Name (% *-dev)'"
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -37,3 +37,13 @@ class SearchRepo4Test(BaseTest):
|
|||||||
fixtureCmds = ["aptly repo create wheezy-main", "aptly repo import wheezy-main wheezy-main Name"]
|
fixtureCmds = ["aptly repo create wheezy-main", "aptly repo import wheezy-main wheezy-main Name"]
|
||||||
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
runCmd = "aptly repo search -with-deps wheezy-main 'Name (nginx)'"
|
runCmd = "aptly repo search -with-deps wheezy-main 'Name (nginx)'"
|
||||||
|
|
||||||
|
|
||||||
|
class SearchRepo5Test(BaseTest):
|
||||||
|
"""
|
||||||
|
search repo: with -format
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
|
fixtureCmds = ["aptly repo create wheezy-main", "aptly repo import wheezy-main wheezy-main Name"]
|
||||||
|
runCmd = "aptly repo search -format='{{.Package}}#{{.Version}}' wheezy-main '$$Architecture (i386), Name (% *-dev)'"
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -33,3 +33,12 @@ class SearchPackage4Test(BaseTest):
|
|||||||
fixtureDB = True
|
fixtureDB = True
|
||||||
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
runCmd = "aptly package search coreutils"
|
runCmd = "aptly package search coreutils"
|
||||||
|
|
||||||
|
|
||||||
|
class SearchPackage5Test(BaseTest):
|
||||||
|
"""
|
||||||
|
search package: with format
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
outputMatchPrepare = lambda _, s: "\n".join(sorted(s.split("\n")))
|
||||||
|
runCmd = "aptly package search -format='{{.Package}}#{{.Version}}' '$$Architecture (i386), Name (% *-dev)'"
|
||||||
|
|||||||
Reference in New Issue
Block a user