Skip to content

Commit

Permalink
🐛 fixing logs with new command format, adding new format to dep comma…
Browse files Browse the repository at this point in the history
…nd (#523)

fixes #453

Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley authored Mar 5, 2024
1 parent 78347e0 commit d483bcb
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 148 deletions.
27 changes: 14 additions & 13 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

logrusr "github.com/bombsimon/logrusr/v3"
"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/engine"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
Expand Down Expand Up @@ -43,22 +44,15 @@ var (
)

func AnalysisCmd() *cobra.Command {

logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
// need to do research on mapping in logrusr to level here TODO
logrusLog.SetLevel(logrus.Level(logLevel))
log := logrusr.New(logrusLog)

logrusErrLog := logrus.New()
logrusErrLog.SetOutput(os.Stderr)
errLog := logrusr.New(logrusErrLog)
var errLog logr.Logger

rootCmd := &cobra.Command{
Use: "analyze",
Short: "Tool for working with analyzer-lsp",
Use: "konveyor-analyzer",
Short: "Tool for working with konveyor-analyzer",
PreRunE: func(c *cobra.Command, args []string) error {
logrusErrLog := logrus.New()
logrusErrLog.SetOutput(os.Stderr)
errLog = logrusr.New(logrusErrLog)
err := validateFlags()
if err != nil {
errLog.Error(err, "failed to validate flags")
Expand All @@ -70,6 +64,13 @@ func AnalysisCmd() *cobra.Command {
},
Run: func(c *cobra.Command, args []string) {

logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
// need to do research on mapping in logrusr to level here TODO
logrusLog.SetLevel(logrus.Level(logLevel))
log := logrusr.New(logrusLog)

// This will globally prevent the yaml library from auto-wrapping lines at 80 characters
yaml.FutureLineWrap()

Expand Down
284 changes: 149 additions & 135 deletions cmd/dep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/bombsimon/logrusr/v3"
"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/analyzer-lsp/provider"
Expand All @@ -23,173 +24,186 @@ var (
treeOutput bool
outputFile string
depLabelSelector string

rootCmd = &cobra.Command{
Use: "analyze",
Short: "Tool for working with analyzer-lsp",
Run: func(c *cobra.Command, args []string) {},
}
)

func init() {
rootCmd.Flags().StringVar(&providerSettings, "provider-settings", "provider_settings.json", "path to the provider settings")
rootCmd.Flags().BoolVar(&treeOutput, "tree", false, "output dependencies as a tree")
rootCmd.Flags().StringVar(&outputFile, "output-file", "output.yaml", "path to output file")
rootCmd.Flags().StringVar(&depLabelSelector, "dep-label-selector", "", "an expression to select dependencies based on labels provided by the provider")
}

func main() {
if err := rootCmd.Execute(); err != nil {
println(err.Error())
}

logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
log := logrusr.New(logrusLog)

logrusErrLog := logrus.New()
logrusErrLog.SetOutput(os.Stderr)
errLog := logrusr.New(logrusErrLog)

err := validateFlags()
if err != nil {
errLog.Error(err, "failed to validate input flags")
os.Exit(1)
}
func DependencyCmd() *cobra.Command {

var errLog logr.Logger
rootCmd := &cobra.Command{
Use: "konveyor-analyzer-dep",
Short: "tool for retrieving dependencies from konveyor-analyzer dependecy providers",
PreRunE: func(c *cobra.Command, args []string) error {
logrusErrLog := logrus.New()
logrusErrLog.SetOutput(os.Stderr)
errLog = logrusr.New(logrusErrLog)
err := validateFlags()
if err != nil {
errLog.Error(err, "failed to validate flags")

var labelSelector *labels.LabelSelector[*konveyor.Dep]
if depLabelSelector != "" {
labelSelector, err = labels.NewLabelSelector[*konveyor.Dep](depLabelSelector, nil)
if err != nil {
errLog.Error(err, "invalid label selector")
os.Exit(1)
}
}
return err
}

ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return nil
},
Run: func(c *cobra.Command, args []string) {
logrusLog := logrus.New()
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
log := logrusr.New(logrusLog)
var labelSelector *labels.LabelSelector[*konveyor.Dep]
var err error
if depLabelSelector != "" {
labelSelector, err = labels.NewLabelSelector[*konveyor.Dep](depLabelSelector, nil)
if err != nil {
errLog.Error(err, "invalid label selector")
os.Exit(1)
}
}

providers := map[string]provider.Client{}
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

// Get the configs
configs, err := provider.GetConfig(providerSettings)
if err != nil {
errLog.Error(err, "unable to get configuration")
os.Exit(1)
}
providers := map[string]provider.Client{}

for _, config := range configs {
prov, err := lib.GetProviderClient(config, log)
if err != nil {
errLog.Error(err, "unable to create provider client")
os.Exit(1)
}
if s, ok := prov.(provider.Startable); ok {
if err := s.Start(ctx); err != nil {
errLog.Error(err, "unable to create provider client")
// Get the configs
configs, err := provider.GetConfig(providerSettings)
if err != nil {
errLog.Error(err, "unable to get configuration")
os.Exit(1)
}
}

time.Sleep(5 * time.Second)

err = prov.ProviderInit(ctx)
b, _ := json.Marshal(config)
if err != nil {
errLog.Error(err, "unable to init the providers", "provider", config.Name, "the-error-is", err, "config", string(b))
os.Exit(1)
} else {
log.Info("init'd provider", "provider", config.Name, "config", string(b))
}
providers[config.Name] = prov
for _, config := range configs {
prov, err := lib.GetProviderClient(config, log)
if err != nil {
errLog.Error(err, "unable to create provider client")
os.Exit(1)
}
if s, ok := prov.(provider.Startable); ok {
if err := s.Start(ctx); err != nil {
errLog.Error(err, "unable to create provider client")
os.Exit(1)
}
}

}
time.Sleep(5 * time.Second)

var depsFlat []konveyor.DepsFlatItem
var depsTree []konveyor.DepsTreeItem
for name, prov := range providers {
if !provider.HasCapability(prov.Capabilities(), "dependency") {
log.Info("provider does not have dependency capability", "provider", name)
continue
}
err = prov.ProviderInit(ctx)
b, _ := json.Marshal(config)
if err != nil {
errLog.Error(err, "unable to init the providers", "provider", config.Name, "the-error-is", err, "config", string(b))
os.Exit(1)
} else {
log.Info("init'd provider", "provider", config.Name, "config", string(b))
}
providers[config.Name] = prov

if treeOutput {
deps, err := prov.GetDependenciesDAG(ctx)
if err != nil {
errLog.Error(err, "failed to get list of dependencies for provider", "provider", name)
continue
}
for u, ds := range deps {
depsTree = append(depsTree, konveyor.DepsTreeItem{
FileURI: string(u),
Provider: name,
Dependencies: ds,
})
}
} else {
deps, err := prov.GetDependencies(ctx)
if err != nil {
errLog.Error(err, "failed to get list of dependencies for provider", "provider", name)
continue
}
for u, ds := range deps {
newDeps := ds
if labelSelector != nil {
newDeps, err = labelSelector.MatchList(ds)

var depsFlat []konveyor.DepsFlatItem
var depsTree []konveyor.DepsTreeItem
for name, prov := range providers {
if !provider.HasCapability(prov.Capabilities(), "dependency") {
log.Info("provider does not have dependency capability", "provider", name)
continue
}

if treeOutput {
deps, err := prov.GetDependenciesDAG(ctx)
if err != nil {
errLog.Error(err, "error matching label selector on deps")
errLog.Error(err, "failed to get list of dependencies for provider", "provider", name)
continue
}
for u, ds := range deps {
depsTree = append(depsTree, konveyor.DepsTreeItem{
FileURI: string(u),
Provider: name,
Dependencies: ds,
})
}
} else {
deps, err := prov.GetDependencies(ctx)
if err != nil {
errLog.Error(err, "failed to get list of dependencies for provider", "provider", name)
continue
}
for u, ds := range deps {
newDeps := ds
if labelSelector != nil {
newDeps, err = labelSelector.MatchList(ds)
if err != nil {
errLog.Error(err, "error matching label selector on deps")
continue
}
}
depsFlat = append(depsFlat, konveyor.DepsFlatItem{
Provider: name,
FileURI: string(u),
Dependencies: newDeps,
})
}
}
depsFlat = append(depsFlat, konveyor.DepsFlatItem{
Provider: name,
FileURI: string(u),
Dependencies: newDeps,
})
}
}
}

// stop providers before exiting
for _, prov := range providers {
prov.Stop()
}
// stop providers before exiting
for _, prov := range providers {
prov.Stop()
}

if depsFlat == nil && depsTree == nil {
errLog.Info("failed to get dependencies from all given providers")
os.Exit(1)
}
if depsFlat == nil && depsTree == nil {
errLog.Info("failed to get dependencies from all given providers")
os.Exit(1)
}

var b []byte
if treeOutput {
b, err = yaml.Marshal(depsTree)
if err != nil {
errLog.Error(err, "failed to marshal dependency data as yaml")
os.Exit(1)
}
} else {
// Sort depsFlat
sort.SliceStable(depsFlat, func(i, j int) bool {
if depsFlat[i].Provider == depsFlat[j].Provider {
return depsFlat[i].FileURI < depsFlat[j].FileURI
var b []byte
if treeOutput {
b, err = yaml.Marshal(depsTree)
if err != nil {
errLog.Error(err, "failed to marshal dependency data as yaml")
os.Exit(1)
}
} else {
return depsFlat[i].Provider < depsFlat[j].Provider
// Sort depsFlat
sort.SliceStable(depsFlat, func(i, j int) bool {
if depsFlat[i].Provider == depsFlat[j].Provider {
return depsFlat[i].FileURI < depsFlat[j].FileURI
} else {
return depsFlat[i].Provider < depsFlat[j].Provider
}
})

b, err = yaml.Marshal(depsFlat)
if err != nil {
errLog.Error(err, "failed to marshal dependency data as yaml")
os.Exit(1)
}
}
})

b, err = yaml.Marshal(depsFlat)
if err != nil {
errLog.Error(err, "failed to marshal dependency data as yaml")
os.Exit(1)
}
err = os.WriteFile(outputFile, b, 0644)
if err != nil {
errLog.Error(err, "failed to write dependencies to output file", "file", outputFile)
os.Exit(1)
}

},
}
rootCmd.Flags().StringVar(&providerSettings, "provider-settings", "provider_settings.json", "path to the provider settings")
rootCmd.Flags().BoolVar(&treeOutput, "tree", false, "output dependencies as a tree")
rootCmd.Flags().StringVar(&outputFile, "output-file", "output.yaml", "path to output file")
rootCmd.Flags().StringVar(&depLabelSelector, "dep-label-selector", "", "an expression to select dependencies based on labels provided by the provider")
return rootCmd

err = os.WriteFile(outputFile, b, 0644)
if err != nil {
errLog.Error(err, "failed to write dependencies to output file", "file", outputFile)
}

func main() {
if err := DependencyCmd().Execute(); err != nil {
os.Exit(1)
} else if DependencyCmd().Flags().Changed("help") {
return
}

}

func validateFlags() error {
Expand Down

0 comments on commit d483bcb

Please sign in to comment.