/
unmount.go
47 lines (43 loc) · 970 Bytes
/
unmount.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
package command
import (
"bytes"
"io/ioutil"
"log"
"os"
"strconv"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewUnmountCommand() *cobra.Command {
sc := &cobra.Command{
Use: "unmount [mountpoint]",
Run: runUnmountCommand,
}
// TODO: need to handle overlapping flag names
//sc.Flags().BoolP("debug", "v", false, "")
viper.BindPFlags(sc.Flags())
return sc
}
func runUnmountCommand(cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatal("Need mountpoint")
}
if _, err := os.Stat("/tmp/dungeonfs.pid"); os.IsNotExist(err) {
log.Fatalf("DungeonFS does not appear to be running\n")
}
data, err := ioutil.ReadFile("/tmp/dungeonfs.pid")
if err != nil {
log.Fatal(err)
}
pid, err := strconv.Atoi(string(bytes.TrimRight(data, "\n")))
if err != nil {
log.Fatal(err)
}
syscall.Kill(pid, syscall.SIGINT)
time.Sleep(1 * time.Second)
if err := os.Remove(args[0]); err != nil {
log.Fatal(err)
}
}