Skip to content

🔑 AddGitLabToken

Adds a GitLab OAuth token to Composer's auth.json, used to access private GitLab repositories.

When to use

Use when the project depends on private Composer repositories hosted on GitLab (including self-hosted instances). The token is written to the gitlab-oauth section of auth.json in the Composer home directory.

Signature

go
func (c *Composer) AddGitLabToken(domain string, token string) error

Parameters

ParameterTypeDescription
domainstringDomain the token applies to, e.g. gitlab.com or a self-hosted instance domain
tokenstringGitLab personal access token

Return value

error: Returns the corresponding error message when reading or writing auth.json fails; 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)
	}

	// Add a token for a self-hosted GitLab instance
	if err := comp.AddGitLabToken("gitlab.example.com", "glpat-xxxxxxxxxxxxxxxxxxxx"); err != nil {
		log.Fatalf("failed to add token: %v", err)
	}

	fmt.Println("GitLab token written to auth.json")
}

Advanced

Released under the MIT License