|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/hbollon/go-edlib" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "gopkg.in/ini.v1" |
| 12 | +) |
| 13 | + |
| 14 | +func getApps() ([]string, error) { |
| 15 | + cmd := exec.Command("flatpak", "list", "--app") |
| 16 | + cmd.Dir = "/var/lib/flatpak" |
| 17 | + |
| 18 | + log.Debug().Str("command", cmd.Path+strings.Join(cmd.Args, " ")).Msg("Running command") |
| 19 | + out, err := cmd.Output() |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + appsLine := strings.Split(string(out), "\n") |
| 25 | + var apps []string |
| 26 | + for _, app := range appsLine { |
| 27 | + app = strings.TrimSpace(app) |
| 28 | + if app == "" { |
| 29 | + continue |
| 30 | + } |
| 31 | + data := strings.Split(app, "\t") |
| 32 | + if len(data) < 2 { |
| 33 | + log.Warn().Msgf("Skipping line: %s", app) |
| 34 | + continue |
| 35 | + } |
| 36 | + appID := strings.TrimSpace(data[1]) |
| 37 | + if appID == "" { |
| 38 | + continue |
| 39 | + } |
| 40 | + log.Trace().Msgf("AppID: %s", appID) |
| 41 | + apps = append(apps, appID) |
| 42 | + } |
| 43 | + return apps, nil |
| 44 | +} |
| 45 | + |
| 46 | +func getFlatpakApp(appID string) (Flatpak, error) { |
| 47 | + cmd := exec.Command("flatpak", "info", "-m", appID) |
| 48 | + |
| 49 | + log.Debug().Str("command", cmd.Path+strings.Join(cmd.Args, " ")).Str("appID", appID).Msg("Running command") |
| 50 | + out, err := cmd.Output() |
| 51 | + if err != nil { |
| 52 | + return Flatpak{}, err |
| 53 | + } |
| 54 | + |
| 55 | + log.Trace().Str("data", string(out)).Msg("Parsing toml") |
| 56 | + var flatpak Flatpak |
| 57 | + cfg, err := ini.Load(out) |
| 58 | + if err != nil { |
| 59 | + return Flatpak{}, err |
| 60 | + } |
| 61 | + err = cfg.MapTo(&flatpak) |
| 62 | + if err != nil { |
| 63 | + return Flatpak{}, err |
| 64 | + } |
| 65 | + |
| 66 | + flatpak.Application.Command = strings.TrimSpace(flatpak.Application.Command) |
| 67 | + |
| 68 | + rank := edlib.LevenshteinDistance(flatpak.Application.Command, appID) |
| 69 | + if rank <= 0 { |
| 70 | + if rank < 0 { |
| 71 | + log.Warn().Str("appID", appID).Str("command", flatpak.Application.Command).Int("rank", rank).Msg("AppID and AppName do not match") |
| 72 | + } else { |
| 73 | + log.Debug().Str("appID", appID).Str("command", flatpak.Application.Command).Int("rank", rank).Msg("AppID and AppName are the same") |
| 74 | + } |
| 75 | + flatpak.Application.Command = strings.ToLower(appID[strings.LastIndex(appID, ".")+1:]) |
| 76 | + } else { |
| 77 | + log.Debug().Str("appID", appID).Str("command", flatpak.Application.Command).Int("rank", rank).Msg("AppID and AppName match") |
| 78 | + } |
| 79 | + |
| 80 | + flatpak.Application.Command = strings.ToLower(flatpak.Application.Command) |
| 81 | + |
| 82 | + return flatpak, nil |
| 83 | +} |
| 84 | + |
| 85 | +func removeDuplicates(apps []Flatpak) []Flatpak { |
| 86 | + keys := make(map[string]*Flatpak) |
| 87 | + list := []Flatpak{} |
| 88 | + duplicates := make(map[string][]Flatpak) |
| 89 | + for _, app := range apps { |
| 90 | + if f, ok := keys[app.Application.Command]; !ok { |
| 91 | + keys[app.Application.Command] = &app |
| 92 | + list = append(list, app) |
| 93 | + } else { |
| 94 | + if len(duplicates[app.Application.Command]) == 0 { |
| 95 | + duplicates[app.Application.Command] = append(duplicates[app.Application.Command], *f, app) |
| 96 | + } else { |
| 97 | + duplicates[app.Application.Command] = append(duplicates[app.Application.Command], app) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return slices.DeleteFunc(list, func(app Flatpak) bool { |
| 102 | + apps, ok := duplicates[app.Application.Command] |
| 103 | + if ok { |
| 104 | + log.Warn().Str("command", app.Application.Command).Interface("apps", apps).Msg("Found duplicates") |
| 105 | + } |
| 106 | + return ok |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func getAllFlatpakApps() []Flatpak { |
| 111 | + appsID, err := getApps() |
| 112 | + if err != nil { |
| 113 | + log.Fatal().Err(err).Msg("Error getting apps") |
| 114 | + } |
| 115 | + |
| 116 | + apps := make([]Flatpak, len(appsID)) |
| 117 | + wg := sync.WaitGroup{} |
| 118 | + for i, appID := range appsID { |
| 119 | + wg.Add(1) |
| 120 | + go func(appID string) { |
| 121 | + defer wg.Done() |
| 122 | + app, err := getFlatpakApp(appID) |
| 123 | + if err != nil { |
| 124 | + log.Error().Err(err).Str("appID", appID).Msg("Error getting app") |
| 125 | + } |
| 126 | + // No need to lock since we are writing to different indexes |
| 127 | + apps[i] = app |
| 128 | + }(appID) |
| 129 | + } |
| 130 | + wg.Wait() |
| 131 | + |
| 132 | + apps = removeDuplicates(apps) |
| 133 | + |
| 134 | + return apps |
| 135 | +} |
0 commit comments