From 899ed92ebcf59d3e2a09d8a93edb5801ae828ac1 Mon Sep 17 00:00:00 2001 From: Joshua Colson Date: Tue, 21 Sep 2021 18:46:59 -0700 Subject: [PATCH] Add -json flag to publish list|show Signed-off-by: Joshua Colson --- cmd/mirror_show.go | 47 ------------ cmd/publish_list.go | 49 +++++++++++- cmd/publish_show.go | 46 ++++++++++- cmd/snapshot_show.go | 2 - deb/publish.go | 14 +++- system/t06_publish/PublishList4Test_gold | 1 + system/t06_publish/PublishList5Test_gold | 97 ++++++++++++++++++++++++ system/t06_publish/PublishShow3Test_gold | 24 ++++++ system/t06_publish/PublishShow4Test_gold | 24 ++++++ system/t06_publish/list.py | 24 ++++++ system/t06_publish/show.py | 26 +++++++ 11 files changed, 302 insertions(+), 52 deletions(-) create mode 100644 system/t06_publish/PublishList4Test_gold create mode 100644 system/t06_publish/PublishList5Test_gold create mode 100644 system/t06_publish/PublishShow3Test_gold create mode 100644 system/t06_publish/PublishShow4Test_gold diff --git a/cmd/mirror_show.go b/cmd/mirror_show.go index a1b466a7..682a6acc 100644 --- a/cmd/mirror_show.go +++ b/cmd/mirror_show.go @@ -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)) diff --git a/cmd/publish_list.go b/cmd/publish_list.go index dddf3b9f..ebfc424e 100644 --- a/cmd/publish_list.go +++ b/cmd/publish_list.go @@ -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 diff --git a/cmd/publish_show.go b/cmd/publish_show.go index cb4c959e..00b34c53 100644 --- a/cmd/publish_show.go +++ b/cmd/publish_show.go @@ -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 } diff --git a/cmd/snapshot_show.go b/cmd/snapshot_show.go index d2a7a8ba..323657fa 100644 --- a/cmd/snapshot_show.go +++ b/cmd/snapshot_show.go @@ -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)) diff --git a/deb/publish.go b/deb/publish.go index 41f66112..1319baae 100644 --- a/deb/publish.go +++ b/deb/publish.go @@ -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 == "" { diff --git a/system/t06_publish/PublishList4Test_gold b/system/t06_publish/PublishList4Test_gold new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/system/t06_publish/PublishList4Test_gold @@ -0,0 +1 @@ +[] diff --git a/system/t06_publish/PublishList5Test_gold b/system/t06_publish/PublishList5Test_gold new file mode 100644 index 00000000..61f468b9 --- /dev/null +++ b/system/t06_publish/PublishList5Test_gold @@ -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": "" + } +] diff --git a/system/t06_publish/PublishShow3Test_gold b/system/t06_publish/PublishShow3Test_gold new file mode 100644 index 00000000..b68fbef1 --- /dev/null +++ b/system/t06_publish/PublishShow3Test_gold @@ -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": "" +} diff --git a/system/t06_publish/PublishShow4Test_gold b/system/t06_publish/PublishShow4Test_gold new file mode 100644 index 00000000..24fe5949 --- /dev/null +++ b/system/t06_publish/PublishShow4Test_gold @@ -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": "" +} diff --git a/system/t06_publish/list.py b/system/t06_publish/list.py index c44a8a1d..d5ad7c1f 100644 --- a/system/t06_publish/list.py +++ b/system/t06_publish/list.py @@ -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" diff --git a/system/t06_publish/show.py b/system/t06_publish/show.py index 5149142c..7616b78e 100644 --- a/system/t06_publish/show.py +++ b/system/t06_publish/show.py @@ -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"