cache

package
v0.10.11 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package cache provides a cache mechanism for the app.

Index

Constants

This section is empty.

Variables

View Source
var Get = func() (*Cache, error) {
	p := resolvePath()

	if _, err := os.Stat(p); os.IsNotExist(err) {
		if err := initCacheFile(p); err != nil {
			return nil, errors.Wrap(err, "failed to create a new cache file")
		}
	} else if err != nil {
		return nil, errors.Wrapf(err, "failed to check the existence of cache file '%s'", p)
	}

	f, err := os.Open(p)
	if err != nil {
		return nil, errors.Wrap(err, "failed to open the cache file")
	}
	defer f.Close()

	var c Cache
	if err := decodeTOML(f, &c); err != nil {
		return nil, errors.Wrap(err, "failed to decode loaded cache content")
	}

	if c.Version == "" || c.Version != meta.Version.String() {
		if err := initCacheFile(p); err != nil {
			return nil, errors.Wrap(err, "failed to clear the cache file")
		}
		if _, err := f.Seek(0, 0); err != nil {
			return nil, errors.Wrap(err, "failed to move to the first")
		}
		if err := decodeTOML(f, &c); err != nil {
			return nil, errors.Wrap(err, "failed to decode cache content")
		}
	}

	return &c, nil
}

Get returns loaded cache contents.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Version        string     `toml:"version"`
	UpdateInfo     UpdateInfo `toml:"updateInfo"`
	CommandHistory []string   `default:"" toml:"commandHistory"`

	// SaveFunc is for testing. It will be ignored if it is nil.
	SaveFunc func() error `toml:"-"`
}

Cache represents cached items.

func (*Cache) Save

func (c *Cache) Save() error

Save writes the receiver to the cache file. It returns an *os.PathError if it can't create a new cache file. Also it returns an error if it failed to encode *Cache with TOML format.

type MeansType

type MeansType updater.MeansType
const MeansTypeUndefined MeansType = ""

type UpdateInfo added in v0.7.3

type UpdateInfo struct {
	LatestVersion string    `default:"" toml:"latestVersion"`
	InstalledBy   MeansType `default:"" toml:"installedBy"`
}

func (UpdateInfo) UpdateAvailable added in v0.7.3

func (i UpdateInfo) UpdateAvailable() bool

Jump to

Keyboard shortcuts

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