Skip to content

Commit

Permalink
Make dev and deploy subcommands display welcoming messages too
Browse files Browse the repository at this point in the history
This relates to [1], because users might have
similar symptoms (few seconds before any output)
when running either `odo dev` or `odo deploy`
in a source code directory with no Devfile,
and those commands try to do an Alizer analysis first.

[1] redhat-developer#5494
  • Loading branch information
rm3l committed Mar 16, 2022
1 parent 83f26da commit 78d501d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 48 deletions.
24 changes: 24 additions & 0 deletions pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,27 @@ func (o InitClient) SelectAndPersonalizeDevfile(flags map[string]string, context
}
return devfileObj, devfilePath, nil
}

func (o InitClient) InitDevfile(flags map[string]string, contextDir string, preInitHandlerFunc func(interactiveMode bool)) error {
containsDevfile, err := location.DirectoryContainsDevfile(o.fsys, contextDir)
if err != nil {
return err
}
if containsDevfile {
return nil
}

preInitHandlerFunc(len(flags) == 0)

devfileObj, _, err := o.SelectAndPersonalizeDevfile(map[string]string{}, contextDir)
if err != nil {
return err
}

// Set the name in the devfile and writes the devfile back to the disk
err = o.PersonalizeName(devfileObj, map[string]string{})
if err != nil {
return fmt.Errorf("failed to update the devfile's name: %w", err)
}
return nil
}
6 changes: 6 additions & 0 deletions pkg/init/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Client interface {
// Validate checks for each backend if flags are valid
Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error

// InitDevfile allows to initialize a Devfile in cases where this operation is needed as a prerequisite,
// like if the directory contains no Devfile at all.
// `preInitHandlerFunc` allows to perform operations prior to triggering the actual Devfile
// initialization and personalization process.
InitDevfile(flags map[string]string, contextDir string, preInitHandlerFunc func(interactiveMode bool)) error

// SelectDevfile returns information about a devfile selected based on Alizer if the directory content,
// or based on the flags if the directory is empty, or
// interactively if flags is empty
Expand Down
14 changes: 14 additions & 0 deletions pkg/init/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 6 additions & 24 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"

"k8s.io/kubectl/pkg/util/templates"
)

Expand Down Expand Up @@ -64,7 +62,12 @@ func (o *DeployOptions) Complete(cmdline cmdline.Cmdline, args []string) (err er
return errors.New("this command cannot run in an empty directory, you need to run it in a directory containing source code")
}

err = o.initDevfile()
err = o.clientset.InitClient.InitDevfile(cmdline.GetFlags(), o.contextDir, func(interactiveMode bool) {
if interactiveMode {
fmt.Println("The current directory already contains source code. " +
"odo will try to autodetect the language and project type in order to select the best suited Devfile for your project.")
}
})
if err != nil {
return err
}
Expand Down Expand Up @@ -105,27 +108,6 @@ func (o *DeployOptions) Validate() error {
return nil
}

func (o *DeployOptions) initDevfile() error {
containsDevfile, err := location.DirectoryContainsDevfile(filesystem.DefaultFs{}, o.contextDir)
if err != nil {
return err
}
if containsDevfile {
return nil
}
devfileObj, _, err := o.clientset.InitClient.SelectAndPersonalizeDevfile(map[string]string{}, o.contextDir)
if err != nil {
return err
}

// Set the name in the devfile and writes the devfile back to the disk
err = o.clientset.InitClient.PersonalizeName(devfileObj, map[string]string{})
if err != nil {
return fmt.Errorf("failed to update the devfile's name: %w", err)
}
return nil
}

// Run contains the logic for the odo command
func (o *DeployOptions) Run() error {
devfileObj := o.EnvSpecificInfo.GetDevfileObj()
Expand Down
30 changes: 6 additions & 24 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/redhat-developer/odo/pkg/devfile/adapters"
"github.com/redhat-developer/odo/pkg/devfile/adapters/common"
"github.com/redhat-developer/odo/pkg/devfile/location"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
"github.com/redhat-developer/odo/pkg/watch"

dfutil "github.com/devfile/library/pkg/util"
Expand Down Expand Up @@ -81,7 +80,12 @@ func (o *DevOptions) Complete(cmdline cmdline.Cmdline, args []string) error {
return errors.New("this command cannot run in an empty directory, you need to run it in a directory containing source code")
}

err = o.initDevfile()
err = o.clientset.InitClient.InitDevfile(cmdline.GetFlags(), o.contextDir, func(interactiveMode bool) {
if interactiveMode {
fmt.Println("The current directory already contains source code. " +
"odo will try to autodetect the language and project type in order to select the best suited Devfile for your project.")
}
})
if err != nil {
return err
}
Expand Down Expand Up @@ -141,28 +145,6 @@ func (o *DevOptions) Validate() error {
return err
}

func (o *DevOptions) initDevfile() error {
containsDevfile, err := location.DirectoryContainsDevfile(filesystem.DefaultFs{}, o.contextDir)
if err != nil {
return err
}
if containsDevfile {
return nil
}

devfileObj, _, err := o.clientset.InitClient.SelectAndPersonalizeDevfile(map[string]string{}, o.contextDir)
if err != nil {
return err
}

// Set the name in the devfile and writes the devfile back to the disk
err = o.clientset.InitClient.PersonalizeName(devfileObj, map[string]string{})
if err != nil {
return fmt.Errorf("failed to update the devfile's name: %w", err)
}
return nil
}

func (o *DevOptions) Run() error {
var err error
var platformContext = kubernetes.KubernetesContext{
Expand Down
39 changes: 39 additions & 0 deletions tests/interactive/cmd_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,44 @@ var _ = Describe("odo deploy interactive command tests", func() {
Expect(output).To(ContainSubstring("no deploy command found in devfile"))
Expect(helper.ListFilesInDir(commonVar.Context)).To(ContainElements("devfile.yaml"))
})

It("should display welcoming messages first", func() {

language := "python"
output, err := helper.RunInteractive([]string{"odo", "deploy"},
// Setting verbosity level to 0, because we would be asserting the welcoming message is the first
// message displayed to the end user. So we do not want any potential debug lines to be printed first.
// Using envvars here (and not via the -v flag), because of https://github.com/redhat-developer/odo/issues/5513
[]string{"ODO_LOG_LEVEL=0"},
func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Based on the files in the current directory odo detected")

helper.ExpectString(ctx, fmt.Sprintf("Language: %s", language))

helper.ExpectString(ctx, fmt.Sprintf("Project type: %s", language))

helper.ExpectString(ctx,
fmt.Sprintf("The devfile \"%s\" from the registry \"DefaultDevfileRegistry\" will be downloaded.", language))

helper.ExpectString(ctx, "Is this correct")
helper.SendLine(ctx, "\n")

helper.ExpectString(ctx, "Select container for which you want to change configuration")
helper.SendLine(ctx, "\n")

helper.ExpectString(ctx, "Enter component name")
helper.SendLine(ctx, "my-app")

helper.ExpectString(ctx, "no deploy command found in devfile")
})

Expect(err).To(Not(BeNil()))
// Make sure it also displays welcoming messages first
lines, err := helper.ExtractLines(output)
Expect(err).To(BeNil())
Expect(lines).To(Not(BeEmpty()))
Expect(lines[0]).To(Equal("The current directory already contains source code. " +
"odo will try to autodetect the language and project type in order to select the best suited Devfile for your project."))
})
})
})
38 changes: 38 additions & 0 deletions tests/interactive/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,43 @@ var _ = Describe("odo dev interactive command tests", func() {

Expect(helper.ListFilesInDir(commonVar.Context)).To(ContainElements("devfile.yaml"))
})

It("should display welcoming messages first", func() {

language := "python"
output, _ := helper.RunInteractive([]string{"odo", "dev"},
// Setting verbosity level to 0, because we would be asserting the welcoming message is the first
// message displayed to the end user. So we do not want any potential debug lines to be printed first.
// Using envvars here (and not via the -v flag), because of https://github.com/redhat-developer/odo/issues/5513
[]string{"ODO_LOG_LEVEL=0"},
func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Based on the files in the current directory odo detected")

helper.ExpectString(ctx, fmt.Sprintf("Language: %s", language))

helper.ExpectString(ctx, fmt.Sprintf("Project type: %s", language))

helper.ExpectString(ctx,
fmt.Sprintf("The devfile %q from the registry \"DefaultDevfileRegistry\" will be downloaded.", language))

helper.ExpectString(ctx, "Is this correct")
helper.SendLine(ctx, "\n")

helper.ExpectString(ctx, "Select container for which you want to change configuration")
helper.SendLine(ctx, "\n")

helper.ExpectString(ctx, "Enter component name")
helper.SendLine(ctx, "my-app")

helper.ExpectString(ctx, "Press Ctrl+c to exit")
ctx.StopCommand()
})

lines, err := helper.ExtractLines(output)
Expect(err).To(BeNil())
Expect(lines).To(Not(BeEmpty()))
Expect(lines[0]).To(Equal("The current directory already contains source code. " +
"odo will try to autodetect the language and project type in order to select the best suited Devfile for your project."))
})
})
})

0 comments on commit 78d501d

Please sign in to comment.