This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
distrobox.go
152 lines (129 loc) · 3.19 KB
/
distrobox.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
type distroboxFinishedMsg struct{ err error }
type distroboxItem struct {
id string
name string
status string
image string
}
type OCICmdOutput struct {
Id string `json:"ID"`
Image string `json:"Image"`
Labels string `json:"Labels"`
Mounts string `json:"Mounts"`
Names string `json:"Names"`
Status string `json:"Status"`
}
func clearScreen() tea.Cmd {
cmd := exec.Command("clear")
return tea.ExecProcess(cmd, func(err error) tea.Msg {
return distroboxFinishedMsg{err}
})
}
func enterDistroBox(name string) tea.Cmd {
dbCmd := fmt.Sprintf("clear && distrobox enter %s", name)
cmd := exec.Command("/bin/sh", "-c", dbCmd)
return tea.ExecProcess(cmd, func(err error) tea.Msg {
return distroboxFinishedMsg{err}
})
}
func removeDistroBox(name string) tea.Cmd {
devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0o755)
if err != nil {
log.Fatalln(err)
}
cmd := exec.Command("distrobox", "rm", name, "--force")
cmd.Stdout = devnull
return tea.ExecProcess(cmd, func(err error) tea.Msg {
return distroboxFinishedMsg{err}
})
}
func stopDistroBox(name string) tea.Cmd {
devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0o755)
if err != nil {
log.Fatalln(err)
}
cmd := exec.Command("distrobox", "stop", name, "--yes")
cmd.Stdout = devnull
return tea.ExecProcess(cmd, func(err error) tea.Msg {
return distroboxFinishedMsg{err}
})
}
func getOCICmd() string {
podmanExists := true
dockerExists := true
podmanCmd, err := exec.LookPath("podman")
if err != nil {
podmanExists = false
}
dockerCmd, err := exec.LookPath("docker")
if err != nil {
dockerExists = false
}
if podmanExists {
return podmanCmd
} else if dockerExists {
return dockerCmd
} else {
return ""
}
}
func getDistroboxItems() (items []distroboxItem) {
ociCmd := getOCICmd()
if ociCmd == "" {
log.Fatalln("Missing dependency: we need a container manager. Please install one of podman or docker.")
}
var outputs []OCICmdOutput
rawOutput, _ := exec.Command(ociCmd, "ps", "-a", "--format", "json", "--no-trunc").Output()
for _, line := range bytes.Split(rawOutput, []byte{'\n'}) {
if string(line) == "" {
continue
}
var ociCmdOutput OCICmdOutput
if err := json.Unmarshal(line, &ociCmdOutput); err != nil {
log.Fatalln(err)
}
outputs = append(outputs, ociCmdOutput)
for _, mount := range strings.Split(ociCmdOutput.Mounts, ",") {
if strings.Contains(mount, "/distrobox-export") {
box := distroboxItem{
id: ociCmdOutput.Id[:12],
name: ociCmdOutput.Names,
status: ociCmdOutput.Status,
image: ociCmdOutput.Image,
}
items = append(items, box)
break
}
}
}
if len(items) == 0 {
for _, jsonElem := range outputs {
labels := strings.Split(jsonElem.Labels, ",")
for _, label := range labels {
if label == "manager=distrobox" {
log.Printf("%+v\n", jsonElem)
box := distroboxItem{
id: jsonElem.Id[:12],
name: jsonElem.Names,
status: jsonElem.Status,
image: jsonElem.Image,
}
items = append(items, box)
break
}
}
}
}
return items
}