package plugin

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/pkg/errors"
)

type metaDataStore struct {
	dir           string
	installTarget *installTarget
}

var errDisaleMetaDataStore = errors.New("MetaData disabled. could not detect owner/repo")

func newMetaDataStore(pluginDir string, target *installTarget) (*metaDataStore, error) {
	owner, repo, err := target.getOwnerAndRepo()
	if err != nil {
		return nil, errDisaleMetaDataStore
	}
	dir := filepath.Join(pluginDir, "meta", owner, repo)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return nil, err
	}
	return &metaDataStore{
		dir:           dir,
		installTarget: target,
	}, nil
}

func (m *metaDataStore) load(key string) (string, error) {
	f, err := os.OpenFile(
		filepath.Join(m.dir, key),
		os.O_RDONLY|os.O_CREATE,
		0644,
	)
	if err != nil {
		return "", err
	}
	defer f.Close()
	b, err := ioutil.ReadAll(f)
	return string(b), err
}

func (m *metaDataStore) store(key, value string) error {
	return ioutil.WriteFile(
		filepath.Join(m.dir, key),
		[]byte(value),
		0644,
	)
}

package plugin

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/pkg/errors"
)

type metaDataStore struct {
	dir           string
	installTarget *installTarget
}

var errDisaleMetaDataStore = errors.New("MetaData disabled. could not detect owner/repo")

func newMetaDataStore(pluginDir string, target *installTarget) (*metaDataStore, error) {
	owner, repo, err := target.getOwnerAndRepo()
	if err != nil {
		return nil, errDisaleMetaDataStore
	}
	dir := filepath.Join(pluginDir, "meta", owner, repo)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return nil, err
	}
	return &metaDataStore{
		dir:           dir,
		installTarget: target,
	}, nil
}

func (m *metaDataStore) load(key string) (string, error) {
	b, err := ioutil.ReadFile(filepath.Join(m.dir, key))
	if os.IsNotExist(err) {
		return "", nil
	} else if err != nil {
		return "", err
	}
	return string(b), nil
}

func (m *metaDataStore) store(key, value string) error {
	return ioutil.WriteFile(
		filepath.Join(m.dir, key),
		[]byte(value),
		0644,
	)
}

Use ioutil.ReadFile instead of os.Openfile with O_CREATE.
Use ioutil.ReadFile instead of os.Openfile with O_CREATE.

No need to create an empty file if meta data key is not found.
