Newer
Older
"os"
"path/filepath"
"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
const (
pathToStores string = "stores"
)
// FromString is a helper to check if a provided string as a valid UUID or a name.
func FromString(id string) (uuid.UUID, error) {
idAsUUID, err := uuid.Parse(id)
// id is no UUID therefore it could be a device name.
// The name will be returned within the error.
if err != nil {
log.WithFields(log.Fields{
"identifier": id,
}).Debug(err)
return uuid.Nil, &errors.ErrInvalidUUID{DeviceName: id}
}
return idAsUUID, nil
}
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
func ensureFilesystemStorePathExists(storeFileName string) error {
completeStorePath := filepath.Join(pathToStores, storeFileName)
if _, err := os.Stat(completeStorePath); os.IsNotExist(err) {
err := ensureFileSystemStoreExists(completeStorePath)
if err != nil {
return err
}
}
return nil
}
func ensureFileSystemStoreExists(pathToStore string) error {
err := ensureDirExists(pathToStore)
if err != nil {
return err
}
emptyArray := []byte("[]")
err = os.WriteFile(pathToStore, emptyArray, 0600)
if err != nil {
return err
}
return nil
}
func ensureDirExists(fileName string) error {
dirName := filepath.Dir(fileName)
if _, serr := os.Stat(dirName); serr != nil {
merr := os.MkdirAll(dirName, os.ModePerm)
if merr != nil {
return merr
}
}
return nil
}
func getCompletePathToFileStore(storeName string) string {
return filepath.Join(pathToStores, storeName)