mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-05 05:20:34 +00:00
Add -json flag to mirror list|show
Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
committed by
Lorenzo Bolla
parent
d582f9bab2
commit
129eb8644d
+43
-2
@@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
@@ -9,12 +10,23 @@ import (
|
||||
)
|
||||
|
||||
func aptlyMirrorList(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 aptlyMirrorListJson(cmd, args)
|
||||
}
|
||||
|
||||
return aptlyMirrorListTxt(cmd, args)
|
||||
}
|
||||
|
||||
func aptlyMirrorListTxt(cmd *commander.Command, args []string) error {
|
||||
var err error
|
||||
|
||||
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
||||
|
||||
repos := make([]string, context.CollectionFactory().RemoteRepoCollection().Len())
|
||||
@@ -31,7 +43,9 @@ func aptlyMirrorList(cmd *commander.Command, args []string) error {
|
||||
|
||||
context.CloseDatabase()
|
||||
|
||||
sort.Strings(repos)
|
||||
if len(repos) > 0 {
|
||||
sort.Strings(repos)
|
||||
}
|
||||
|
||||
if raw {
|
||||
for _, repo := range repos {
|
||||
@@ -52,6 +66,32 @@ func aptlyMirrorList(cmd *commander.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func aptlyMirrorListJson(cmd *commander.Command, args []string) error {
|
||||
var err error
|
||||
|
||||
repos := make([]*deb.RemoteRepo, context.CollectionFactory().RemoteRepoCollection().Len())
|
||||
i := 0
|
||||
context.CollectionFactory().RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error {
|
||||
repos[i] = repo
|
||||
i++
|
||||
return nil
|
||||
})
|
||||
|
||||
context.CloseDatabase()
|
||||
|
||||
sort.Slice(repos, func(i, j int) bool {
|
||||
return repos[i].Name < repos[j].Name
|
||||
})
|
||||
|
||||
if output, e := json.MarshalIndent(repos, "", " "); e == nil {
|
||||
fmt.Println(string(output))
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func makeCmdMirrorList() *commander.Command {
|
||||
cmd := &commander.Command{
|
||||
Run: aptlyMirrorList,
|
||||
@@ -66,6 +106,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
|
||||
|
||||
+103
-1
@@ -1,7 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/aptly-dev/aptly/deb"
|
||||
@@ -11,12 +13,23 @@ import (
|
||||
)
|
||||
|
||||
func aptlyMirrorShow(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 aptlyMirrorShowJson(cmd, args)
|
||||
}
|
||||
|
||||
return aptlyMirrorShowTxt(cmd, args)
|
||||
}
|
||||
|
||||
func aptlyMirrorShowTxt(cmd *commander.Command, args []string) error {
|
||||
var err error
|
||||
|
||||
name := args[0]
|
||||
|
||||
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(name)
|
||||
@@ -79,6 +92,94 @@ func aptlyMirrorShow(cmd *commander.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func aptlyMirrorShowJson(cmd *commander.Command, args []string) error {
|
||||
var err error
|
||||
|
||||
name := args[0]
|
||||
|
||||
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
|
||||
|
||||
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to show: %s", err)
|
||||
}
|
||||
|
||||
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo)
|
||||
if err != nil {
|
||||
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 {
|
||||
var list *deb.PackageList
|
||||
list, err = deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), context.Progress())
|
||||
|
||||
list.PrepareIndex()
|
||||
list.ForEachIndexed(func(p *deb.Package) error {
|
||||
repo.Packages = append(repo.Packages, p.GetFullName())
|
||||
return nil
|
||||
})
|
||||
|
||||
sort.Strings(repo.Packages)
|
||||
}
|
||||
}
|
||||
|
||||
// merge the repo object with the package list
|
||||
var output []byte
|
||||
if output, err = json.MarshalIndent(repo, "", " "); err == nil {
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func makeCmdMirrorShow() *commander.Command {
|
||||
cmd := &commander.Command{
|
||||
Run: aptlyMirrorShow,
|
||||
@@ -94,6 +195,7 @@ Example:
|
||||
Flag: *flag.NewFlagSet("aptly-mirror-show", flag.ExitOnError),
|
||||
}
|
||||
|
||||
cmd.Flag.Bool("json", false, "display record in JSON format")
|
||||
cmd.Flag.Bool("with-packages", false, "show detailed list of packages and versions stored in the mirror")
|
||||
|
||||
return cmd
|
||||
|
||||
@@ -125,7 +125,6 @@ func aptlySnapshotShowJson(cmd *commander.Command, args []string) error {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
source.ReleaseFiles = nil // do not include the release file info
|
||||
snapshot.RemoteRepos = append(snapshot.RemoteRepos, source)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user