Add --output option to search

In such way it can be parsed by scripts more easily.
It also disable the spinner based on loglevel

Fixes #92
This commit is contained in:
Ettore Di Giacinto
2020-04-18 23:22:00 +02:00
parent 82cd0e17b6
commit ac871cb0a3
3 changed files with 52 additions and 6 deletions

View File

@@ -19,14 +19,18 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/ghodss/yaml"
. "github.com/mudler/luet/pkg/config" . "github.com/mudler/luet/pkg/config"
installer "github.com/mudler/luet/pkg/installer" installer "github.com/mudler/luet/pkg/installer"
. "github.com/mudler/luet/pkg/logger" . "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package" pkg "github.com/mudler/luet/pkg/package"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type Results struct {
Packages []string `json:"packages"`
}
var searchCmd = &cobra.Command{ var searchCmd = &cobra.Command{
Use: "search <term>", Use: "search <term>",
Short: "Search packages", Short: "Search packages",
@@ -42,7 +46,7 @@ var searchCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var systemDB pkg.PackageDatabase var systemDB pkg.PackageDatabase
var results Results
if len(args) != 1 { if len(args) != 1 {
Fatal("Wrong number of arguments (expected 1)") Fatal("Wrong number of arguments (expected 1)")
} }
@@ -53,6 +57,10 @@ var searchCmd = &cobra.Command{
attempts := LuetCfg.Viper.GetInt("solver.max_attempts") attempts := LuetCfg.Viper.GetInt("solver.max_attempts")
searchWithLabel, _ := cmd.Flags().GetBool("by-label") searchWithLabel, _ := cmd.Flags().GetBool("by-label")
searchWithLabelMatch, _ := cmd.Flags().GetBool("by-label-regex") searchWithLabelMatch, _ := cmd.Flags().GetBool("by-label-regex")
out, _ := cmd.Flags().GetString("output")
if out != "terminal" {
LuetCfg.GetLogging().SetLogLevel("error")
}
LuetCfg.GetSolverOptions().Type = stype LuetCfg.GetSolverOptions().Type = stype
LuetCfg.GetSolverOptions().LearnRate = float32(rate) LuetCfg.GetSolverOptions().LearnRate = float32(rate)
@@ -96,6 +104,7 @@ var searchCmd = &cobra.Command{
} }
for _, m := range matches { for _, m := range matches {
Info(fmt.Sprintf(":file_folder:%s", m.Repo.GetName()), fmt.Sprintf(":package:%s", m.Package.HumanReadableString())) Info(fmt.Sprintf(":file_folder:%s", m.Repo.GetName()), fmt.Sprintf(":package:%s", m.Package.HumanReadableString()))
results.Packages = append(results.Packages, m.Package.HumanReadableString())
} }
} else { } else {
@@ -123,8 +132,25 @@ var searchCmd = &cobra.Command{
for _, pack := range iMatches { for _, pack := range iMatches {
Info(fmt.Sprintf(":package:%s", pack.HumanReadableString())) Info(fmt.Sprintf(":package:%s", pack.HumanReadableString()))
results.Packages = append(results.Packages, pack.HumanReadableString())
} }
}
y, err := yaml.Marshal(results)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
switch out {
case "yaml":
fmt.Println(string(y))
case "json":
j2, err := yaml.YAMLToJSON(y)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(j2))
} }
}, },
@@ -139,6 +165,7 @@ func init() {
searchCmd.Flags().String("system-target", path, "System rootpath") searchCmd.Flags().String("system-target", path, "System rootpath")
searchCmd.Flags().Bool("installed", false, "Search between system packages") searchCmd.Flags().Bool("installed", false, "Search between system packages")
searchCmd.Flags().String("solver-type", "", "Solver strategy ( Defaults none, available: "+AvailableResolvers+" )") searchCmd.Flags().String("solver-type", "", "Solver strategy ( Defaults none, available: "+AvailableResolvers+" )")
searchCmd.Flags().StringP("output", "o", "terminal", "Output format ( Defaults: terminal, available: json,yaml )")
searchCmd.Flags().Float32("solver-rate", 0.7, "Solver learning rate") searchCmd.Flags().Float32("solver-rate", 0.7, "Solver learning rate")
searchCmd.Flags().Float32("solver-discount", 1.0, "Solver discount rate") searchCmd.Flags().Float32("solver-discount", 1.0, "Solver discount rate")
searchCmd.Flags().Int("solver-attempts", 9000, "Solver maximum attempts") searchCmd.Flags().Int("solver-attempts", 9000, "Solver maximum attempts")

View File

@@ -307,6 +307,10 @@ func (c *LuetGeneralConfig) GetSpinnerMs() time.Duration {
return duration return duration
} }
func (c *LuetLoggingConfig) SetLogLevel(s string) {
c.Level = s
}
func (c *LuetLoggingConfig) String() string { func (c *LuetLoggingConfig) String() string {
ans := fmt.Sprintf(` ans := fmt.Sprintf(`
logging: logging:

View File

@@ -53,12 +53,20 @@ func ZapLogger() error {
} }
func Spinner(i int) { func Spinner(i int) {
var confLevel int
if LuetCfg.GetGeneral().Debug {
confLevel = 3
} else {
confLevel = level2Number(LuetCfg.GetLogging().Level)
}
if 2 > confLevel {
return
}
if i > 43 { if i > 43 {
i = 43 i = 43
} }
if !LuetCfg.GetGeneral().Debug && !s.Active() { if !s.Active() {
// s.UpdateCharSet(spinner.CharSets[i]) // s.UpdateCharSet(spinner.CharSets[i])
s.Start() // Start the spinner s.Start() // Start the spinner
} }
@@ -79,9 +87,16 @@ func SpinnerText(suffix, prefix string) {
} }
func SpinnerStop() { func SpinnerStop() {
if !LuetCfg.GetGeneral().Debug { var confLevel int
s.Stop() if LuetCfg.GetGeneral().Debug {
confLevel = 3
} else {
confLevel = level2Number(LuetCfg.GetLogging().Level)
} }
if 2 > confLevel {
return
}
s.Stop()
} }
func level2Number(level string) int { func level2Number(level string) int {