shortcut.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package shortcut
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "strings"
  6. "imuslab.com/arozos/mod/common"
  7. )
  8. /*
  9. A simple package to better handle shortcuts in ArozOS
  10. Author: tobychui
  11. */
  12. //A shortcut representing struct
  13. type ShortcutData struct {
  14. Type string //The type of shortcut
  15. Name string //The name of the shortcut
  16. Path string //The path of shortcut
  17. Icon string //The icon of shortcut
  18. }
  19. func ReadShortcut(shortcutFile string) (*ShortcutData, error) {
  20. if common.FileExists(shortcutFile) {
  21. content, err := ioutil.ReadFile(shortcutFile)
  22. if err != nil {
  23. return nil, err
  24. }
  25. //Split the content of the shortcut files into lines
  26. fileContent := strings.ReplaceAll(strings.TrimSpace(string(content)), "\r\n", "\n")
  27. lines := strings.Split(fileContent, "\n")
  28. if len(lines) < 4 {
  29. return nil, errors.New("Corrupted Shortcut File")
  30. }
  31. for i := 0; i < len(lines); i++ {
  32. lines[i] = strings.TrimSpace(lines[i])
  33. }
  34. //Render it as shortcut data
  35. result := ShortcutData{
  36. Type: lines[0],
  37. Name: lines[1],
  38. Path: lines[2],
  39. Icon: lines[3],
  40. }
  41. return &result, nil
  42. } else {
  43. return nil, errors.New("File not exists.")
  44. }
  45. }