config.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package filesystem
  2. import (
  3. "encoding/json"
  4. "errors"
  5. )
  6. //FileSystem configuration. Append more lines if required.
  7. type FileSystemOption struct {
  8. Name string `json:"name"` //Display name of this device
  9. Uuid string `json:"uuid"` //UUID of this device, e.g. S1
  10. Path string `json:"path"` //Path for the storage root
  11. Access string `json:"access,omitempty"` //Access right, allow {readonly, readwrite}
  12. Hierarchy string `json:"hierarchy"` //Folder hierarchy, allow {public, user}
  13. Automount bool `json:"automount"` //Automount this device if exists
  14. Filesystem string `json:"filesystem,omitempty"` //Support {"ext4","ext2", "ext3", "fat", "vfat", "ntfs"}
  15. Mountdev string `json:"mountdev,omitempty"` //Device file (e.g. /dev/sda1)
  16. Mountpt string `json:"mountpt,omitempty"` //Device mount point (e.g. /media/storage1)
  17. Username string `json:"username,omitempty"` //Username if the storage require auth
  18. Password string `json:"password,omitempty"` //Password if the storage require auth
  19. }
  20. //Parse a list of StorageConfig from the given json content
  21. func loadConfigFromJSON(jsonContent []byte) ([]FileSystemOption, error) {
  22. storageInConfig := []FileSystemOption{}
  23. //Try to parse the JSON content
  24. err := json.Unmarshal(jsonContent, &storageInConfig)
  25. return storageInConfig, err
  26. }
  27. //Validate if the given options are correct
  28. func ValidateOption(options *FileSystemOption) error {
  29. //Check if path exists
  30. if options.Name == "" {
  31. return errors.New("File System Handler name cannot be empty")
  32. }
  33. if options.Uuid == "" {
  34. return errors.New("File System Handler uuid cannot be empty")
  35. }
  36. if !fileExists(options.Path) {
  37. return errors.New("Path not exists, given: " + options.Path)
  38. }
  39. if !inSlice([]string{"readonly", "readwrite"}, options.Access) {
  40. return errors.New("Not supported access mode: " + options.Access)
  41. }
  42. if !inSlice([]string{"user", "public"}, options.Hierarchy) {
  43. return errors.New("Not supported hierarchy: " + options.Hierarchy)
  44. }
  45. if !inSlice([]string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs"}, options.Filesystem) {
  46. return errors.New("Not supported file system type: " + options.Filesystem)
  47. }
  48. if options.Mountpt != "" && !fileExists(options.Mountpt) {
  49. return errors.New("Mount point not exists: " + options.Mountpt)
  50. }
  51. return nil
  52. }