Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to run and list commands #230

Closed
wants to merge 17 commits into from
Closed

Conversation

christian-bromann
Copy link
Contributor

@christian-bromann christian-bromann commented Apr 30, 2023

According to our internal proposal this patch makes some changes to the run and list commands. These features are behind an environment flag RUNME_EXPERIMENTAL_CLI. So the current behavior doesn't change. The new (experimental) CLI behavior works as following:

  • the list command now lists all commands of all markdown files within a project
    • it only shows the command id and the description, everything else seems not relevant
  • the run command
    • now only takes the block name to execute it
    • can receive more block names which will be run sequentially
    • now has a -p or --parallel flag to run a set of commands in parallel
    • allows to execute sequential commands and parallel ones through one CLI call, e.g. runme run foo bar loo -p faz baz -s loo bar will run: foo, bar and loo sequentially, then will run faz and baz in parallel and lastly loo and bar sequential again

List Command Demo:
Screenshot 2023-05-01 at 22 59 55

Screenshot of the WebdriverIO project based on webdriverio/webdriverio#10237

Run Command Demo:
run

Reviewer Notes:
This patch basically does the following:

  • moves flat functions like getCodeBlocks or readMarkdownFile into the project module
  • implements a getAllMarkdownFiles in the project module to find all markdown files
  • implement LookUpCodeBlockById to find a block just by its name
  • unrelated: removed -s for server address as it seems to be better suited (and more used) for providing the sequential flag

Outstanding:

  • read from .gitignore to not include markdown files matching these patterns (otherwise we would e.g. read from a bunch of node_modules which would be quite overwhelming
  • write unit tests
  • more thorough testing of new functionality
  • detect duplicate commands and throw errors
  • improve error handling

@christian-bromann christian-bromann changed the title Improved CLI Improvements to run and list commands Apr 30, 2023
@christian-bromann christian-bromann marked this pull request as draft April 30, 2023 02:08
@sourishkrout
Copy link
Member

sourishkrout commented May 2, 2023

The scope of the original ticket #229 is about parallel/multi-target changes, there wasn't green light for any other changes yet @christian-bromann.

Copy link
Contributor

@mxsdev mxsdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really loving the UX changes, would love to see this implemented asap!

The runner-based implementation is also reasonable, though I do have some changes in mind. But the broad-strokes idea is good!

ctx, cancel := ctxWithSigCancel(cmd.Context())
defer cancel()
inParallelMode := false
blocks := os.Args[2:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an args argument in the RunE function. This will parse out all cli flags.

Do you think we can then use cobra's argument parsing facilities for the parallel/serial flags, and remove manual parsing?

Comment on lines +153 to +155
cmd.Flags().BoolVarP(&runInParallel, "parallel", "p", false, "Run commands in parallel. (experimental)")
cmd.Flags().BoolVarP(&runSequential, "sequential", "s", true, "Run commands sequentially. (experimental)")
cmd.Flags().BoolVarP(&onlyCommandIO, "onlyCommandOutput", "", false, "If set, Runme will only output command output. (experimental)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cobra has StringSliceP, which would allow for the following syntax:

runme run one two three -p four,five,six -s seven,eight,nine

Do you think that would be sufficient for this purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah totally! Nice

Comment on lines +69 to +71
if blockID == "--experimental" {
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot more valid flags than "--experimental" which you are not catching, which will presumably be run as blocks.

For example, running runme run -s a b c tries to parse -s as a code block

Comment on lines +85 to +88
func (p *Project) ReadMarkdownFile(relativePath string, args []string) ([]byte, error) {
absPath := path.Join(p.RootDir, relativePath)
rootDir := path.Dir(absPath)
fileName := path.Base(absPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (p *Project) ReadMarkdownFile(relativePath string, args []string) ([]byte, error) {
absPath := path.Join(p.RootDir, relativePath)
rootDir := path.Dir(absPath)
fileName := path.Base(absPath)
func (p *Project) ReadMarkdownFile(absPath string, args []string) ([]byte, error) {
rootDir := path.Dir(absPath)
fileName := path.Base(absPath)

Why not use an absolute path? The relative option seems to make some other parts of the code a little cumbersome

p := project.New(fChdir)
if !isInExperimentalMode() {
filePath := path.Join(fChdir, fFileName)
blocks, err := p.GetCodeBlocks(filePath[len(p.RootDir):], fAllowUnknown, fIgnoreNameless)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of filePath[len(p.RootDir):]?

At any rate, one of the methods in filepath would be much preferable rather than to do manual parsing...


ref, err := r.Head()
func (p *Project) getAllMarkdownFiles() ([]string, error) {
root, err := os.Getwd()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
root, err := os.Getwd()
root := p.RootDir

if ref.Name().IsBranch() {
return ref.Name().Short()
fsys := os.DirFS(root)
matches, err := fs.Glob(fsys, "**/*.md")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work as expected, and in particular will ignore files in the root directory.

You should probably use a WalkDir-based solution instead.

if err != nil {
return nil, errors.New("error while querying repository branches")
for _, file := range files {
codeBlock, err := p.GetCodeBlocks(file[len(p.RootDir):], false, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
codeBlock, err := p.GetCodeBlocks(file[len(p.RootDir):], false, true)
codeBlock, err := p.GetCodeBlocks(file[len(p.RootDir):], allowUnknown, ignoreNameless)

Should pass these down

internal/cmd/run.go Outdated Show resolved Hide resolved
select {
case <-wgDone:
// carry on
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This break does nothing since there is no surrounding loop

Co-authored-by: Max Stoumen <[email protected]>
@christian-bromann
Copy link
Contributor Author

Closing in favor of #236

@christian-bromann christian-bromann deleted the cb-better-cli branch May 18, 2023 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants