-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
channel.go
68 lines (60 loc) · 1.72 KB
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package nix
import (
"encoding/json"
"strings"
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/style"
)
// ActionLocalChannels completes local channels
//
// nixos (https://nixos.org/channels/nixos-22.05)
// nixpkgs (https://nixos.org/channels/nixpkgs-unstable)
func ActionLocalChannels() carapace.Action {
return carapace.ActionExecCommand("nix-channel", "--list")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)
for _, line := range lines[:len(lines)-1] {
fields := strings.Fields(line)
vals = append(vals, fields[0], strings.Join(fields[1:], " "))
}
return carapace.ActionValuesDescribed(vals...)
})
}
// ActionRemoteChannels completes remote channels
//
// nixos-21.11-small (unmaintained)
// nixos-22.05 (stable)
func ActionRemoteChannels() carapace.Action {
return carapace.ActionExecCommand("curl", "https://prometheus.nixos.org/api/v1/query?query=channel_revision")(func(output []byte) carapace.Action {
var response struct {
Data struct {
Result []struct {
Metric struct {
Channel string
Status string
}
}
}
}
if err := json.Unmarshal(output, &response); err != nil {
return carapace.ActionMessage(err.Error())
}
styleForStatus := func(s string) string {
switch s {
case "rolling":
return style.Blue
case "stable":
return style.Green
case "unmaintained":
return style.Red
default:
return style.Default
}
}
vals := make([]string, 0)
for _, result := range response.Data.Result {
vals = append(vals, result.Metric.Channel, result.Metric.Status, styleForStatus(result.Metric.Status))
}
return carapace.ActionStyledValuesDescribed(vals...)
})
}