Help us shape the future of Go! Take the 2026 Go Developer Survey

manifest

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package manifest reads and writes .gotools.json: the layout, the settings and the list of managed tools.

A Manifest holds file values only. Values resolved from the environment belong to package config, so an environment override is never written back. Parse accepts any valid JSON layout and ignores unknown keys; Render writes the layout committed files use, with a fixed key order and the tools sorted.

Example

Parse reads any valid layout, Render writes the committed one, and Flush replaces the file atomically.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/piusalfred/gotools/internal/manifest"
)

func main() {
	res, err := manifest.Parse([]byte(`{"strategy":"split","tools":{"gofumpt":{"source":"go","package":"mvdan.cc/gofumpt","version":"v0.9.2"}}}`), ".gotools.json")
	fmt.Println(len(res.Warnings), err)

	m := res.Manifest

	m.SetTool(manifest.Tool{Name: "gci", Source: "go", Package: "github.com/daixiang0/gci", Version: "v0.14.0"})

	dir, _ := os.MkdirTemp("", "manifest-example")
	defer os.RemoveAll(dir)

	path := filepath.Join(dir, ".gotools.json")
	if err := m.Flush(path); err != nil {
		fmt.Println(err)
	}

	back, _ := manifest.Load(path)
	fmt.Println(back.Manifest.ToolNames())

}
Output:
0 <nil>
[gci gofumpt]

Index

Examples

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the manifest schema this build understands. Bump it only when reading would misinterpret a newer format; additive changes do not need a bump.

Variables

View Source
var (
	// ErrMissing matches the error Load returns when there is no manifest.
	ErrMissing = errors.New("manifest not found")
	// ErrMalformed matches the error returned for truncated or malformed JSON.
	ErrMalformed = errors.New("manifest is not parseable: truncated or malformed JSON")
)

Functions

func CleanupTemps

func CleanupTemps(path string)

CleanupTemps removes the temp files a crashed Flush left next to path.

func FindRoot

func FindRoot(start string) (root string, found bool)

FindRoot walks up from start looking for the manifest file. It returns the directory that holds it, or start when there is none. It never changes the process working directory.

Types

type Manifest

type Manifest struct {
	Schema       int
	Strategy     string
	Dir          string
	GoVersion    string
	ModulePrefix string
	Settings     config.Settings
	Tools        []Tool
}

Manifest is the in-memory form of .gotools.json. Empty layout fields mean the file does not set them. Settings carries the defaults for keys the file leaves out, and Render writes them all.

func New

func New() *Manifest

New returns an empty manifest whose settings are the defaults.

func (*Manifest) FindByPackage

func (m *Manifest) FindByPackage(pkg string) (string, bool)

FindByPackage returns the name of the first tool whose package is pkg.

func (*Manifest) Flush

func (m *Manifest) Flush(path string) error

Flush writes the manifest atomically: a temp file created exclusively next to the target, so a pre-planted symlink cannot redirect the write, then renamed over it.

func (*Manifest) Get

func (m *Manifest) Get(key string) (string, bool)

Get returns the file's value for a GOTOOLS_* key (config.Keys) and whether it is set. A layout key the file leaves out is not set; a settings key always has a value, the default when the file says nothing.

func (*Manifest) RemoveTool

func (m *Manifest) RemoveTool(name string) bool

RemoveTool deletes a tool entry, reporting whether it existed.

func (*Manifest) Render

func (m *Manifest) Render() ([]byte, error)

Render produces the manifest bytes in the layout committed files use: fixed key order, 2-space indent, settings alphabetical, tools sorted by source, package, version and name. Layout fields the manifest leaves empty are written as their defaults, and the module prefix defaults to the directory.

func (*Manifest) Set

func (m *Manifest) Set(key, value string) error

Set applies value to a GOTOOLS_* key in memory, after checking it against what the key holds. A value that does not fit is a usage error and leaves the manifest unchanged; Flush writes it.

func (*Manifest) SetTool

func (m *Manifest) SetTool(t Tool)

SetTool adds or replaces a tool entry.

func (*Manifest) SortedTools

func (m *Manifest) SortedTools() []Tool

SortedTools returns the tools in manifest order: source, package, version, then name.

func (*Manifest) Tool

func (m *Manifest) Tool(name string) (Tool, bool)

Tool returns the entry for name.

func (*Manifest) ToolNames

func (m *Manifest) ToolNames() []string

ToolNames lists tool names in store order.

func (*Manifest) Twin

func (m *Manifest) Twin(name, pkg, version string, unified bool) (Tool, bool)

Twin reports another tool that is the same tool as (name, pkg, version): the same package, and either the same version or, when unified is true, any version. The unified strategy shares one go.mod, which holds a single version of a module, so a second name for a package can only be the same tool.

func (*Manifest) Twins

func (m *Manifest) Twins(unified bool) [][]Tool

Twins groups the tools that are one tool under several names (see Twin), in manifest order. Each group has at least two entries.

type NewerSchemaError

type NewerSchemaError struct {
	File      string
	Found     int // the schema version in the file
	Supported int // the schema version this build understands
}

NewerSchemaError reports a manifest written by a newer gotools. It is wrapped in an Environment error.

func (*NewerSchemaError) Error

func (e *NewerSchemaError) Error() string

type ParseResponse

type ParseResponse struct {
	Manifest *Manifest
	// Warnings are problems that did not stop the parse, such as a skipped tool
	// entry with an unsafe name.
	Warnings []string
}

ParseResponse is what Parse and Load return alongside the error.

func Load

func Load(path string) (ParseResponse, error)

Load reads and parses the manifest at path. A missing file is an Environment error matching ErrMissing.

func Parse

func Parse(data []byte, name string) (ParseResponse, error)

Parse decodes manifest bytes. It ignores unknown keys, skips tool entries with unsafe names (reported as warnings), and fills settings the file omits with their defaults. name only feeds error messages. Every error is an Environment error: ErrMalformed for bad JSON or a bad value, and a *NewerSchemaError for a newer schema.

type Status

type Status int

Status is the result of Check.

const (
	Valid       Status = iota // parses, with a strategy
	Missing                   // no manifest file
	NewerSchema               // written by a newer gotools
	Unreadable                // malformed JSON, or no top-level strategy
)

func Check

func Check(path string) (Status, error)

Check is the non-fatal validation for diagnostics: it classifies the manifest at path and returns the error behind any status but Valid.

type Tool

type Tool struct {
	Name    string
	Source  string
	Package string
	Version string
}

Tool is one managed tool entry.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL