Refactor repo list into json and txt output

Signed-off-by: Joshua Colson <joshua.colson@gmail.com>
This commit is contained in:
Joshua Colson
2021-09-20 12:33:21 -07:00
committed by Lorenzo Bolla
parent 1f56fb86e3
commit f9c0d99790

View File

@@ -10,17 +10,26 @@ import (
)
func aptlyRepoList(cmd *commander.Command, args []string) error {
var err error
if len(args) != 0 {
cmd.Usage()
return commander.ErrCommandError
}
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
jsonFlag := cmd.Flag.Lookup("json").Value.Get().(bool)
if jsonFlag {
return aptlyRepoListJson(cmd, args)
}
return aptlyRepoListTxt(cmd, args)
}
func aptlyRepoListTxt(cmd *commander.Command, args []string) error {
var err error
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
repos := make([]string, context.CollectionFactory().LocalRepoCollection().Len())
jsonRepos := make([]*deb.LocalRepo, context.CollectionFactory().LocalRepoCollection().Len())
i := 0
context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
if raw {
@@ -31,11 +40,7 @@ func aptlyRepoList(cmd *commander.Command, args []string) error {
return e
}
if jsonFlag {
jsonRepos[i] = repo
} else {
repos[i] = fmt.Sprintf(" * %s (packages: %d)", repo.String(), repo.NumPackages())
}
repos[i] = fmt.Sprintf(" * %s (packages: %d)", repo.String(), repo.NumPackages())
}
i++
return nil
@@ -48,15 +53,6 @@ func aptlyRepoList(cmd *commander.Command, args []string) error {
for _, repo := range repos {
fmt.Printf("%s\n", repo)
}
} else if jsonFlag {
sort.Slice(jsonRepos, func(i, j int) bool {
return jsonRepos[i].Name < jsonRepos[j].Name
})
if output, e := json.MarshalIndent(jsonRepos, "", " "); e == nil {
fmt.Println(string(output))
} else {
err = e
}
} else {
sort.Strings(repos)
if len(repos) > 0 {
@@ -74,6 +70,36 @@ func aptlyRepoList(cmd *commander.Command, args []string) error {
return err
}
func aptlyRepoListJson(cmd *commander.Command, args []string) error {
var err error
jsonRepos := make([]*deb.LocalRepo, context.CollectionFactory().LocalRepoCollection().Len())
i := 0
context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
e := context.CollectionFactory().LocalRepoCollection().LoadComplete(repo)
if e != nil {
return e
}
jsonRepos[i] = repo
i++
return nil
})
context.CloseDatabase()
sort.Slice(jsonRepos, func(i, j int) bool {
return jsonRepos[i].Name < jsonRepos[j].Name
})
if output, e := json.MarshalIndent(jsonRepos, "", " "); e == nil {
fmt.Println(string(output))
} else {
err = e
}
return err
}
func makeCmdRepoList() *commander.Command {
cmd := &commander.Command{
Run: aptlyRepoList,