mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-06 05:30:57 +00:00
Add -json flag to snapshot show|list
Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
committed by
Lorenzo Bolla
parent
f9c0d99790
commit
0f1575d5af
+5
-5
@@ -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
|
||||
|
||||
+1
-1
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+37
-1
@@ -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")
|
||||
|
||||
|
||||
+88
-2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user