| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | ) |
| 7 | |
| 8 | type class string |
| 9 | |
| 10 | const ( |
| 11 | classKeepBusiness class = "keep-business" |
| 12 | classMigrateHost class = "migrate-host" |
| 13 | classDeleteShell class = "delete-shell" |
| 14 | ) |
| 15 | |
| 16 | // entry is one desktop shell entry point with its migration destination. |
| 17 | type entry struct { |
| 18 | Kind string `json:"kind"` |
| 19 | Name string `json:"name"` |
| 20 | Detail string `json:"detail,omitempty"` |
| 21 | Location string `json:"location,omitempty"` |
| 22 | Class class `json:"class"` |
| 23 | Owner string `json:"owner"` |
| 24 | } |
| 25 | |
| 26 | const ( |
| 27 | kindCommand = "command" |
| 28 | kindNativeCall = "native-call" |
| 29 | kindEvent = "event" |
| 30 | kindFrontendNative = "frontend-native" |
| 31 | kindFrontendEvent = "frontend-event" |
| 32 | kindCSSMarker = "css-marker" |
| 33 | kindPersistence = "persistence" |
| 34 | kindShellFile = "shell-file" |
| 35 | kindArtifact = "artifact" |
| 36 | kindCIJob = "ci-job" |
| 37 | ) |
| 38 | |
| 39 | var kindOrder = []string{ |
| 40 | kindCommand, kindNativeCall, kindEvent, kindFrontendNative, kindFrontendEvent, |
| 41 | kindCSSMarker, kindPersistence, kindShellFile, kindArtifact, kindCIJob, |
| 42 | } |
| 43 | |
| 44 | type inventory struct { |
| 45 | Baseline string `json:"baseline"` |
| 46 | Entries []entry `json:"entries"` |
| 47 | } |
| 48 | |
| 49 | func (inv *inventory) add(e entry) { inv.Entries = append(inv.Entries, e) } |
| 50 | |
| 51 | func (inv *inventory) count() int { return len(inv.Entries) } |
| 52 | |
| 53 | func (inv *inventory) unclassified() []string { |
| 54 | var out []string |
| 55 | for _, e := range inv.Entries { |
| 56 | if e.Class == "" || e.Owner == "" { |
| 57 | out = append(out, fmt.Sprintf("%s %s (%s)", e.Kind, e.Name, e.Location)) |
| 58 | } |
| 59 | } |
| 60 | return out |
| 61 | } |
| 62 | |
| 63 | func (inv *inventory) sorted() []entry { |
| 64 | rank := map[string]int{} |
| 65 | for i, k := range kindOrder { |
| 66 | rank[k] = i |
| 67 | } |
| 68 | out := append([]entry(nil), inv.Entries...) |
| 69 | sort.SliceStable(out, func(i, j int) bool { |
| 70 | if rank[out[i].Kind] != rank[out[j].Kind] { |
| 71 | return rank[out[i].Kind] < rank[out[j].Kind] |
| 72 | } |
| 73 | if out[i].Name != out[j].Name { |
| 74 | return out[i].Name < out[j].Name |
| 75 | } |
| 76 | return out[i].Location < out[j].Location |
| 77 | }) |
| 78 | return out |
| 79 | } |
| 80 | |
| 81 | func (inv *inventory) counts() map[string]map[class]int { |
| 82 | out := map[string]map[class]int{} |
| 83 | for _, e := range inv.Entries { |
| 84 | if out[e.Kind] == nil { |
| 85 | out[e.Kind] = map[class]int{} |
| 86 | } |
| 87 | out[e.Kind][e.Class]++ |
| 88 | } |
| 89 | return out |
| 90 | } |
| 91 |