main.go 1.62 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
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
package main

import (
        "os"
        "os/exec"
        "strings"
        "syscall"
)

func isWritable(path string) bool {
        return syscall.Access(path, syscall.O_RDWR) == nil
}

func fixVolumeArg(arg string) string {
        parts := strings.Split(arg, ":")
        if len(parts) < 2 {
                return arg
        }
        hostPath := parts[0]

        writable := isWritable(hostPath)

        hasMode := len(parts) == 3

        if !writable {
                if hasMode {
                        parts[len(parts)-1] = "ro"
                        return strings.Join(parts, ":")
                }
                return arg + ":ro"
        }

        return arg
}
func HasRunSubcommand(args [] string) bool {
     isrun := false
     for _, arg := range args {
         if arg == "run"{
            isrun = true
            return isrun
         }
     }
     return isrun

}
func main() {
songlinfeng's avatar
songlinfeng committed
47
        runtime := os.Args[0]
songlinfeng's avatar
songlinfeng committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        args := os.Args[1:]
        newArgs := []string{}
        if !HasRunSubcommand(args) {
           newArgs = args

        }else {
           for i := 0; i < len(args); i++ {
               if args[i] == "-v" && i + 1 < len(args) {
                   fixed := fixVolumeArg(args[i+1])
                   newArgs = append(newArgs, "-v", fixed)
                   i++
               } else {
                   newArgs = append(newArgs, args[i])
               }
           }
        }

songlinfeng's avatar
songlinfeng committed
65
        cmd := exec.Command("s"+runtime, newArgs...)
songlinfeng's avatar
songlinfeng committed
66
67
68
69
70
71
72
73
        cmd.Stdin = os.Stdin
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr

        if err := cmd.Run(); err != nil {
                os.Exit(1)
        }
}