Skip to content

💾 SaveAuthConfig ​

Writes the given authentication configuration to the auth.json file in the Composer home directory in JSON format.

When to use ​

Use this when you directly construct or modify an AuthConfig struct (rather than going through the Add*Token family of convenience methods) and need to persist the change to disk. It can also be used for batch import/migration of authentication configuration.

Signature ​

go
func (c *Composer) SaveAuthConfig(config *AuthConfig) error

Parameters ​

ParameterTypeDescription
config*AuthConfigPointer to the authentication configuration struct to save

Return value ​

error: returns the corresponding error message when getting the directory, creating the directory, or writing the file 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)
	}

	// Directly construct and save an authentication configuration
	config := &composer.AuthConfig{
		GitHub: map[string]string{
			"github.com": "ghp_xxxxxxxxxxxxxxxxxxxx",
		},
		HTTPBasic: map[string]string{
			"repo.intranet.com": "ci-user:s3cret",
		},
	}

	if err := comp.SaveAuthConfig(config); err != nil {
		log.Fatalf("save auth config failed: %v", err)
	}

	fmt.Println("auth.json written")
}

Advanced ​

  • The file is serialized with json.MarshalIndent (4-space indentation), with permission 0600, readable and writable only by the current user.
  • Before writing, os.MkdirAll ensures the Composer home directory exists.
  • You usually do not need to call this manually; the Add*Token family of methods already call it internally. See GetAuthConfig.

Released under the MIT License