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

Project mode is default unless chdir or filename flags are being used #318

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func Root() *cobra.Command {
}
}

fFileMode = !cmd.Flags().Changed("project")

if envProject, ok := os.LookupEnv("RUNME_PROJECT"); ok && fFileMode {
fFileMode = false
// backwards compat
if envProject, ok := os.LookupEnv("RUNME_PROJECT"); ok {
fProject = envProject
}

if !fFileMode && !cmd.Flags().Changed("allow-unnamed") {
fAllowUnnamed = false
fFileMode = cmd.Flags().Changed("chdir") || cmd.Flags().Changed("filename")

if fFileMode && !cmd.Flags().Changed("allow-unnamed") {
fAllowUnnamed = true
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand All @@ -63,7 +63,7 @@ func Root() *cobra.Command {
pflags := cmd.PersistentFlags()

pflags.BoolVar(&fAllowUnknown, "allow-unknown", true, "Display snippets without known executor")
pflags.BoolVar(&fAllowUnnamed, "allow-unnamed", true, "Allow scripts without explicit names")
pflags.BoolVar(&fAllowUnnamed, "allow-unnamed", false, "Allow scripts without explicit names")

pflags.StringVar(&fChdir, "chdir", getCwd(), "Switch to a different working directory before executing the command")
pflags.StringVar(&fFileName, "filename", "README.md", "Name of the README file")
Expand Down
24 changes: 9 additions & 15 deletions testdata/script/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ exec runme ls
cmp stdout golden-list.txt
! stderr .

exec runme ls --allow-unknown=false
cmp stdout golden-list-disallow-unknown.txt
exec runme ls --allow-unnamed=true
cmp stdout golden-list-allow-unnamed.txt
! stderr .

! exec runme ls --filename nonexistent.md
Expand All @@ -16,26 +16,26 @@ stdout 'Hello, runme!'
! stderr .

env SHELL=/bin/bash
exec runme run --index 0
exec runme run --filename README.md --index 0
stdout 'Hello, runme!'
! stderr .

env SHELL=/bin/bash
exec runme run echo-1
exec runme run --filename README.md echo-1
stdout '1\n2\n3\n'
! stderr .

env SHELL=/bin/bash
exec runme run tempdir
exec runme run --allow-unnamed tempdir
stdout 'hi!'
! stderr .

! exec runme run --allow-unknown=false hello-world-2
! exec runme run --filename README.md --allow-unknown=false hello-world-2
! stdout .
stderr 'unable to find any script named "hello-world-2"'

env HOME=/tmp
exec sh -c 'runme run package-main'
exec sh -c 'runme run --allow-unnamed package-main'
stdout 'Hello from Go, runme!'
! stderr .

Expand Down Expand Up @@ -113,17 +113,11 @@ func main() {

-- golden-list.txt --
NAME FILE FIRST COMMAND DESCRIPTION
echo-hello README.md echo "Hello, runme!" This is a basic snippet with shell command.
echo-hello-2 README.md echo "Hello, runme!" You can omit the language, and runme will assume you are in shell.
echo-inferred README.md echo Inferred Names will automatically be inferred from a script's contents.
echo README.md echo "Hello, runme!" With {name=hello} you can annotate it and give it a nice name.
echo-1 README.md echo "1" It can contain multiple lines too.
echo-hello-3 README.md echo "Hello, runme! Again!" Also, the dollar sign is not needed.
tempdir README.md temp_dir=$(mktemp -d -t "runme-XXXXXXX") It works with cd, pushd, and similar because all lines are executed as a single script.
package-main README.md package main It can also execute a snippet of Go code.
-- golden-list-disallow-unknown.txt --
-- golden-list-allow-unnamed.txt --
NAME FILE FIRST COMMAND DESCRIPTION
echo-hello README.md echo "Hello, runme!" This is a basic snippet with shell command.
echo-hello-2 README.md echo "Hello, runme!" You can omit the language, and runme will assume you are in shell.
echo-inferred README.md echo Inferred Names will automatically be inferred from a script's contents.
echo README.md echo "Hello, runme!" With {name=hello} you can annotate it and give it a nice name.
echo-1 README.md echo "1" It can contain multiple lines too.
Expand Down