github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/menus/fs-menu-store.go (about)

     1  package menus
     2  
     3  // // NewFSMenuStore returns a new FSMenuStore.
     4  // func NewFSMenuStore(fileSystem filesystem.FileSystem, filePath string, registryItems MenuItemList) *FSMenuStore {
     5  // 	ret := &FSMenuStore{
     6  // 		FileSystem:    fileSystem,
     7  // 		FilePath:      filePath,
     8  // 		registryItems: MenuItemList,
     9  // 	}
    10  // 	return ret
    11  // }
    12  
    13  // // FSMenuStore implements Store by loading and saving all menu data to a YAML file.
    14  // // It also can use a registry to provide default menu items that can be edited
    15  // // in order to customize, without those edits being overwritten.
    16  // type FSMenuStore struct {
    17  // 	FileSystem filesystem.FileSystem // filesystem to store to
    18  // 	FilePath   string                // path of file to write to (yaml)
    19  
    20  // 	internalStore *MapMenuStore // our in memory represtation of the menus
    21  
    22  // 	registryItems     MenuItemList    // the registry
    23  // 	registryEditedIDs map[string]bool // the registry items that have been edited
    24  
    25  // 	fileMu   sync.RWMutex // lock around file operations
    26  // 	lastTime time.Time    // last timestamp related to the file, to detect if file changed on disk
    27  // }
    28  
    29  // // // fsMenuStoreFile is what gets yaml marshal/unmarshal'ed from the file
    30  // // type fsMenuStoreFile struct {
    31  // // 	DisplayMenuItem Root `json:"root" yaml:"root"`
    32  // // 	// RegistryEditedIDs []string `json:"registry_edited_ids" yaml:"registry_edited_ids"`
    33  // // }
    34  
    35  // func (s *FSMenuStore) Close() error {
    36  // 	return s.SaveFile()
    37  // }
    38  
    39  // func (s *FSMenuStore) SaveFile() error {
    40  // 	s.fileMu.Lock()
    41  // 	defer s.fileMu.Unlock()
    42  // }
    43  
    44  // func (s *FSMenuStore) LoadFile() error {
    45  // 	s.fileMu.Lock()
    46  // 	defer s.fileMu.Unlock()
    47  
    48  // 	f, err := s.FileSystem.Open(s.FilePath)
    49  // 	if err != nil {
    50  // 		return err
    51  // 	}
    52  // 	defer f.Close()
    53  // 	fb, err := ioutil.ReadAll(f)
    54  // 	if err != nil {
    55  // 		return err
    56  // 	}
    57  
    58  // 	var dmi DisplayMenuItem
    59  
    60  // 	err = yaml.Unmarshal(fb, &dmi)
    61  // 	if err != nil {
    62  // 		return err
    63  // 	}
    64  
    65  // 	m := NewMapMenuStore()
    66  
    67  // 	var handleItem func(i DisplayMenuItem) error
    68  // 	handleItem = func(i DisplayMenuItem, parentID string) error {
    69  
    70  // 		// overwrite the parent menu ID
    71  // 		i.MenuItem.ParentMenuID = parentID
    72  // 		err := m.CreateMenuItem(&i.MenuItem)
    73  // 		if err != nil {
    74  // 			return err
    75  // 		}
    76  
    77  // 		// TODO: handle the additional for registry overrides
    78  
    79  // 		for _, child := range i.Children {
    80  // 			err = handleItem(child, i.MenuID)
    81  // 			if err != nil {
    82  // 				return err
    83  // 			}
    84  // 		}
    85  
    86  // 		return nil
    87  // 	}
    88  
    89  // 	err = handleItem(dmi, "")
    90  // 	if err != nil {
    91  // 		return err
    92  // 	}
    93  
    94  // 	s.internalStore = m
    95  
    96  // 	return nil
    97  // }
    98  
    99  // func (s *FSMenuStore) LoadFileIfChanged() error {
   100  
   101  // 	st, err := s.FileSystem.Stat(s.FilePath)
   102  // 	if err != nil {
   103  // 		return err
   104  // 	}
   105  // 	// file is older than lastTime, we're done here
   106  // 	if !st.ModTime().After(s.lastTime) {
   107  // 		return nil
   108  // 	}
   109  // 	return s.LoadFile()
   110  // }
   111  
   112  // func (s *FSMenuStore) ReadMenuItem(id string) (*MenuItem, error) {
   113  // 	err := s.LoadFileIfChanged()
   114  // 	if err != nil {
   115  // 		return nil, err
   116  // 	}
   117  // 	return s.internalStore.ReadMenuItem(id)
   118  // }
   119  
   120  // func (s *FSMenuStore) CreateMenuItem(mi *MenuItem) error {
   121  // 	err := s.internalStore.CreateMenuItem(mi)
   122  // 	if err != nil {
   123  // 		return err
   124  // 	}
   125  // 	return s.SaveFile()
   126  // }
   127  
   128  // func (s *FSMenuStore) UpdateMenuItem(mi *MenuItem) error {
   129  // 	err := s.internalStore.UpdateMenuItem(mi)
   130  // 	if err != nil {
   131  // 		return err
   132  // 	}
   133  // 	return s.SaveFile()
   134  // }
   135  
   136  // func (s *FSMenuStore) DeleteMenuItem(id string) error {
   137  // 	err := s.internalStore.DeleteMenuItem(id)
   138  // 	if err != nil {
   139  // 		return err
   140  // 	}
   141  // 	return s.SaveFile()
   142  // }
   143  
   144  // func (s *FSMenuStore) FindChildren(id string) ([]string, error) {
   145  // 	err := s.LoadFileIfChanged()
   146  // 	if err != nil {
   147  // 		return nil, err
   148  // 	}
   149  // 	return s.internalStore.FindChildren(id)
   150  // }
   151  
   152  // // func escapeFileName(s string) string {
   153  // // 	var out []byte
   154  // // 	sb := []byte(s)
   155  // // 	for _, b := range sb {
   156  // // 		if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') {
   157  // // 			out = append(out, b)
   158  // // 			continue
   159  // // 		}
   160  // // 		out = append(out, []byte(fmt.Sprintf("_%X", b))...)
   161  // // 	}
   162  // // 	return string(out)
   163  // // }
   164  
   165  // // func unescapeFileName(s string) string {
   166  // // 	var out []byte
   167  // // 	sb := []byte(s)
   168  // // 	for i := 0; i < len(sb); i++ {
   169  // // 		b := sb[i]
   170  // // 		if b != '_' {
   171  // // 			out = append(out, b)
   172  // // 			continue
   173  // // 		}
   174  // // 		if i+2 < len(sb) {
   175  // // 			sbsubstr := string(sb[i+1 : i+3])
   176  // // 			o, _ := hex.DecodeString(sbsubstr)
   177  // // 			out = append(out, o...)
   178  // // 		}
   179  // // 		i += 2
   180  // // 	}
   181  // // 	return string(out)
   182  // // }