persist

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Persist

Save one Go value as JSON between runs. Use it for CLI preferences or small configuration structs.

go get github.com/clmncol/persist
type Config struct {
	Profile string
	Verbose bool
}

store := persist.Expect("mycli-config", Config{Profile: "default"})
cfg, err := store.Pull()
if err != nil {
	return err
}

cfg.Profile = "work" // Keep the other settings.
return store.Push(cfg)

See example_test.go for complete struct and single-value examples.

Pull returns the supplied default (or the type’s zero value) when the file is missing. Push replaces expect/<name>.expect.json under os.UserConfigDir() using a temp file and rename. Names can’t contain / or \. There’s no locking.

Documentation

Overview

Example

A full config example.

package main

import (
	"github.com/clmncol/persist"
)

func main() {
	type Config struct {
		Profile string `json:"profile"`
		Verbose bool   `json:"verbose"`
	}

	store := persist.Expect("mycli-config", Config{Profile: "default"})

	cfg, err := store.Pull()
	if err != nil {
		panic(err)
	}

	// Change one preference without losing the others.
	cfg.Profile = "work"
	if err := store.Push(cfg); err != nil {
		panic(err)
	}
}
Example (SingleValue)

A single value example.

package main

import (
	"github.com/clmncol/persist"
)

func main() {
	// Inside your command:
	p := persist.Expect[string]("mycli-profile", "default")
	profile, err := p.Pull()
	if err != nil {
		panic(err)
	}
	_ = profile // use profile, then save the new selection.
	if err := p.Push("work"); err != nil {
		panic(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Persist

type Persist[T any] struct {
	Name  string
	Value T
	// contains filtered or unexported fields
}

Persist is a generic, file-backed persistent config value. Values are stored as JSON in the "expect" subdirectory of os.UserConfigDir() in a file named "<name>.expect.json".

func Expect

func Expect[T any](name string, defaults ...T) *Persist[T]

Expect establishes a new persist handle for the given config name. The file "<name>.expect.json" is created in the "expect" subdirectory of os.UserConfigDir() on Push. The name must not contain path separators; Pull and Push will return an error if it does. If a default is provided, it is returned by Pull when no file exists yet.

func (*Persist[T]) Pull

func (p *Persist[T]) Pull() (T, error)

Pull returns the value stored in the persist file. If the file does not exist yet, Pull returns the default value (or the zero value of T if none was given to Expect) with no error. The file itself is left untouched.

func (*Persist[T]) Push

func (p *Persist[T]) Push(v T) error

Push overwrites the persist file with v.

Jump to

Keyboard shortcuts

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