Skip to content

⏱️ RunWithTimeout ​

Executes a Composer command within a specified timeout, auto-cancelling when the timeout elapses. Internally implemented on top of RunWithContext.

When to use ​

Use this when a command may run for a long time (e.g. install, update on a large project) and you want to give it an explicit time limit. Run defaults to 10 minutes; this method lets you customize it.

Signature ​

go
func (c *Composer) RunWithTimeout(timeout time.Duration, args ...string) (string, error)

Parameters ​

ParameterTypeDescription
timeouttime.DurationThe maximum timeout for command execution
args...stringCommand arguments; the first argument is the Composer subcommand

Return value ​

Return valueTypeDescription
First return valuestringThe standard output of the command (including merged stderr)
Second return valueerrorReturned when the command fails or times out (context.DeadlineExceeded)

Example ​

go
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("init failed: %v", err)
	}

	// Give the install command a 30-minute timeout
	output, err := comp.RunWithTimeout(30*time.Minute, "install")
	if err != nil {
		log.Fatalf("install timed out or failed: %v", err)
	}
	fmt.Println("Install complete:")
	fmt.Println(output)
}

Advanced ​

  • For manual cancellation, use RunWithContext.
  • If you do not need a custom timeout, use Run (default 10 minutes, determined by Options.DefaultTimeout).
  • To adjust the default timeout, set Options.DefaultTimeout at construction time.

Released under the MIT License