⚡ RunScript
Executes a script defined in composer.json and can pass extra arguments to the script, equivalent to running composer run-script <script> -- <args...>.
When to use
Use this when you need to trigger an automation script defined in the project (such as test, build, deploy) and want to pass through command-line arguments. Scripts are defined in the scripts section of composer.json.
Signature
go
func (c *Composer) RunScript(scriptName string, args ...string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
scriptName | string | The name of the script to execute; must be defined in the scripts section of composer.json |
args | ...string | Extra arguments passed through to the script (appended after --) |
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | string | The standard output of the command execution |
| Second return value | error | Returned when an error occurs during execution; nil on success |
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("init failed: %v", err)
}
// Run the test script and pass a filter argument
output, err := comp.RunScript("test", "--filter=UserTest")
if err != nil {
log.Fatalf("run script failed: %v", err)
}
fmt.Println(output)
}Advanced
- If you do not need to pass arguments and just want to run a script simply, use the more concise
ExecuteScript(based oncomposer run). - To see which scripts are defined in the project, use
ListScripts. - To read script definitions programmatically, call the
GetScripts()method to get amap[string]interface{}.