Merge pull request #79 from sqshq/remove_metadata_storage
Remove metadata storage
This commit is contained in:
commit
e44776a526
|
@ -1,59 +0,0 @@
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
macOSDir = "/Library/Application Support/Sampler"
|
||||
linuxDir = "/.config/Sampler"
|
||||
windowsDir = "Sampler"
|
||||
)
|
||||
|
||||
func fileExists(filename string) bool {
|
||||
_, err := os.Stat(getPlatformStoragePath(filename))
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func getPlatformStoragePath(filename string) string {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
home, _ := homedir.Dir()
|
||||
return filepath.Join(home, macOSDir, filename)
|
||||
case "windows":
|
||||
cache, _ := os.UserCacheDir()
|
||||
return filepath.Join(cache, windowsDir, filename)
|
||||
default:
|
||||
home, _ := homedir.Dir()
|
||||
return filepath.Join(home, linuxDir, filename)
|
||||
}
|
||||
}
|
||||
|
||||
func initStorage() {
|
||||
err := os.MkdirAll(getPlatformStoragePath(""), os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to init storage: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func readStorageFile(path string) []byte {
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to the read storage file: %s", path)
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
func saveStorageFile(file []byte, fileName string) {
|
||||
err := ioutil.WriteFile(getPlatformStoragePath(fileName), file, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to save the storage file: %s %v", fileName, err)
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type f struct {
|
||||
a int
|
||||
}
|
||||
|
||||
func Test_fileExists(t *testing.T) {
|
||||
|
||||
initStorage()
|
||||
|
||||
_, err := os.Create(getPlatformStoragePath("exists"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer os.Remove(getPlatformStoragePath("exists"))
|
||||
|
||||
type args struct {
|
||||
filename string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{"should verify that file does not exist", args{"does-not-exist"}, false},
|
||||
{"should verify that file exists", args{"exists"}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := fileExists(tt.args.filename); got != tt.want {
|
||||
t.Errorf("fileExists() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_saveStorageFile(t *testing.T) {
|
||||
|
||||
initStorage()
|
||||
|
||||
file, _ := yaml.Marshal(f{a: 1})
|
||||
name := "test"
|
||||
|
||||
saveStorageFile(file, name)
|
||||
|
||||
read, _ := ioutil.ReadFile(getPlatformStoragePath(name))
|
||||
|
||||
if !bytes.Equal(file, read) {
|
||||
t.Errorf("read file != saved file")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_readStorageFile(t *testing.T) {
|
||||
|
||||
initStorage()
|
||||
|
||||
file, _ := yaml.Marshal(f{a: 1})
|
||||
name := "test"
|
||||
|
||||
err := ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
read := readStorageFile(getPlatformStoragePath(name))
|
||||
|
||||
if !bytes.Equal(file, read) {
|
||||
t.Errorf("read file != saved file")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue