Skip to content

Commit

Permalink
Adds debug-command flag to odo watch (redhat-developer#4381)
Browse files Browse the repository at this point in the history
* Adds debug-command flag to odo watch

It takes the custom debug command from the CLI while trigerring odo watch.

Signed-off-by: mik-dass <mrinald7@gmail.com>

* Adds a mention of the custom build, run and debug flag for watch in the doc
  • Loading branch information
mik-dass authored and kadel committed Feb 4, 2021
1 parent f3d4cff commit 0d63234
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
7 changes: 7 additions & 0 deletions docs/public/deploying-a-devfile-using-odo.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ Pushing devfile component myopenliberty
You can now continue developing your application. Just refreshing the browser will render the source code changes.
You can also trigger `odo watch` with custom devfile build, run and debug commands.
[source,sh]
-----
$ odo watch --build-command="mybuild" --run-command="myrun" --debug-command="mydebug"
----
Run `odo delete` to delete the application from cluster.
. To delete your deployed application:
Expand Down
14 changes: 9 additions & 5 deletions pkg/odo/cli/component/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/validate"
"github.com/openshift/odo/pkg/envinfo"

"github.com/devfile/library/pkg/devfile"
"github.com/openshift/odo/pkg/config"
Expand Down Expand Up @@ -37,8 +38,8 @@ var watchLongDesc = ktemplates.LongDesc(`Watch for changes, update component on
var watchExampleWithDevfile = ktemplates.Examples(` # Watch for changes in directory for current component
%[1]s
# Watch source code changes with custom devfile commands using --build-command and --run-command for experimental mode
%[1]s --build-command="mybuild" --run-command="myrun"
# Watch source code changes with custom devfile commands using --build-command, --run-command and --debug-command for devfile based components
%[1]s --build-command="mybuild" --run-command="myrun" --debug-command="mydebug"
`)

// WatchOptions contains attributes of the watch command
Expand All @@ -63,6 +64,7 @@ type WatchOptions struct {
// devfile commands
devfileBuildCommand string
devfileRunCommand string
devfileDebugCommand string

*genericclioptions.Context
}
Expand Down Expand Up @@ -94,9 +96,6 @@ func (wo *WatchOptions) Complete(name string, cmd *cobra.Command, args []string)

// Get the component name
wo.componentName = wo.EnvSpecificInfo.GetName()
if err != nil {
return err
}

// Parse devfile and validate
devObj, err := devfile.ParseAndValidate(wo.devfilePath)
Expand Down Expand Up @@ -161,6 +160,9 @@ func (wo *WatchOptions) Validate() (err error) {

// if experimental mode is enabled and devfile is present, return. The rest of the validation is for non-devfile components
if util.CheckPathExists(wo.devfilePath) {
if wo.devfileDebugCommand != "" && wo.EnvSpecificInfo != nil && wo.EnvSpecificInfo.GetRunMode() != envinfo.Debug {
return fmt.Errorf("please start the component in debug mode using `odo push --debug` to use the --debug-command flag")
}
exists, err := wo.initialDevfileHandler.DoesComponentExist(wo.componentName)
if err != nil {
return err
Expand Down Expand Up @@ -218,6 +220,7 @@ func (wo *WatchOptions) Run() (err error) {
Show: wo.show,
DevfileBuildCmd: strings.ToLower(wo.devfileBuildCommand),
DevfileRunCmd: strings.ToLower(wo.devfileRunCommand),
DevfileDebugCmd: strings.ToLower(wo.devfileDebugCommand),
EnvSpecificInfo: wo.EnvSpecificInfo,
},
)
Expand Down Expand Up @@ -278,6 +281,7 @@ func NewCmdWatch(name, fullName string) *cobra.Command {

watchCmd.Flags().StringVar(&wo.devfileBuildCommand, "build-command", "", "Devfile Build Command to execute")
watchCmd.Flags().StringVar(&wo.devfileRunCommand, "run-command", "", "Devfile Run Command to execute")
watchCmd.Flags().StringVar(&wo.devfileDebugCommand, "debug-command", "", "Devfile Debug Command to execute")

// Adding context flag
genericclioptions.AddContextFlag(watchCmd, &wo.componentContext)
Expand Down
4 changes: 4 additions & 0 deletions pkg/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type WatchParameters struct {
DevfileBuildCmd string
// DevfileRunCmd takes the run command through the command line and overwrites devfile run command
DevfileRunCmd string
// DevfileDebugCmd takes the debug command through the command line and overwrites the devfile debug command
DevfileDebugCmd string
}

// addRecursiveWatch handles adding watches recursively for the path provided
Expand Down Expand Up @@ -347,6 +349,7 @@ func WatchAndPush(client *occlient.Client, out io.Writer, parameters WatchParame
ForceBuild: false,
DevfileBuildCmd: parameters.DevfileBuildCmd,
DevfileRunCmd: parameters.DevfileRunCmd,
DevfileDebugCmd: parameters.DevfileDebugCmd,
DevfileScanIndexForWatch: !hasFirstSuccessfulPushOccurred,
EnvSpecificInfo: *parameters.EnvSpecificInfo,
Debug: parameters.EnvSpecificInfo.GetRunMode() == envinfo.Debug,
Expand All @@ -373,6 +376,7 @@ func WatchAndPush(client *occlient.Client, out io.Writer, parameters WatchParame
ForceBuild: false,
DevfileBuildCmd: parameters.DevfileBuildCmd,
DevfileRunCmd: parameters.DevfileRunCmd,
DevfileDebugCmd: parameters.DevfileDebugCmd,
DevfileScanIndexForWatch: !hasFirstSuccessfulPushOccurred,
EnvSpecificInfo: *parameters.EnvSpecificInfo,
Debug: parameters.EnvSpecificInfo.GetRunMode() == envinfo.Debug,
Expand Down
55 changes: 40 additions & 15 deletions pkg/watch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ type mockPushParameters struct {
show bool
isDebug bool
debugPort int

devfileBuildCmd string
devfileRunCmd string
devfileDebugCmd string
}

var mockPush mockPushParameters
Expand All @@ -120,6 +124,15 @@ func mockDevFilePush(parameters common.PushParameters, _ WatchParameters) error
os.Exit(1)
}

if parameters.DevfileBuildCmd != mockPush.devfileBuildCmd || parameters.DevfileRunCmd != mockPush.devfileRunCmd || parameters.DevfileDebugCmd != mockPush.devfileDebugCmd {
fmt.Printf("some of the push parameters are different, wanted: %v, got: %v", mockPush, []string{
"devfileBuildCmd:" + parameters.DevfileBuildCmd,
"devfileRunCmd:" + parameters.DevfileRunCmd,
"devfileDebugCmd:" + parameters.DevfileDebugCmd,
})
os.Exit(1)
}

return commonChecks(parameters.Path, parameters.WatchFiles, parameters.WatchDeletedFiles, parameters.IgnoredFiles)
}

Expand Down Expand Up @@ -203,6 +216,9 @@ func TestWatchAndPush(t *testing.T) {
setupEnv func(componentName string, requiredFilePaths []testingutil.FileProperties) (string, map[string]testingutil.FileProperties, error)
isExperimental bool
isDebug bool
devfileBuildCmd string
devfileRunCmd string
devfileDebugCmd string
debugPort int
}{
{
Expand Down Expand Up @@ -410,9 +426,10 @@ func TestWatchAndPush(t *testing.T) {
ModificationType: testingutil.DELETE,
},
},
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
devfileBuildCmd: "build-new",
},
{
name: "Case 3: Valid watch with list of files to be ignored with a create and a delete event",
Expand Down Expand Up @@ -506,9 +523,10 @@ func TestWatchAndPush(t *testing.T) {
ModificationType: testingutil.DELETE,
},
},
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
devfileRunCmd: "run-new",
},
{
name: "Case 4: Valid watch with list of files to be ignored with a folder create event",
Expand Down Expand Up @@ -608,9 +626,10 @@ func TestWatchAndPush(t *testing.T) {
ModificationType: testingutil.CREATE,
},
},
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
want: []string{"__init__.py"},
wantDeleted: []string{"src/read_licenses.py"},
setupEnv: setUpF8AnalyticsComponentSrc,
devfileDebugCmd: "debug-new",
},
{
name: "Case 5: Valid watch with list of files to be ignored with a folder delete event",
Expand Down Expand Up @@ -740,8 +759,11 @@ func TestWatchAndPush(t *testing.T) {
isForcePush: tt.forcePush,
globExps: tt.ignores,
show: tt.show,
debugPort: tt.debugPort,
isDebug: tt.isDebug,
debugPort: tt.debugPort,
devfileBuildCmd: tt.devfileBuildCmd,
devfileRunCmd: tt.devfileRunCmd,
devfileDebugCmd: tt.devfileDebugCmd,
}

ExpectedChangedFiles = tt.want
Expand Down Expand Up @@ -812,11 +834,14 @@ func TestWatchAndPush(t *testing.T) {
ComponentName: tt.componentName,
Path: basePath,
// convert the glob expressions to absolute form for WatchAndPush to work properly
FileIgnores: util.GetAbsGlobExps(basePath, tt.ignores),
PushDiffDelay: tt.delayInterval,
StartChan: StartChan,
ExtChan: ExtChan,
Show: tt.show,
FileIgnores: util.GetAbsGlobExps(basePath, tt.ignores),
StartChan: StartChan,
ExtChan: ExtChan,
PushDiffDelay: tt.delayInterval,
Show: tt.show,
DevfileBuildCmd: tt.devfileBuildCmd,
DevfileRunCmd: tt.devfileRunCmd,
DevfileDebugCmd: tt.devfileDebugCmd,
}

if tt.isExperimental {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ commands:
group:
kind: debug
isDefault: true
- id: debug
exec:
component: runtime
commandLine: npm run debug
workingDir: ${PROJECTS_ROOT}
group:
kind: debug
12 changes: 12 additions & 0 deletions tests/integration/devfile/cmd_devfile_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ var _ = Describe("odo devfile watch command tests", func() {
// odo watch and validate if we can port forward successfully
utils.OdoWatchWithDebug(odoV2Watch, commonVar.Context, watchFlag)

// check the --debug-command flag
watchFlag = "--debug-command debug"
odoV2Watch.StringsToBeMatched = []string{"Executing debug command"}

// odo watch and validate if we can port forward successfully
utils.OdoWatchWithDebug(odoV2Watch, commonVar.Context, watchFlag)

// revert to normal odo push
watchFlag = ""
output = helper.CmdShouldPass("odo", "push", "--project", commonVar.Project)
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

Expand All @@ -194,6 +202,10 @@ var _ = Describe("odo devfile watch command tests", func() {
StringsToBeMatched: []string{"Executing devbuild command", "Executing devrun command"},
}
utils.OdoWatch(utils.OdoV1Watch{}, odoV2Watch, commonVar.Project, commonVar.Context, watchFlag, commonVar.CliRunner, "kube")

// check that the --debug-command fails when the component is not pushed using debug mode
output = helper.CmdShouldFailWithRetry(1, 1, "odo", "watch", "--context", commonVar.Context, "--debug-command", "debug")
Expect(output).To(ContainSubstring("please start the component in debug mode"))
})
})

Expand Down

0 comments on commit 0d63234

Please sign in to comment.