Skip to content
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
vendor/

crane
.DS_Store
.DS_Store

audit/
9 changes: 6 additions & 3 deletions cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/konveyor/crane/internal/apply"
"github.com/konveyor/crane/internal/flags"
"github.com/konveyor/crane/internal/kustomize"
internalTransform "github.com/konveyor/crane/internal/transform"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -45,12 +45,14 @@ type Flags struct {
func (o *Options) Complete(c *cobra.Command, args []string) error {
// Store positional arguments as requested stages
o.RequestedStages = args
o.globalFlags.SetCmdName("apply")
o.log = o.globalFlags.GetLoggerOrDefault()

return nil
}

func (o *Options) Validate() error {
log := o.globalFlags.GetLoggerOrDefault()
log := o.log
info, err := os.Stat(o.TransformDir)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -74,6 +76,7 @@ func (o *Options) Run() error {
func NewApplyCommand(f *flags.GlobalFlags) *cobra.Command {
o := &Options{
cobraGlobalFlags: f,
log: logrus.StandardLogger(),
}
cmd := &cobra.Command{
Use: "apply [stage...]",
Expand Down Expand Up @@ -135,7 +138,7 @@ func addFlagsForOptions(o *Flags, cmd *cobra.Command) {
}

func (o *Options) run() error {
log := o.globalFlags.GetLoggerOrDefault()
log := o.log
log.Infof("Starting apply...")

transformDir, err := filepath.Abs(o.TransformDir)
Expand Down
6 changes: 5 additions & 1 deletion cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func TestComplete(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Options{}
o := &Options{
log: logrus.StandardLogger()}
cmd := &cobra.Command{}

err := o.Complete(cmd, tt.args)
Expand Down Expand Up @@ -237,6 +238,7 @@ func TestRun_UnresolvedStagesError(t *testing.T) {
o := &Options{
cobraGlobalFlags: globalFlags,
globalFlags: globalFlags,
log: logrus.StandardLogger(),
Flags: Flags{
TransformDir: transformDir,
OutputDir: outputDir,
Expand Down Expand Up @@ -302,6 +304,7 @@ func TestValidate_TransformDir(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Options{
log: logrus.StandardLogger(),
Flags: Flags{
TransformDir: tt.transformDir,
},
Expand Down Expand Up @@ -331,6 +334,7 @@ func TestValidate_MissingTransformDir_DoesNotCreateOutputDir(t *testing.T) {
outputDir := filepath.Join(tmpDir, "output")

o := &Options{
log: logrus.StandardLogger(),
Flags: Flags{
TransformDir: missingTransformDir,
OutputDir: outputDir,
Expand Down
16 changes: 7 additions & 9 deletions cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package convert

import (
"github.com/konveyor/crane-lib/convert"
"github.com/konveyor/crane/internal/flags"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -16,6 +17,7 @@ import (
type ConvertOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
globalFlags *flags.GlobalFlags
SourceContext string
Namespace string
Logger logrus.FieldLogger
Expand All @@ -27,15 +29,11 @@ type ConvertOptions struct {
debug bool
}

func NewConvertOptions(streams genericclioptions.IOStreams) *cobra.Command {
logger := logrus.New()
logger.SetOutput(streams.Out)
logger.SetFormatter(&logrus.TextFormatter{})

func NewConvertOptions(streams genericclioptions.IOStreams, f *flags.GlobalFlags) *cobra.Command {
t := &ConvertOptions{
configFlags: genericclioptions.NewConfigFlags(false),
IOStreams: streams,
Logger: logger,
globalFlags: f,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -72,11 +70,11 @@ func addFlagsForConvertOptions(t *ConvertOptions, cmd *cobra.Command) {
}

func (t *ConvertOptions) Complete(c *cobra.Command, args []string) error {
t.globalFlags.SetCmdName("convert")
if t.debug {
if logger, ok := t.Logger.(*logrus.Logger); ok {
logger.SetLevel(logrus.DebugLevel)
}
t.globalFlags.Debug = true
}
t.Logger = t.globalFlags.GetLoggerOrDefault()
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd/api"

)

// ExportOptions holds CLI flags and runtime state for a single export run.
Expand Down Expand Up @@ -56,6 +55,7 @@ type ExportOptions struct {
// Complete loads kubeconfig context, namespace, and parses --as-extras into o.extras.
func (o *ExportOptions) Complete(c *cobra.Command, args []string) error {
var err error
o.globalFlags.SetCmdName("export")
o.log = o.globalFlags.GetLoggerOrDefault()
log := o.log

Expand Down Expand Up @@ -108,15 +108,15 @@ func (o *ExportOptions) Complete(c *cobra.Command, args []string) error {
// Users can override by explicitly using --include-gk Event or --exclude-gk <other-kinds>
if len(o.includeGK) == 0 && len(o.excludeGK) == 0 {
o.excludeGK = []string{"Event"}
log.Debugf("No GK filters specified; applying default exclusion: Event")
}

return nil
}

// Validate checks flag combinations (e.g. --as-extras requires impersonation).
func (o *ExportOptions) Validate() error {
log := o.globalFlags.GetLoggerOrDefault()

log := o.log
if o.configFlags.Context != nil && *o.configFlags.Context != "" {
for _, f := range []struct {
flag string
Expand Down Expand Up @@ -156,7 +156,7 @@ func (o *ExportOptions) Validate() error {
}
}
if _, err := NewGKFilter(o.includeGK, o.excludeGK); err != nil {
log.Debugf("Invalid GK filter: %v", err)
log.Errorf("Invalid GK filter: %v", err)
return err
}
return nil
Expand Down Expand Up @@ -218,7 +218,7 @@ func mergeImpersonationExtras(dest, src map[string][]string) map[string][]string
func (o *ExportOptions) Run() error {
var err error

log := o.globalFlags.GetLoggerOrDefault()
log := o.log
log.Infof("Starting export for namespace %q", o.userSpecifiedNamespace)

restConfig, err := o.configFlags.ToRESTConfig()
Expand Down Expand Up @@ -358,10 +358,10 @@ func (o *ExportOptions) Run() error {
// NewExportCommand builds the cobra export command with flags and viper wiring.
func NewExportCommand(streams genericclioptions.IOStreams, f *flags.GlobalFlags) *cobra.Command {
o := &ExportOptions{
configFlags: genericclioptions.NewConfigFlags(true),

configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
cobraGlobalFlags: f,
log: logrus.StandardLogger(),
}
cmd := &cobra.Command{
Use: "export",
Expand Down
37 changes: 21 additions & 16 deletions cmd/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestComplete_AsExtras(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &ExportOptions{
log: logrus.StandardLogger(),
configFlags: genericclioptions.NewConfigFlags(true),
asExtras: tt.asExtras,
}
Expand Down Expand Up @@ -180,6 +181,7 @@ func TestValidate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
o := &ExportOptions{
configFlags: genericclioptions.NewConfigFlags(true),
log: logrus.StandardLogger(),
asExtras: tt.asExtras,
labelSelector: tt.labelSelector,
}
Expand Down Expand Up @@ -261,6 +263,7 @@ func TestValidate_ContextConflicts(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
o := &ExportOptions{
configFlags: genericclioptions.NewConfigFlags(true),
log: logrus.StandardLogger(),
}
if tt.context != nil {
o.configFlags.Context = tt.context
Expand Down Expand Up @@ -335,6 +338,7 @@ func TestValidate_CRDGroupConflict(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &ExportOptions{
log: logrus.StandardLogger(),
configFlags: genericclioptions.NewConfigFlags(true),
crdSkipGroups: tt.crdSkipGroups,
crdIncludeGroups: tt.crdIncludeGroups,
Expand Down Expand Up @@ -666,7 +670,8 @@ func TestValidate_GKFilter(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
o := &ExportOptions{
configFlags: genericclioptions.NewConfigFlags(true),
globalFlags: nil, // GetLoggerOrDefault handles nil
globalFlags: nil,
log: logrus.StandardLogger(),
includeGK: tt.includeGK,
excludeGK: tt.excludeGK,
}
Expand All @@ -684,33 +689,33 @@ func TestValidate_GKFilter(t *testing.T) {

func TestComplete_DefaultExcludeEvent(t *testing.T) {
tests := []struct {
name string
includeGK []string
excludeGK []string
name string
includeGK []string
excludeGK []string
wantExclude []string
}{
{
name: "no GK filters - defaults to exclude Event",
includeGK: nil,
excludeGK: nil,
name: "no GK filters - defaults to exclude Event",
includeGK: nil,
excludeGK: nil,
wantExclude: []string{"Event"},
},
{
name: "explicit include - no default",
includeGK: []string{"Deployment"},
excludeGK: nil,
name: "explicit include - no default",
includeGK: []string{"Deployment"},
excludeGK: nil,
wantExclude: nil,
},
{
name: "explicit exclude - no default",
includeGK: nil,
excludeGK: []string{"Secret"},
name: "explicit exclude - no default",
includeGK: nil,
excludeGK: []string{"Secret"},
wantExclude: []string{"Secret"},
},
{
name: "empty slices - defaults to exclude Event",
includeGK: []string{},
excludeGK: []string{},
name: "empty slices - defaults to exclude Event",
includeGK: []string{},
excludeGK: []string{},
wantExclude: []string{"Event"},
},
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/plugin-manager/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {

cobraGlobalFlags *flags.GlobalFlags
globalFlags *flags.GlobalFlags
log *logrus.Logger
// Two Flags struct fields are needed
// 1. cobraFlags for explicit CLI args parsed by cobra
// 2. Flags for the args merged with values from the viper config file
Expand All @@ -42,6 +43,8 @@ type Flags struct {

func (o *Options) Complete(c *cobra.Command, args []string) error {
// TODO: @jgabani
o.globalFlags.SetCmdName("plugin-manager add")
o.log = o.globalFlags.GetLoggerOrDefault()
return nil
}

Expand Down Expand Up @@ -96,7 +99,6 @@ func NewAddCommand(f *flags.GlobalFlags) *cobra.Command {
o := &Options{
globalFlags: f,
}
log := o.globalFlags.GetLogger()
cmd := &cobra.Command{
Use: "add <name>",
Short: "installs the desired plugin",
Expand All @@ -105,7 +107,7 @@ func NewAddCommand(f *flags.GlobalFlags) *cobra.Command {
return err
}
if err := o.Validate(args); err != nil {
log.Errorf("%s", err.Error())
o.log.Errorf("%s", err.Error())
return nil
}
if err := o.Run(args); err != nil {
Expand All @@ -130,7 +132,7 @@ func addFlagsForOptions(o *Flags, cmd *cobra.Command) {
}

func (o *Options) run(args []string) error {
log := o.globalFlags.GetLogger()
log := o.log
Comment thread
Tamar-Dinavetsky marked this conversation as resolved.

manifestMap, err := plugin.BuildManifestMap(log, args[0], o.Repo)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions cmd/plugin-manager/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/konveyor/crane/internal/flags"
"github.com/konveyor/crane/internal/plugin"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -20,6 +21,7 @@ type Options struct {
// 2. globalFlags for the args merged with values from the viper config file
cobraGlobalFlags *flags.GlobalFlags
globalFlags *flags.GlobalFlags
log *logrus.Logger
// Two Flags struct fields are needed
// 1. cobraFlags for explicit CLI args parsed by cobra
// 2. Flags for the args merged with values from the viper config file
Expand All @@ -45,6 +47,8 @@ type AvailablePlugins struct {

func (o *Options) Complete(c *cobra.Command, args []string) error {
// TODO: @jgabani
o.globalFlags.SetCmdName("plugin-manager list")
o.log = o.globalFlags.GetLoggerOrDefault()
return nil
}

Expand Down Expand Up @@ -96,7 +100,7 @@ func addFlagsForOptions(o *Flags, cmd *cobra.Command) {
}

func (o *Options) run() error {
log := o.globalFlags.GetLogger()
log := o.log
if o.Installed {
// retrieve list of all the plugins that are installed within plugin dir
// TODO: differentiate between multiple repos
Expand Down Expand Up @@ -163,7 +167,7 @@ func (o *Options) run() error {
return nil
}

//TODO: this can be merged with printParamsInformation
// TODO: this can be merged with printParamsInformation
func printInstalledInformation(plugins []transform2.Plugin) {
for _, thisPlugin := range plugins {
printTable([][]string{
Expand Down
Loading
Loading