Add -json flag to mirror list|show

Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
Joshua Colson
2021-09-21 14:37:13 -07:00
committed by Lorenzo Bolla
parent d582f9bab2
commit 129eb8644d
17 changed files with 816 additions and 12 deletions
+43 -2
View File
@@ -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