From ac871cb0a387e1d4f8c0fbbac599aaea7529734e Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 18 Apr 2020 23:22:00 +0200 Subject: [PATCH] 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 --- cmd/search.go | 31 +++++++++++++++++++++++++++++-- pkg/config/config.go | 4 ++++ pkg/logger/logger.go | 23 +++++++++++++++++++---- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/cmd/search.go b/cmd/search.go index 6a8835c5..d2bf5d1c 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -19,14 +19,18 @@ import ( "os" "path/filepath" + "github.com/ghodss/yaml" . "github.com/mudler/luet/pkg/config" installer "github.com/mudler/luet/pkg/installer" . "github.com/mudler/luet/pkg/logger" pkg "github.com/mudler/luet/pkg/package" - "github.com/spf13/cobra" ) +type Results struct { + Packages []string `json:"packages"` +} + var searchCmd = &cobra.Command{ Use: "search ", Short: "Search packages", @@ -42,7 +46,7 @@ var searchCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { var systemDB pkg.PackageDatabase - + var results Results if len(args) != 1 { Fatal("Wrong number of arguments (expected 1)") } @@ -53,6 +57,10 @@ var searchCmd = &cobra.Command{ attempts := LuetCfg.Viper.GetInt("solver.max_attempts") searchWithLabel, _ := cmd.Flags().GetBool("by-label") 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().LearnRate = float32(rate) @@ -96,6 +104,7 @@ var searchCmd = &cobra.Command{ } for _, m := range matches { 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 { @@ -123,8 +132,25 @@ var searchCmd = &cobra.Command{ for _, pack := range iMatches { 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().Bool("installed", false, "Search between system packages") 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-discount", 1.0, "Solver discount rate") searchCmd.Flags().Int("solver-attempts", 9000, "Solver maximum attempts") diff --git a/pkg/config/config.go b/pkg/config/config.go index df8cac6b..eccc911b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -307,6 +307,10 @@ func (c *LuetGeneralConfig) GetSpinnerMs() time.Duration { return duration } +func (c *LuetLoggingConfig) SetLogLevel(s string) { + c.Level = s +} + func (c *LuetLoggingConfig) String() string { ans := fmt.Sprintf(` logging: diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 9d355b37..b2a31c83 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -53,12 +53,20 @@ func ZapLogger() error { } 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 { i = 43 } - if !LuetCfg.GetGeneral().Debug && !s.Active() { + if !s.Active() { // s.UpdateCharSet(spinner.CharSets[i]) s.Start() // Start the spinner } @@ -79,9 +87,16 @@ func SpinnerText(suffix, prefix string) { } func SpinnerStop() { - if !LuetCfg.GetGeneral().Debug { - s.Stop() + var confLevel int + if LuetCfg.GetGeneral().Debug { + confLevel = 3 + } else { + confLevel = level2Number(LuetCfg.GetLogging().Level) } + if 2 > confLevel { + return + } + s.Stop() } func level2Number(level string) int {