Skip to content

🎯 ExecuteScript

Runs a script defined in composer.json, equivalent to running composer run <script>.

When to use

Use it when you want to simply and directly trigger an already-defined script without forwarding extra arguments. The difference from RunScript is that it uses the composer run command and does not accept variadic arguments.

Signature

go
func (c *Composer) ExecuteScript(scriptName string) (string, error)

Parameters

ParameterTypeDescription
scriptNamestringThe name of the script to execute; it must be defined in the scripts section of composer.json

Return value

Return valueTypeDescription
First return valuestringThe standard output of the command execution
Second return valueerrorReturned when an error occurs during execution; nil indicates 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("initialization failed: %v", err)
	}

	// Execute the deploy script
	output, err := comp.ExecuteScript("deploy")
	if err != nil {
		log.Fatalf("failed to execute script: %v", err)
	}
	fmt.Println(output)
}

Advanced

  • To forward command-line arguments to the script, use RunScript (composer run-script ... -- <args>).
  • To list all available scripts, use ListScripts.
  • To add a new script definition in composer.json, you can use the AddScript method.

Released under the MIT License