Skip to content

📦 ArchivePackage ​

Creates an archive file for the specified package, optionally with a specific version. Equivalent to running composer archive <packageName>[=<version>] --dir=<destination>.

When to use ​

Use when you need to package a dependency package (rather than the whole project) into an archive, for offline distribution or mirror archiving.

Signature ​

go
func (c *Composer) ArchivePackage(packageName string, version string, destination string) (string, error)

Parameters ​

ParameterTypeDescription
packageNamestringName of the package to archive, e.g. symfony/console
versionstringPackage version, e.g. v5.4.0; pass an empty string to use the latest version
destinationstringTarget directory path where the archive file is stored

Return value ​

Return valueTypeDescription
First return valuestringOutput of the archive command
Second return valueerrorReturned when archive creation fails

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)
	}

	// Archive a specific version of a package
	output, err := comp.ArchivePackage("symfony/console", "v5.4.0", "/tmp/pkg-archive")
	if err != nil {
		log.Fatalf("failed to create package archive: %v", err)
	}
	fmt.Println("Package archive result:", output)

	// Archive the latest version
	if _, err := comp.ArchivePackage("symfony/console", "", "/tmp/pkg-archive"); err != nil {
		log.Fatalf("failed to create package archive: %v", err)
	}
}

Advanced ​

  • To customize more options such as format, use ArchivePackageWithOptions (located in archive.go).
  • To archive the whole project, use Archive.
  • The archive command is executed under the hood via Run; refer to it for timeout and context control.

Released under the MIT License