Add -json flag to snapshot show|list

Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
Joshua Colson
2021-09-21 06:11:45 -07:00
committed by Lorenzo Bolla
parent f9c0d99790
commit 0f1575d5af
20 changed files with 583 additions and 15 deletions

View File

@@ -73,7 +73,7 @@ func aptlyRepoListTxt(cmd *commander.Command, args []string) error {
func aptlyRepoListJson(cmd *commander.Command, args []string) error {
var err error
jsonRepos := make([]*deb.LocalRepo, context.CollectionFactory().LocalRepoCollection().Len())
repos := make([]*deb.LocalRepo, context.CollectionFactory().LocalRepoCollection().Len())
i := 0
context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
e := context.CollectionFactory().LocalRepoCollection().LoadComplete(repo)
@@ -81,17 +81,17 @@ func aptlyRepoListJson(cmd *commander.Command, args []string) error {
return e
}
jsonRepos[i] = repo
repos[i] = repo
i++
return nil
})
context.CloseDatabase()
sort.Slice(jsonRepos, func(i, j int) bool {
return jsonRepos[i].Name < jsonRepos[j].Name
sort.Slice(repos, func(i, j int) bool {
return repos[i].Name < repos[j].Name
})
if output, e := json.MarshalIndent(jsonRepos, "", " "); e == nil {
if output, e := json.MarshalIndent(repos, "", " "); e == nil {
fmt.Println(string(output))
} else {
err = e

View File

@@ -40,7 +40,7 @@ func aptlyRepoShow(cmd *commander.Command, args []string) error {
var list *deb.PackageList
list, err = deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), context.Progress())
if err == nil {
packageList = list.Strings() // similar output to /api/{repo}/packages
packageList = list.FullNames()
}
}

View File

@@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"github.com/aptly-dev/aptly/deb"
@@ -8,12 +9,23 @@ import (
)
func aptlySnapshotList(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 aptlySnapshotListJson(cmd, args)
}
return aptlySnapshotListTxt(cmd, args)
}
func aptlySnapshotListTxt(cmd *commander.Command, args []string) error {
var err error
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string)
@@ -46,6 +58,29 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error {
return err
}
func aptlySnapshotListJson(cmd *commander.Command, args []string) error {
var err error
sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string)
collection := context.CollectionFactory().SnapshotCollection()
jsonSnapshots := make([]*deb.Snapshot, collection.Len())
i := 0
collection.ForEachSorted(sortMethodString, func(snapshot *deb.Snapshot) error {
jsonSnapshots[i] = snapshot
i++
return nil
})
if output, e := json.MarshalIndent(jsonSnapshots, "", " "); e == nil {
fmt.Println(string(output))
} else {
err = e
}
return err
}
func makeCmdSnapshotList() *commander.Command {
cmd := &commander.Command{
Run: aptlySnapshotList,
@@ -60,6 +95,7 @@ Example:
`,
}
cmd.Flag.Bool("json", false, "display list in JSON format")
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
cmd.Flag.String("sort", "name", "display list in 'name' or creation 'time' order")

View File

@@ -1,7 +1,9 @@
package cmd
import (
"encoding/json"
"fmt"
"sort"
"github.com/aptly-dev/aptly/deb"
"github.com/smira/commander"
@@ -9,14 +11,26 @@ import (
)
func aptlySnapshotShow(cmd *commander.Command, args []string) error {
var err error
if len(args) != 1 {
cmd.Usage()
return commander.ErrCommandError
}
jsonFlag := cmd.Flag.Lookup("json").Value.Get().(bool)
if jsonFlag {
return aptlySnapshotShowJson(cmd, args)
}
return aptlySnapshotShowTxt(cmd, args)
}
func aptlySnapshotShowTxt(cmd *commander.Command, args []string) error {
var err error
name := args[0]
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
@@ -64,7 +78,6 @@ func aptlySnapshotShow(cmd *commander.Command, args []string) error {
}
}
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
if withPackages {
ListPackagesRefList(snapshot.RefList())
}
@@ -72,6 +85,78 @@ func aptlySnapshotShow(cmd *commander.Command, args []string) error {
return err
}
func aptlySnapshotShowJson(cmd *commander.Command, args []string) error {
var err error
name := args[0]
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
// include the sources
if len(snapshot.SourceIDs) > 0 {
for _, sourceID := range snapshot.SourceIDs {
if snapshot.SourceKind == deb.SourceSnapshot {
var source *deb.Snapshot
source, err = context.CollectionFactory().SnapshotCollection().ByUUID(sourceID)
if err != nil {
continue
}
snapshot.Snapshots = append(snapshot.Snapshots, source)
} else if snapshot.SourceKind == deb.SourceLocalRepo {
var source *deb.LocalRepo
source, err = context.CollectionFactory().LocalRepoCollection().ByUUID(sourceID)
if err != nil {
continue
}
snapshot.LocalRepos = append(snapshot.LocalRepos, source)
} else if snapshot.SourceKind == deb.SourceRemoteRepo {
var source *deb.RemoteRepo
source, err = context.CollectionFactory().RemoteRepoCollection().ByUUID(sourceID)
if err != nil {
continue
}
source.ReleaseFiles = nil // do not include the release file info
snapshot.RemoteRepos = append(snapshot.RemoteRepos, source)
}
}
}
// include packages if requested
// packageList := []string{}
if withPackages {
if snapshot.RefList() != nil {
var list *deb.PackageList
list, err = deb.NewPackageListFromRefList(snapshot.RefList(), context.CollectionFactory().PackageCollection(), context.Progress())
list.PrepareIndex()
list.ForEachIndexed(func(p *deb.Package) error {
snapshot.Packages = append(snapshot.Packages, p.GetFullName())
return nil
})
sort.Strings(snapshot.Packages)
}
}
// merge the repo object with the package list
var output []byte
if output, err = json.MarshalIndent(snapshot, "", " "); err == nil {
fmt.Println(string(output))
}
return err
}
func makeCmdSnapshotShow() *commander.Command {
cmd := &commander.Command{
Run: aptlySnapshotShow,
@@ -87,6 +172,7 @@ Example:
Flag: *flag.NewFlagSet("aptly-snapshot-show", flag.ExitOnError),
}
cmd.Flag.Bool("json", false, "display record in JSON format")
cmd.Flag.Bool("with-packages", false, "show list of packages")
return cmd

View File

@@ -266,6 +266,19 @@ func (l *PackageList) Strings() []string {
return result
}
// FullNames builds a list of package {name}_{version}_{arch}
func (l *PackageList) FullNames() []string {
result := make([]string, l.Len())
i := 0
for _, p := range l.packages {
result[i] = p.GetFullName()
i++
}
return result
}
// depSliceDeduplicate removes dups in slice of Dependencies
func depSliceDeduplicate(s []Dependency) []Dependency {
l := len(s)

View File

@@ -361,6 +361,11 @@ func (p *Package) GetName() string {
return p.Name
}
// GetFullName returns the package full name
func (p *Package) GetFullName() string {
return strings.Join([]string{p.Name, p.Version, p.Architecture}, "_")
}
// GetVersion returns package version
func (p *Package) GetVersion() string {
return p.Version

View File

@@ -33,7 +33,7 @@ const (
// RemoteRepo represents remote (fetchable) Debian repository.
//
// Repostitory could be filtered when fetching by components, architectures
// Repository could be filtered when fetching by components, architectures
type RemoteRepo struct {
// Permanent internal ID
UUID string

View File

@@ -26,8 +26,14 @@ type Snapshot struct {
CreatedAt time.Time
// Source: kind + ID
SourceKind string `codec:"SourceKind" json:"-"`
SourceKind string `codec:"SourceKind"`
SourceIDs []string `codec:"SourceIDs" json:"-"`
// Sources
Snapshots []*Snapshot `codec:"-" json:",omitempty"`
RemoteRepos []*RemoteRepo `codec:"-" json:",omitempty"`
LocalRepos []*LocalRepo `codec:"-" json:",omitempty"`
Packages []string `codec:"-" json:",omitempty"`
// Description of how snapshot was created
Description string

View File

@@ -0,0 +1,42 @@
[
{
"Name": "snap2",
"SourceKind": "repo",
"Description": "Snapshot from mirror [wheezy-main]: http://mirror.yandex.ru/debian/ wheezy",
"Origin": "Debian",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap1",
"SourceKind": "repo",
"Description": "Snapshot from mirror [wheezy-contrib]: http://mirror.yandex.ru/debian/ wheezy",
"Origin": "Debian",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap3",
"SourceKind": "snapshot",
"Description": "Merged from sources: 'snap1', 'snap2'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap4",
"SourceKind": "snapshot",
"Description": "Pulled into 'snap1' with 'snap2' as source, pull request was: 'mame unrar'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap5",
"SourceKind": "local",
"Description": "Snapshot from local repo [local-repo]",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}
]

View File

@@ -0,0 +1,42 @@
[
{
"Name": "snap1",
"SourceKind": "repo",
"Description": "Snapshot from mirror [wheezy-main]: http://mirror.yandex.ru/debian/ wheezy",
"Origin": "Debian",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap2",
"SourceKind": "repo",
"Description": "Snapshot from mirror [wheezy-contrib]: http://mirror.yandex.ru/debian/ wheezy",
"Origin": "Debian",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap3",
"SourceKind": "snapshot",
"Description": "Merged from sources: 'snap1', 'snap2'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap4",
"SourceKind": "snapshot",
"Description": "Pulled into 'snap1' with 'snap2' as source, pull request was: 'mame unrar'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap5",
"SourceKind": "local",
"Description": "Snapshot from local repo [local-repo]",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}
]

View File

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

View File

@@ -0,0 +1,45 @@
{
"Name": "snap1",
"SourceKind": "repo",
"RemoteRepos": [
{
"UUID": "d49dfdff-88a2-40d5-a682-ef37e76bdc3f",
"Name": "wheezy-non-free",
"ArchiveRoot": "http://mirror.yandex.ru/debian/",
"Distribution": "wheezy",
"Components": [
"non-free"
],
"Architectures": [
"i386",
"amd64"
],
"Meta": {
"Architectures": "amd64 armel armhf i386 ia64 kfreebsd-amd64 kfreebsd-i386 mips mipsel powerpc s390 s390x sparc",
"Codename": "wheezy",
"Components": "main contrib non-free",
"Date": "Sat, 26 Apr 2014 09:27:11 UTC",
"Description": " Debian 7.5 Released 26 April 2014\n",
"Label": "Debian",
"Origin": "Debian",
"Suite": "stable",
"Version": "7.5"
},
"LastDownloadDate": "2014-06-28T01:23:26.597799094+04:00",
"ReleaseFiles": null,
"Filter": "",
"Status": 0,
"WorkerPID": 0,
"FilterWithDeps": false,
"SkipComponentCheck": false,
"SkipArchitectureCheck": false,
"DownloadSources": false,
"DownloadUdebs": false,
"DownloadInstaller": false
}
],
"Description": "Snapshot from mirror [wheezy-non-free]: http://mirror.yandex.ru/debian/ wheezy",
"Origin": "Debian",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -0,0 +1,55 @@
{
"Name": "snap1",
"SourceKind": "repo",
"RemoteRepos": [
{
"UUID": "c2e70bbb-0640-45d8-b066-58b598c93b43",
"Name": "gnuplot-maverick",
"ArchiveRoot": "http://ppa.launchpad.net/gladky-anton/gnuplot/ubuntu/",
"Distribution": "maverick",
"Components": [
"main"
],
"Architectures": [
"amd64",
"armel",
"i386",
"powerpc"
],
"Meta": {
"Architectures": "amd64 armel i386 powerpc",
"Codename": "maverick",
"Components": "main",
"Date": "Mon, 22 Oct 2012 13:19:50 UTC",
"Description": " Ubuntu Maverick 10.10\n",
"Label": "gnuplot",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"Suite": "maverick",
"Version": "10.10"
},
"LastDownloadDate": "2014-06-28T01:24:09.760026156+04:00",
"ReleaseFiles": null,
"Filter": "",
"Status": 0,
"WorkerPID": 0,
"FilterWithDeps": false,
"SkipComponentCheck": false,
"SkipArchitectureCheck": false,
"DownloadSources": false,
"DownloadUdebs": false,
"DownloadInstaller": false
}
],
"Packages": [
"gnuplot-doc_4.6.1-1~maverick2_all",
"gnuplot-nox_4.6.1-1~maverick2_amd64",
"gnuplot-nox_4.6.1-1~maverick2_i386",
"gnuplot-x11_4.6.1-1~maverick2_amd64",
"gnuplot-x11_4.6.1-1~maverick2_i386",
"gnuplot_4.6.1-1~maverick2_all"
],
"Description": "Snapshot from mirror [gnuplot-maverick]: http://ppa.launchpad.net/gladky-anton/gnuplot/ubuntu/ maverick",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -0,0 +1,16 @@
{
"Name": "snap1",
"SourceKind": "local",
"LocalRepos": [
{
"Name": "repo1",
"Comment": "Cool",
"DefaultDistribution": "wheezy",
"DefaultComponent": "contrib"
}
],
"Description": "Snapshot from local repo [repo1]: Cool",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -0,0 +1,22 @@
{
"Name": "snap1",
"SourceKind": "local",
"LocalRepos": [
{
"Name": "repo1",
"Comment": "Cool",
"DefaultDistribution": "wheezy",
"DefaultComponent": "contrib"
}
],
"Packages": [
"libboost-program-options-dev_1.49.0.1_i386",
"libboost-program-options-dev_1.62.0.1_i386",
"pyspi_0.6.1-1.3_source",
"pyspi_0.6.1-1.4_source"
],
"Description": "Snapshot from local repo [repo1]: Cool",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -0,0 +1,26 @@
{
"Name": "snap3",
"SourceKind": "snapshot",
"Snapshots": [
{
"Name": "snap1",
"SourceKind": "repo",
"Description": "Snapshot from mirror [gnuplot-maverick]: http://ppa.launchpad.net/gladky-anton/gnuplot/ubuntu/ maverick",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap2",
"SourceKind": "repo",
"Description": "Snapshot from mirror [sensu]: http://repos.sensuapp.org/apt/ sensu",
"Origin": "Sensu",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}
],
"Description": "Pulled into 'snap1' with 'snap2' as source, pull request was: 'sensu'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -0,0 +1,36 @@
{
"Name": "snap3",
"SourceKind": "snapshot",
"Snapshots": [
{
"Name": "snap1",
"SourceKind": "repo",
"Description": "Snapshot from mirror [gnuplot-maverick]: http://ppa.launchpad.net/gladky-anton/gnuplot/ubuntu/ maverick",
"Origin": "LP-PPA-gladky-anton-gnuplot",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
},
{
"Name": "snap2",
"SourceKind": "repo",
"Description": "Snapshot from mirror [sensu]: http://repos.sensuapp.org/apt/ sensu",
"Origin": "Sensu",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}
],
"Packages": [
"gnuplot-doc_4.6.1-1~maverick2_all",
"gnuplot-nox_4.6.1-1~maverick2_amd64",
"gnuplot-nox_4.6.1-1~maverick2_i386",
"gnuplot-x11_4.6.1-1~maverick2_amd64",
"gnuplot-x11_4.6.1-1~maverick2_i386",
"gnuplot_4.6.1-1~maverick2_all",
"sensu_0.12.6-5_amd64",
"sensu_0.12.6-5_i386"
],
"Description": "Pulled into 'snap1' with 'snap2' as source, pull request was: 'sensu'",
"Origin": "",
"NotAutomatic": "",
"ButAutomaticUpgrades": ""
}

View File

@@ -1,3 +1,5 @@
import re
from lib import BaseTest
@@ -92,3 +94,50 @@ class ListSnapshot7Test(BaseTest):
]
runCmd = "aptly -sort=planet snapshot list"
expectedCode = 1
class ListSnapshot8Test(BaseTest):
"""
list snapshots: json regular list
"""
fixtureDB = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror wheezy-main",
"aptly snapshot create snap2 from mirror wheezy-contrib",
"aptly snapshot merge snap3 snap1 snap2",
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
"aptly repo create local-repo",
"aptly repo add local-repo ${files}",
"aptly snapshot create snap5 from repo local-repo",
]
runCmd = "aptly -json snapshot list"
def outputMatchPrepare(self, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ListSnapshot9Test(BaseTest):
"""
list snapshots: json empty list
"""
runCmd = "aptly snapshot -json list"
class ListSnapshot10Test(BaseTest):
"""
list snapshots: json regular list sorted by time
"""
fixtureDB = True
fixtureCmds = [
"aptly snapshot create snap2 from mirror wheezy-main",
"aptly snapshot create snap1 from mirror wheezy-contrib",
"aptly snapshot merge snap3 snap1 snap2",
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
"aptly repo create local-repo",
"aptly repo add local-repo ${files}",
"aptly snapshot create snap5 from repo local-repo",
]
runCmd = "aptly -json -sort=time snapshot list"
def outputMatchPrepare(self, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)

View File

@@ -33,3 +33,91 @@ class ShowSnapshot3Test(BaseTest):
def outputMatchPrepare(_, s):
return re.sub(r"Created At: [0-9:A-Za-z -]+\n", "", s)
class ShowSnapshot4Test(BaseTest):
"""
show snapshot json: from mirror w/o packages
"""
fixtureDB = True
fixtureCmds = ["aptly snapshot create snap1 from mirror wheezy-non-free"]
runCmd = "aptly snapshot show -json snap1"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ShowSnapshot5Test(BaseTest):
"""
show snapshot json: from mirror with packages
"""
fixtureDB = True
fixtureCmds = ["aptly snapshot create snap1 from mirror gnuplot-maverick"]
runCmd = "aptly snapshot show -json -with-packages snap1"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ShowSnapshot6Test(BaseTest):
"""
show snapshot json: from local repo w/o packages
"""
fixtureDB = True
fixtureCmds = [
"aptly repo create -comment=Cool -distribution=wheezy -component=contrib repo1",
"aptly repo add repo1 ${files}",
"aptly snapshot create snap1 from repo repo1"
]
runCmd = "aptly snapshot show -json snap1"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ShowSnapshot7Test(BaseTest):
"""
show snapshot json: from local repo with packages
"""
fixtureDB = True
fixtureCmds = [
"aptly repo create -comment=Cool -distribution=wheezy -component=contrib repo1",
"aptly repo add repo1 ${files}",
"aptly snapshot create snap1 from repo repo1"
]
runCmd = "aptly snapshot show -json -with-packages snap1"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ShowSnapshot8Test(BaseTest):
"""
show snapshot json: from local repo w/o packages
"""
fixtureDB = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror gnuplot-maverick",
"aptly snapshot create snap2 from mirror sensu",
"aptly snapshot pull snap1 snap2 snap3 sensu"
]
runCmd = "aptly snapshot show -json snap3"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)
class ShowSnapshot9Test(BaseTest):
"""
show snapshot json: from local repo with packages
"""
fixtureDB = True
fixtureCmds = [
"aptly snapshot create snap1 from mirror gnuplot-maverick",
"aptly snapshot create snap2 from mirror sensu",
"aptly snapshot pull snap1 snap2 snap3 sensu"
]
runCmd = "aptly snapshot show -json -with-packages snap3"
def outputMatchPrepare(_, s):
return re.sub(r'[ ]*"CreatedAt": "[^"]+",?\n', '', s)

View File

@@ -4,9 +4,9 @@
"DefaultDistribution": "wheezy",
"DefaultComponent": "contrib",
"Packages": [
"Pi386 libboost-program-options-dev 1.49.0.1 918d2f433384e378",
"Pi386 libboost-program-options-dev 1.62.0.1 7760e62f99c551cb",
"Psource pyspi 0.6.1-1.3 3a8b37cbd9a3559e",
"Psource pyspi 0.6.1-1.4 f8f1daa806004e89"
"libboost-program-options-dev_1.49.0.1_i386",
"libboost-program-options-dev_1.62.0.1_i386",
"pyspi_0.6.1-1.3_source",
"pyspi_0.6.1-1.4_source"
]
}