Add -json flag to publish list|show

Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
Joshua Colson
2021-09-21 18:46:59 -07:00
committed by Lorenzo Bolla
parent 129eb8644d
commit 899ed92ebc
11 changed files with 302 additions and 52 deletions

View File

@@ -109,52 +109,6 @@ func aptlyMirrorShowJson(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to show: %s", err)
}
// fmt.Printf("Name: %s\n", repo.Name)
// if repo.Status == deb.MirrorUpdating {
// fmt.Printf("Status: In Update (PID %d)\n", repo.WorkerPID)
// }
// fmt.Printf("Archive Root URL: %s\n", repo.ArchiveRoot)
// fmt.Printf("Distribution: %s\n", repo.Distribution)
// fmt.Printf("Components: %s\n", strings.Join(repo.Components, ", "))
// fmt.Printf("Architectures: %s\n", strings.Join(repo.Architectures, ", "))
// downloadSources := No
// if repo.DownloadSources {
// downloadSources = Yes
// }
// fmt.Printf("Download Sources: %s\n", downloadSources)
// downloadUdebs := No
// if repo.DownloadUdebs {
// downloadUdebs = Yes
// }
// fmt.Printf("Download .udebs: %s\n", downloadUdebs)
// if repo.Filter != "" {
// fmt.Printf("Filter: %s\n", repo.Filter)
// filterWithDeps := No
// if repo.FilterWithDeps {
// filterWithDeps = Yes
// }
// fmt.Printf("Filter With Deps: %s\n", filterWithDeps)
// }
// if repo.LastDownloadDate.IsZero() {
// fmt.Printf("Last update: never\n")
// } else {
// fmt.Printf("Last update: %s\n", repo.LastDownloadDate.Format("2006-01-02 15:04:05 MST"))
// fmt.Printf("Number of packages: %d\n", repo.NumPackages())
// }
// fmt.Printf("\nInformation from release file:\n")
// for _, k := range utils.StrMapSortedKeys(repo.Meta) {
// fmt.Printf("%s: %s\n", k, repo.Meta[k])
// }
// if withPackages {
// if repo.LastDownloadDate.IsZero() {
// fmt.Printf("Unable to show package list, mirror hasn't been downloaded yet.\n")
// } else {
// ListPackagesRefList(repo.RefList())
// }
// }
// include packages if requested
if withPackages {
if repo.RefList() != nil {
@@ -171,7 +125,6 @@ func aptlyMirrorShowJson(cmd *commander.Command, args []string) error {
}
}
// merge the repo object with the package list
var output []byte
if output, err = json.MarshalIndent(repo, "", " "); err == nil {
fmt.Println(string(output))

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"sort"
@@ -9,12 +10,23 @@ import (
)
func aptlyPublishList(cmd *commander.Command, args []string) error {
var err error
if len(args) != 0 {
cmd.Usage()
return commander.ErrCommandError
}
jsonFlag := cmd.Flag.Lookup("json").Value.Get().(bool)
if jsonFlag {
return aptlyPublishListJson(cmd, args)
}
return aptlyPublishListTxt(cmd, args)
}
func aptlyPublishListTxt(cmd *commander.Command, args []string) error {
var err error
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
published := make([]string, 0, context.CollectionFactory().PublishedRepoCollection().Len())
@@ -61,6 +73,40 @@ func aptlyPublishList(cmd *commander.Command, args []string) error {
return err
}
func aptlyPublishListJson(cmd *commander.Command, args []string) error {
var err error
repos := make([]*deb.PublishedRepo, 0, context.CollectionFactory().PublishedRepoCollection().Len())
err = context.CollectionFactory().PublishedRepoCollection().ForEach(func(repo *deb.PublishedRepo) error {
e := context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory())
if e != nil {
return e
}
repos = append(repos, repo)
return nil
})
if err != nil {
return fmt.Errorf("unable to load list of repos: %s", err)
}
context.CloseDatabase()
sort.Slice(repos, func(i, j int) bool {
return repos[i].GetPath() < repos[j].GetPath()
})
if output, e := json.MarshalIndent(repos, "", " "); e == nil {
fmt.Println(string(output))
} else {
err = e
}
return err
}
func makeCmdPublishList() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishList,
@@ -75,6 +121,7 @@ Example:
`,
}
cmd.Flag.Bool("json", false, "display list in JSON format")
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
return cmd

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"strings"
@@ -9,12 +10,23 @@ import (
)
func aptlyPublishShow(cmd *commander.Command, args []string) error {
var err error
if len(args) < 1 || len(args) > 2 {
cmd.Usage()
return commander.ErrCommandError
}
jsonFlag := cmd.Flag.Lookup("json").Value.Get().(bool)
if jsonFlag {
return aptlyPublishShowJson(cmd, args)
}
return aptlyPublishShowTxt(cmd, args)
}
func aptlyPublishShowTxt(cmd *commander.Command, args []string) error {
var err error
distribution := args[0]
param := "."
@@ -63,6 +75,36 @@ func aptlyPublishShow(cmd *commander.Command, args []string) error {
return err
}
func aptlyPublishShowJson(cmd *commander.Command, args []string) error {
var err error
distribution := args[0]
param := "."
if len(args) == 2 {
param = args[1]
}
storage, prefix := deb.ParsePrefix(param)
repo, err := context.CollectionFactory().PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
err = context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory())
if err != nil {
return err
}
var output []byte
if output, err = json.MarshalIndent(repo, "", " "); err == nil {
fmt.Println(string(output))
}
return err
}
func makeCmdPublishShow() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishShow,
@@ -77,5 +119,7 @@ Example:
`,
}
cmd.Flag.Bool("json", false, "display record in JSON format")
return cmd
}

