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)
}
}
Output:
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)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Persist ¶
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 ¶
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.