-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount.go
More file actions
202 lines (181 loc) · 5.18 KB
/
mount.go
File metadata and controls
202 lines (181 loc) · 5.18 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/coreos/go-systemd/v22/dbus"
"github.com/samber/lo"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func availableMountsForArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if lo.Contains(args, "all") {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
// remove args from the list of available mounts
remotes := getRemotes()
wantedRemotes := make([]string, 0, len(remotes))
for _, remote := range remotes {
if !lo.Contains(args, remote) {
wantedRemotes = append(wantedRemotes, remote)
}
}
return getMountsWithStatus(cmd.Context(), wantedRemotes), cobra.ShellCompDirectiveNoFileComp
}
func getMountsWithStatus(ctx context.Context, remotes []string) []string {
conn, err := dbus.NewUserConnectionContext(ctx)
if err != nil {
log.Fatalln("Failed to start dbus connection:", err)
}
statuses, err := statusServices(ctx, conn, remotes)
if err != nil {
log.Fatalln("Failed to get service status:", err)
}
mounts := make([]string, len(statuses)+1)
mounts[0] = "all\tmount all drives"
for i, status := range statuses {
mounts[i+1] = fmt.Sprintf("%s\t%s", unitNameToDriveName(status.Name), status.ActiveState)
}
return mounts
}
func mount(cmd *cobra.Command, args []string) {
conn, err := dbus.NewUserConnectionContext(cmd.Context())
if err != nil {
log.Fatalln("Failed to start dbus connection:", err)
}
if lo.Contains(args, "all") {
args = getRemotes()
}
for _, arg := range args {
if err := ensureFolderExists(getDriveDataPath(arg)); err != nil {
log.Printf("Failed to create mount path: %v", err)
continue
}
if err := startService(cmd.Context(), conn, arg); err != nil {
log.Printf("Failed to mount drive: %v", err)
continue
}
log.Println("Mounted Drive:", arg)
}
}
func umount(cmd *cobra.Command, args []string) {
conn, err := dbus.NewUserConnectionContext(cmd.Context())
if err != nil {
log.Fatalln("Failed to start dbus connection:", err)
}
if lo.Contains(args, "all") {
args = getRemotes()
}
for _, arg := range args {
if err := stopService(cmd.Context(), conn, arg); err != nil {
log.Printf("Failed to umount drive: %v", err)
continue
}
log.Println("Umounted Drive:", arg)
if umountCmdFlags.Force {
forceUmount(cmd.Context(), arg)
}
}
}
// forceUmount calls fusermount -u to force unmount the drive in addition to stopping the systemd service.
// This doesnt always work, but it is a good last resort.
// Errors are always ignored, as fusermount -u will return an error if the drive is not mounted.
func forceUmount(ctx context.Context, driveName string) {
drivePath := getDriveDataPath(driveName)
exec.CommandContext(ctx, "/bin/fusermount", "-u", drivePath).Run() //nolint:errcheck
}
func list(cmd *cobra.Command, _ []string) {
conn, err := dbus.NewUserConnectionContext(cmd.Context())
if err != nil {
log.Fatalln("Failed to start dbus connection:", err)
}
statuses, err := statusServices(cmd.Context(), conn, getRemotes())
if err != nil {
log.Fatalln("Failed to get service status:", err)
}
if listCmdFlags.JSON {
renderJSON(statuses)
} else if listCmdFlags.YAML {
renderYAML(statuses)
} else {
renderTable(statuses)
}
}
func renderTable(statuses []dbus.UnitStatus) {
rows := make([][]string, len(statuses))
for i, status := range statuses {
var prefix string
switch status.ActiveState {
case "active":
prefix = "✅"
case "failed":
prefix = "☠️"
case "inactive":
prefix = "⬜"
default:
prefix = "❓"
}
rows[i] = []string{
prefix,
status.Name,
status.ActiveState,
getDriveDataPath(unitNameToDriveName(status.Name)),
}
}
re := lipgloss.NewRenderer(os.Stdout)
cellStyle := re.NewStyle().Padding(0, 1)
headerStyle := cellStyle.Bold(true).Align(lipgloss.Center)
t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("2e4b98"))).
StyleFunc(func(row, col int) lipgloss.Style {
switch row {
case table.HeaderRow:
return headerStyle
default:
return cellStyle
}
}).
Headers("Ok?", "Name", "Status", "Mount Path").
Rows(rows...)
fmt.Println()
fmt.Println(t)
fmt.Println()
}
type serviceStatus struct {
Name string
Status string
MountPath string
}
func statusesToServiceStatuses(statuses []dbus.UnitStatus) []serviceStatus {
serviceStatuses := make([]serviceStatus, len(statuses))
for i, status := range statuses {
serviceStatuses[i] = serviceStatus{
Name: unitNameToDriveName(status.Name),
Status: status.ActiveState,
MountPath: getDriveDataPath(unitNameToDriveName(status.Name)),
}
}
return serviceStatuses
}
func renderJSON(statuses []dbus.UnitStatus) {
s := statusesToServiceStatuses(statuses)
jsonData, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Fatalln("Failed to marshal JSON:", err)
}
fmt.Println(string(jsonData))
}
func renderYAML(statuses []dbus.UnitStatus) {
s := statusesToServiceStatuses(statuses)
yamlData, err := yaml.Marshal(s)
if err != nil {
log.Fatalln("Failed to marshal YAML:", err)
}
fmt.Println(string(yamlData))
}