View File

@@ -131,7 +131,6 @@ func aptlySnapshotShowJson(cmd *commander.Command, args []string) error {
}
// include packages if requested
// packageList := []string{}
if withPackages {
if snapshot.RefList() != nil {
var list *deb.PackageList
@@ -147,7 +146,6 @@ func aptlySnapshotShowJson(cmd *commander.Command, args []string) error {
}
}
// merge the repo object with the package list
var output []byte
if output, err = json.MarshalIndent(snapshot, "", " "); err == nil {
fmt.Println(string(output))

View File

@@ -282,7 +282,7 @@ func NewPublishedRepo(storage, prefix, distribution string, architectures []stri
return result, nil
}
// MarshalJSON requires object to be "loeaded completely"
// MarshalJSON requires object to be "loaded completely"
func (p *PublishedRepo) MarshalJSON() ([]byte, error) {
type sourceInfo struct {
Component, Name string
@@ -313,6 +313,7 @@ func (p *PublishedRepo) MarshalJSON() ([]byte, error) {
"NotAutomatic": p.NotAutomatic,
"ButAutomaticUpgrades": p.ButAutomaticUpgrades,
"Prefix": p.Prefix,
"Path": p.GetPath(),
"SourceKind": p.SourceKind,
"Sources": sources,
"Storage": p.Storage,
@@ -491,6 +492,17 @@ func (p *PublishedRepo) GetLabel() string {
return p.Label
}
// GetName returns the unique name of the repo
func (p *PublishedRepo) GetPath() string {
prefix := p.StoragePrefix()
if prefix == "" {
return p.Distribution
}
return fmt.Sprintf("%s/%s", prefix, p.Distribution)
}
// GetSuite returns default or manual Suite:
func (p *PublishedRepo) GetSuite() string {
if p.Suite == "" {

View File

@@ -0,0 +1 @@
[]

View File

@@ -0,0 +1,97 @@
[
{
"AcquireByHash": false,
"Architectures": [
"amd64",
"i386"
],
"ButAutomaticUpgrades": "",
"Distribution": "maverick",
"Label": "",
"NotAutomatic": "",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"Path": "./maverick",
"Prefix": ".",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "main",
"Name": "snap1"
}
],
"Storage": "",
"Suite": ""
},
{
"AcquireByHash": false,
"Architectures": [
"amd64"
],
"ButAutomaticUpgrades": "",
"Distribution": "wheezy",
"Label": "",
"NotAutomatic": "",
"Origin": "",
"Path": "ppa/smira/wheezy",
"Prefix": "ppa/smira",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "contrib",
"Name": "snap2"
}
],
"Storage": "",
"Suite": ""
},
{
"AcquireByHash": false,
"Architectures": [
"amd64",
"i386"
],
"ButAutomaticUpgrades": "",
"Distribution": "maverick",
"Label": "",
"NotAutomatic": "",
"Origin": "origin1",
"Path": "ppa/tr1/maverick",
"Prefix": "ppa/tr1",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "main",
"Name": "snap2"
}
],
"Storage": "",
"Suite": ""
},
{
"AcquireByHash": false,
"Architectures": [
"amd64",
"i386"
],
"ButAutomaticUpgrades": "",
"Distribution": "maverick",
"Label": "label1",
"NotAutomatic": "",
"Origin": "",
"Path": "ppa/tr2/maverick",
"Prefix": "ppa/tr2",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "main",
"Name": "snap2"
}
],
"Storage": "",
"Suite": ""
}
]

