-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (56 loc) · 1.6 KB
/
main.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
package main
import (
"fmt"
"os"
"os/exec"
"github.com/jessevdk/go-flags"
)
var (
dockerImage = "raahii/tex-docker:latest"
)
type arguments struct {
Path string `short:"p" long:"path" required:"true" description:"Latex source path to compile (execute latexmk)"`
ContainerName string `short:"n" long:"container-name" description:"Docker container name"`
WatchExp string `short:"w" long:"watch" description:"Process any events whose filename matches the specified POSIX extended regular expression"`
Recursive bool `short:"r" long:"recursive" description:"Watch all subdirectories of the --path directory"`
}
func buildCommand(args *arguments) *exec.Cmd {
// build commands
cmds := []string{"docker", "run", "--rm"}
// volume option
volume := fmt.Sprintf("%s:/home/work", args.Path)
cmds = append(cmds, "--volume")
cmds = append(cmds, volume)
// name option
if len(args.ContainerName) > 0 {
cmds = append(cmds, "--name")
cmds = append(cmds, args.ContainerName)
}
// docker image name
cmds = append(cmds, dockerImage)
// command in docker container
cmds = append(cmds, "tex-docker")
cmds = append(cmds, "--command")
cmds = append(cmds, "latexmk")
if args.WatchExp != "" {
cmds = append(cmds, "--watch")
cmds = append(cmds, args.WatchExp)
}
if args.Recursive {
cmds = append(cmds, "--recursive")
}
// build command
cmd := exec.Command(cmds[0], cmds[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func main() {
var args arguments
if _, err := flags.Parse(&args); err != nil {
return
}
cmd := buildCommand(&args)
cmd.Start()
cmd.Wait()
}