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