Fix recomputing diffs N times.
[lightning.git] / cmd.go
1 package lightning
2
3 import (
4         "fmt"
5         "io"
6         "io/ioutil"
7         "os"
8         "os/exec"
9
10         "git.arvados.org/arvados.git/lib/cmd"
11         "github.com/mattn/go-isatty"
12         "github.com/sirupsen/logrus"
13 )
14
15 var (
16         handler = cmd.Multi(map[string]cmd.Handler{
17                 "version":   cmd.Version,
18                 "-version":  cmd.Version,
19                 "--version": cmd.Version,
20
21                 "ref2genome":         &ref2genome{},
22                 "vcf2fasta":          &vcf2fasta{},
23                 "import":             &importer{},
24                 "annotate":           &annotatecmd{},
25                 "export":             &exporter{},
26                 "export-numpy":       &exportNumpy{},
27                 "flake":              &flakecmd{},
28                 "numpy-comvar":       &numpyComVar{},
29                 "filter":             &filtercmd{},
30                 "build-docker-image": &buildDockerImage{},
31                 "pca-go":             &goPCA{},
32                 "pca-py":             &pythonPCA{},
33                 "plot":               &pythonPlot{},
34                 "diff-fasta":         &diffFasta{},
35                 "stats":              &statscmd{},
36                 "merge":              &merger{},
37                 "dumpgob":            &dumpGob{},
38         })
39 )
40
41 func Main() {
42         if !isatty.IsTerminal(os.Stderr.Fd()) {
43                 logrus.StandardLogger().Formatter = &logrus.TextFormatter{DisableTimestamp: true}
44         }
45         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
46 }
47
48 type buildDockerImage struct{}
49
50 func (cmd *buildDockerImage) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
51         tmpdir, err := ioutil.TempDir("", "")
52         if err != nil {
53                 fmt.Fprint(stderr, err)
54                 return 1
55         }
56         defer os.RemoveAll(tmpdir)
57         err = ioutil.WriteFile(tmpdir+"/Dockerfile", []byte(`FROM debian:bullseye
58 RUN DEBIAN_FRONTEND=noninteractive \
59   apt-get update && \
60   apt-get dist-upgrade -y && \
61   apt-get install -y --no-install-recommends bcftools bedtools samtools python2 python3-sklearn python3-matplotlib ca-certificates && \
62   apt-get clean
63 `), 0644)
64         if err != nil {
65                 fmt.Fprint(stderr, err)
66                 return 1
67         }
68         docker := exec.Command("docker", "build", "--tag=lightning-runtime", tmpdir)
69         docker.Stdout = stdout
70         docker.Stderr = stderr
71         err = docker.Run()
72         if err != nil {
73                 return 1
74         }
75         fmt.Fprintf(stderr, "built and tagged new docker image, lightning-runtime\n")
76         return 0
77 }