// Sync encrypts the store content and writes it to the disk
func (s *Store) Sync() error {
	switch s.encryptionOpts.Algorithm {
	case crypto.GPG_ENCRYPTION:
		var f *openpgp.GpgFile
		var err error

		f, err = openpgp.OpenFile(s.path,
			os.O_CREATE|os.O_WRONLY,
			s.encryptionOpts.Passphrase,
			s.Meta.Recipients)
		if err != nil {
			return err
		}
		defer f.Close()

		jsonData, err := s.ToJson()
		if err != nil {
			return err
		}

		_, err = f.Write([]byte(jsonData))
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("Invalid encryption method provided")
	}

	return nil
}
// LoadStore reads and decrypt a trousseau data store from the
// provided path, using the provided encryption options.
func LoadStore(path string, opts *crypto.Options) (*Store, error) {
	var store *Store = NewStore(path, opts)
	var err error = nil

	switch opts.Algorithm {
	case crypto.GPG_ENCRYPTION:
		var f *openpgp.GpgFile
		var jsonData []byte

		f, err = openpgp.OpenFile(path, os.O_RDONLY, opts.Passphrase, opts.Recipients)
		if err != nil {
			return nil, fmt.Errorf("trousseau data store not found (%s)", err.(*os.PathError).Path)
		}
		defer f.Close()

		jsonData, err = f.ReadAll()
		if err != nil {
			return nil, err
		}

		err = store.FromJson(jsonData)
		if err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("Invalid encryption method provided")
	}

	return store, nil
}