Skip to content

⚡ 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

ParameterTypeDescription
scriptNamestringThe name of the script to execute; must be defined in the scripts section of composer.json
args...stringExtra arguments passed through to the script (appended after --)

Return value

Return valueTypeDescription
First return valuestringThe standard output of the command execution
Second return valueerrorReturned 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 on composer run).
  • To see which scripts are defined in the project, use ListScripts.
  • To read script definitions programmatically, call the GetScripts() method to get a map[string]interface{}.

Released under the MIT License