Enhance output with consistent usage of logger

This commit is contained in:
Ettore Di Giacinto 2019-11-02 10:26:28 +01:00
parent 2b9e512272
commit c04e6496fb
No known key found for this signature in database
GPG Key ID: 1ADA699B145A2D1C
5 changed files with 30 additions and 27 deletions

View File

@ -15,10 +15,9 @@
package cmd package cmd
import ( import (
"fmt"
"log"
"os" "os"
. "github.com/mudler/luet/pkg/logger"
tree "github.com/mudler/luet/pkg/tree" tree "github.com/mudler/luet/pkg/tree"
"github.com/mudler/luet/pkg/tree/builder/gentoo" "github.com/mudler/luet/pkg/tree/builder/gentoo"
@ -36,12 +35,13 @@ var convertCmd = &cobra.Command{
c := viper.GetInt("concurrency") c := viper.GetInt("concurrency")
if len(args) != 2 { if len(args) != 2 {
log.Fatalln("Incorrect number of arguments") Error("Incorrect number of arguments")
os.Exit(1)
} }
input := args[0] input := args[0]
output := args[1] output := args[1]
fmt.Println("Converting trees from" + input + " [" + t + "]") Info("Converting trees from" + input + " [" + t + "]")
var builder tree.Parser var builder tree.Parser
switch t { switch t {
@ -53,20 +53,20 @@ var convertCmd = &cobra.Command{
packageTree, err := builder.Generate(input) packageTree, err := builder.Generate(input)
if err != nil { if err != nil {
fmt.Println("Error: " + err.Error()) Error("Error: " + err.Error())
os.Exit(1) os.Exit(1)
} }
defer packageTree.GetPackageSet().Clean() defer packageTree.GetPackageSet().Clean()
fmt.Println("Tree generated") Info("Tree generated")
generalRecipe := tree.NewGeneralRecipe() generalRecipe := tree.NewGeneralRecipe()
fmt.Println("Saving generated tree to " + output) Info("Saving generated tree to " + output)
generalRecipe.WithTree(packageTree) generalRecipe.WithTree(packageTree)
err = generalRecipe.Save(output) err = generalRecipe.Save(output)
if err != nil { if err != nil {
fmt.Println("Error: " + err.Error()) Error("Error: " + err.Error())
os.Exit(1) os.Exit(1)
} }
}, },

View File

@ -16,12 +16,12 @@
package cmd package cmd
import ( import (
"fmt"
"log"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
. "github.com/mudler/luet/pkg/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -40,7 +40,7 @@ var RootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) Error(err)
os.Exit(-1) os.Exit(-1)
} }
} }
@ -55,12 +55,13 @@ func init() {
func initConfig() { func initConfig() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil { if err != nil {
log.Fatal(err) Error(err)
os.Exit(1)
} }
viper.SetConfigName(".luet") // name of config file (without extension) viper.SetConfigName(".luet") // name of config file (without extension)
if cfgFile != "" { // enable ability to specify config file via flag if cfgFile != "" { // enable ability to specify config file via flag
fmt.Println(">>> cfgFile: ", cfgFile) Info(">>> cfgFile: ", cfgFile)
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
configDir := path.Dir(cfgFile) configDir := path.Dir(cfgFile)
if configDir != "." && configDir != dir { if configDir != "." && configDir != dir {
@ -75,9 +76,9 @@ func initConfig() {
// If a config file is found, read it in. // If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed()) Info("Using config file:", viper.ConfigFileUsed())
} else { } else {
fmt.Println(err) Error(err)
} }
} }

View File

@ -12,7 +12,6 @@ import (
var s *spinner.Spinner var s *spinner.Spinner
func Spinner(i int) { func Spinner(i int) {
if i > 43 { if i > 43 {
i = 43 i = 43
} }
@ -34,46 +33,46 @@ func SpinnerStop() {
func Warning(msg ...interface{}) { func Warning(msg ...interface{}) {
if s != nil { if s != nil {
SpinnerText(Sprintf(msg), Bold(Yellow("Warn")).BgBlack().String()) SpinnerText(Sprintf(msg), Bold(Yellow("Warn")).BgBlack().String())
return // return
} }
cmd := []interface{}{Bold(Yellow("Warn")).BgBlack().String()} cmd := []interface{}{Bold(Yellow("Warn")).BgBlack().String()}
for _, f := range msg { for _, f := range msg {
cmd = append(cmd, f) cmd = append(cmd, f)
} }
fmt.Println(cmd) fmt.Println(cmd...)
} }
func Debug(msg ...interface{}) { func Debug(msg ...interface{}) {
if s != nil { if s != nil {
SpinnerText(Sprintf(msg), Bold(White("Debug")).BgBlack().String()) SpinnerText(Sprintf(msg), Bold(White("Debug")).BgBlack().String())
return // return
} }
cmd := []interface{}{Bold(White("Debug")).String()} cmd := []interface{}{Bold(White("Debug")).String()}
for _, f := range msg { for _, f := range msg {
cmd = append(cmd, f) cmd = append(cmd, f)
} }
fmt.Println(cmd) fmt.Println(cmd...)
} }
func Info(msg ...interface{}) { func Info(msg ...interface{}) {
if s != nil { if s != nil {
SpinnerText(Sprintf(msg), Bold(Blue("Info")).BgBlack().String()) SpinnerText(Sprintf(msg), Bold(Blue("Info")).BgBlack().String())
return // return
} }
cmd := []interface{}{Bold(Green("Info")).String()} cmd := []interface{}{Bold(Green("Info")).String()}
for _, f := range msg { for _, f := range msg {
cmd = append(cmd, f) cmd = append(cmd, f)
} }
fmt.Println(cmd) fmt.Println(cmd...)
} }
func Error(msg ...interface{}) { func Error(msg ...interface{}) {
if s != nil { if s != nil {
SpinnerText(Sprintf(msg), Bold(Red("Error")).BgBlack().String()) SpinnerText(Sprintf(msg), Bold(Red("Error")).BgBlack().String())
return // return
} }
cmd := []interface{}{Bold(Red("Error")).String()} cmd := []interface{}{Bold(Red("Error")).String()}
for _, f := range msg { for _, f := range msg {
cmd = append(cmd, f) cmd = append(cmd, f)
} }
fmt.Println(cmd) fmt.Println(cmd...)
} }

View File

@ -66,7 +66,6 @@ func (gb *GentooBuilder) scanEbuild(path string, t pkg.Tree) error {
return err return err
} }
} }
} }
return nil return nil
} }
@ -75,7 +74,7 @@ func (gb *GentooBuilder) worker(i int, wg *sync.WaitGroup, s chan string, t pkg.
defer wg.Done() defer wg.Done()
for path := range s { for path := range s {
SpinnerText(" "+path, "Scanning ") Info("Scanning", path)
err := gb.scanEbuild(path, t) err := gb.scanEbuild(path, t)
if err != nil { if err != nil {
Error("scanning ebuild: " + path) Error("scanning ebuild: " + path)
@ -121,7 +120,9 @@ func (gb *GentooBuilder) Generate(dir string) (pkg.Tree, error) {
} }
close(toScan) close(toScan)
Debug("Waiting for goroutines to finish")
wg.Wait() wg.Wait()
Info("Scan finished")
Info("Resolving deps") Info("Resolving deps")
return tree, tree.ResolveDeps(gb.Concurrency) return tree, tree.ResolveDeps(gb.Concurrency)
} }

View File

@ -27,6 +27,8 @@ import (
"regexp" "regexp"
"strings" "strings"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package" pkg "github.com/mudler/luet/pkg/package"
"mvdan.cc/sh/expand" "mvdan.cc/sh/expand"
"mvdan.cc/sh/shell" "mvdan.cc/sh/shell"
@ -55,7 +57,7 @@ func SourceFile(ctx context.Context, path string) (map[string]expand.Variable, e
func (ep *SimpleEbuildParser) ScanEbuild(path string, tree pkg.Tree) ([]pkg.Package, error) { func (ep *SimpleEbuildParser) ScanEbuild(path string, tree pkg.Tree) ([]pkg.Package, error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Println("Recovered in scanebuild", r) Error(r)
} }
}() }()
file := filepath.Base(path) file := filepath.Base(path)