mirror of
https://github.com/mudler/luet.git
synced 2025-09-26 07:10:25 +00:00
Add 'tree pkglist' command cli
This commit is contained in:
36
cmd/tree.go
Normal file
36
cmd/tree.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
// Daniele Rondina <geaaru@sabayonlinux.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/mudler/luet/cmd/tree"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var treeGroupCmd = &cobra.Command{
|
||||||
|
Use: "tree [command] [OPTIONS]",
|
||||||
|
Short: "Tree operations",
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(treeGroupCmd)
|
||||||
|
|
||||||
|
treeGroupCmd.AddCommand(
|
||||||
|
NewTreePkglistCommand(),
|
||||||
|
)
|
||||||
|
}
|
126
cmd/tree/pkglist.go
Normal file
126
cmd/tree/pkglist.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
// Daniele Rondina <geaaru@sabayonlinux.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package cmd_tree
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
//. "github.com/mudler/luet/pkg/config"
|
||||||
|
. "github.com/mudler/luet/pkg/logger"
|
||||||
|
pkg "github.com/mudler/luet/pkg/package"
|
||||||
|
tree "github.com/mudler/luet/pkg/tree"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func pkgDetail(pkg pkg.Package) string {
|
||||||
|
ans := fmt.Sprintf(`
|
||||||
|
@@ Package: %s/%s-%s
|
||||||
|
Description: %s
|
||||||
|
License: %s`,
|
||||||
|
pkg.GetCategory(), pkg.GetName(), pkg.GetVersion(),
|
||||||
|
pkg.GetDescription(), pkg.GetLicense())
|
||||||
|
|
||||||
|
for idx, u := range pkg.GetURI() {
|
||||||
|
if idx == 0 {
|
||||||
|
ans += fmt.Sprintf(" URLs: %s", u)
|
||||||
|
} else {
|
||||||
|
ans += fmt.Sprintf(" %s", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTreePkglistCommand() *cobra.Command {
|
||||||
|
var excludes []string
|
||||||
|
|
||||||
|
var ans = &cobra.Command{
|
||||||
|
Use: "pkglist [OPTIONS]",
|
||||||
|
Short: "List of the packages found in tree.",
|
||||||
|
Args: cobra.OnlyValidArgs,
|
||||||
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
t, _ := cmd.Flags().GetString("tree")
|
||||||
|
if t == "" {
|
||||||
|
Fatal("Mandatory tree param missing.")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
|
treePath, _ := cmd.Flags().GetString("tree")
|
||||||
|
verbose, _ := cmd.Flags().GetBool("verbose")
|
||||||
|
full, _ := cmd.Flags().GetBool("full")
|
||||||
|
reciper := tree.NewInstallerRecipe(pkg.NewInMemoryDatabase(false))
|
||||||
|
err := reciper.Load(treePath)
|
||||||
|
if err != nil {
|
||||||
|
Fatal("Error on load tree ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
regExcludes := make([]*regexp.Regexp, len(excludes))
|
||||||
|
if len(excludes) > 0 {
|
||||||
|
for _, excreg := range excludes {
|
||||||
|
re := regexp.MustCompile(excreg)
|
||||||
|
if re == nil {
|
||||||
|
Fatal("Invalid regex " + excreg + "!")
|
||||||
|
}
|
||||||
|
regExcludes = append(regExcludes, re)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plist := make([]string, 0)
|
||||||
|
for _, p := range reciper.GetDatabase().World() {
|
||||||
|
pkgstr := ""
|
||||||
|
addPkg := true
|
||||||
|
if full {
|
||||||
|
pkgstr = pkgDetail(p)
|
||||||
|
} else if verbose {
|
||||||
|
pkgstr = fmt.Sprintf("%s/%s-%s", p.GetCategory(), p.GetName(), p.GetVersion())
|
||||||
|
} else {
|
||||||
|
pkgstr = fmt.Sprintf("%s/%s", p.GetCategory(), p.GetName())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(excludes) > 0 {
|
||||||
|
for _, rgx := range regExcludes {
|
||||||
|
if rgx.MatchString(pkgstr) {
|
||||||
|
addPkg = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if addPkg {
|
||||||
|
plist = append(plist, pkgstr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(plist)
|
||||||
|
for _, p := range plist {
|
||||||
|
fmt.Println(p)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ans.Flags().BoolP("verbose", "v", false, "Add package version")
|
||||||
|
ans.Flags().BoolP("full", "f", false, "Show package detail")
|
||||||
|
ans.Flags().StringP("tree", "t", "", "Path of the tree to use.")
|
||||||
|
ans.Flags().StringSliceVarP(&excludes, "exclude", "e", []string{},
|
||||||
|
"Exclude matched packages from list. (Use string as regex).")
|
||||||
|
|
||||||
|
return ans
|
||||||
|
}
|
Reference in New Issue
Block a user