View File

@@ -0,0 +1,24 @@
{
"AcquireByHash": false,
"Architectures": [
"amd64",
"i386"
],
"ButAutomaticUpgrades": "",
"Distribution": "maverick",
"Label": "",
"NotAutomatic": "",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"Path": "./maverick",
"Prefix": ".",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "main",
"Name": "snap1"
}
],
"Storage": "",
"Suite": ""
}

View File

@@ -0,0 +1,24 @@
{
"AcquireByHash": false,
"Architectures": [
"amd64",
"i386"
],
"ButAutomaticUpgrades": "",
"Distribution": "maverick",
"Label": "",
"NotAutomatic": "",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"Path": "ppa/smira/maverick",
"Prefix": "ppa/smira",
"SkipContents": false,
"SourceKind": "snapshot",
"Sources": [
{
"Component": "main",
"Name": "snap1"
}
],
"Storage": "",
"Suite": ""
}

View File

@@ -41,3 +41,27 @@ class PublishList3Test(BaseTest):
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec -label=label1 snap2 ppa/tr2",
]
runCmd = "aptly publish list -raw"
class PublishList4Test(BaseTest):
"""
publish list json: empty list
"""
runCmd = "aptly publish list -json"
class PublishList5Test(BaseTest):
"""
publish list json: several repos list
"""
fixtureDB = True
fixturePool = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror gnuplot-maverick",
"aptly snapshot merge snap2 snap1",
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec snap1",
"aptly -architectures=amd64 publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec -distribution=wheezy -component=contrib snap2 ppa/smira",
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec -origin=origin1 snap2 ppa/tr1",
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec -label=label1 snap2 ppa/tr2",
]
runCmd = "aptly publish list -json"

View File

@@ -25,3 +25,29 @@ class PublishShow2Test(BaseTest):
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec snap1 ppa/smira",
]
runCmd = "aptly publish show maverick ppa/smira"
class PublishShow3Test(BaseTest):
"""
publish show json: existing snapshot
"""
fixtureDB = True
fixturePool = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror gnuplot-maverick",
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec snap1",
]
runCmd = "aptly publish show -json maverick"
class PublishShow4Test(BaseTest):
"""
publish show json: under prefix
"""
fixtureDB = True
fixturePool = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror gnuplot-maverick",
"aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec snap1 ppa/smira",
]
runCmd = "aptly publish show -json maverick ppa/smira"