Skip to content

Commit

Permalink
Adds .RUN and .RUNFILE attributes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
TekWizely committed Jan 5, 2020
1 parent 584e19b commit 4cd9fd0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ In run, the entire script is executed within a single sub-shell.
- [Referencing Other Variables](#referencing-other-variables)
- [Shell Substitution](#shell-substitution)
- [Conditional Assignment](#conditional-assignment)
- [Invoking Other Commands & Runfiles](#invoking-other-commands--runfiles)
- [.RUN & .RUNFILE Attributes](#run--runfile-attributes)
- [Script Shells](#script-shells)
- [Per-Command Shell Config](#per-command-shell-config)
- [Global Default Shell Config](#global-default-shell-config)
Expand Down Expand Up @@ -706,6 +708,37 @@ NAME="Newman" run hello
Hello, Newman
```

#### Invoking Other Commands & Runfiles

##### .RUN / .RUNFILE Attributes
Run exposes the following attributes:

* `.RUN` - Absolute path of the run binary currently in use
* `.RUNFILE` - Absolute path of the current Runfile

Your command script can use these to invoke other commands:

_Runfile_
```
##
# Invokes hello
# EXPORT RUN := ${.RUN}
# EXPORT RUNFILE := ${.RUNFILE}
test:
"${RUN}" -r "${RUNFILE}" hello
hello:
echo "Hello, World"
```

_output_
```
$ run test
Hello, World
```


-----------------
### Script Shells

Expand Down
2 changes: 2 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func ProcessAST(ast *Ast) *runfile.Runfile {
// Seed defaults
//
rf.Scope.PutAttr(".SHELL", config.DefaultShell)
rf.Scope.PutAttr(".RUN", config.RunBin)
rf.Scope.PutAttr(".RUNFILE", config.RunFile)
for _, n := range ast.nodes {
n.Apply(rf)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ var CommandList []*Command
//
var CommandMap = make(map[string]*Command)

// RunBin holds the absolute path to the run command in use.
//
var RunBin string

// RunFile holds the absolute path to the current Runfile.
var RunFile string

// EnableFnTrace shows parser/lexer fn call/stack
//
var EnableFnTrace = false
Expand Down
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/tekwizely/run/internal/ast"
Expand Down Expand Up @@ -71,7 +72,12 @@ func showVersion() {
//
func main() {
config.ErrOut = os.Stderr
config.Me = path.Base(os.Args[0])
if exec, err := os.Executable(); err != nil { // Returns abs path on success
config.RunBin = exec
} else {
config.RunBin = os.Args[0] // Punt to arg[0]
}
config.Me = path.Base(config.RunBin)
// Configure logging
//
log.SetFlags(0)
Expand Down Expand Up @@ -116,13 +122,17 @@ func main() {
log.Printf("Error reading file '%s': File not considered 'regular'\n", inputFile)
showUsage() // exits
}
if config.RunFile, err = filepath.Abs(inputFile); err != nil {
log.Printf("Error reading file '%s': Cannot determine absolute path\n", inputFile)
showUsage() // exits
}
} else {
log.Printf("Input file not found: '%s' : Please create the file or specify an alternative", inputFile)
showUsage() // exits
}
// Read file into memory
//
fileBytes, err := readFile(inputFile)
fileBytes, err := readFile(config.RunFile)
if err != nil {
log.Printf("Error reading file '%s': %s\n", inputFile, err.Error())
showUsage() // exits
Expand Down

0 comments on commit 4cd9fd0

Please sign in to comment.