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
-47
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))
+48 -1
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
+45 -1
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
}
-2
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))