-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (122 loc) · 2.91 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"github.com/pkg/errors"
)
type ProgressData struct {
Progress float64 `json:"progress"`
}
type ProgressMessage struct {
Format string `json:"format"`
Version string `json:"version"`
Type string `json:"type"`
Data ProgressData `json:"data"`
}
func setProgress(progress *float64, newValue float64, stdout io.Writer) error {
*progress = newValue
message := ProgressMessage{
Format: "sath",
Version: "1.0",
Type: "progress",
Data: ProgressData{
Progress: *progress,
},
}
jsonData, err := json.Marshal(message)
if err != nil {
return err
}
stdout.Write(jsonData)
stdout.Write([]byte("\n"))
return nil
}
func runVinaDock(stdout io.Writer, program string) error {
cmd := exec.Command(fmt.Sprintf("/vinadock/bin/%s", program), "--config", "/data/config.txt")
config, err := os.ReadFile("/data/config.txt")
if err != nil {
return errors.WithStack(err)
}
fmt.Printf("program: %s\n", program)
fmt.Println(string(config))
stdoutIn, err := cmd.StdoutPipe()
if err != nil {
return errors.WithStack(err)
}
stdoutErr, err := cmd.StderrPipe()
if err != nil {
return errors.WithStack(err)
}
err = cmd.Start()
if err != nil {
return errors.WithStack(err)
}
buf := make([]byte, 1024)
var progress float64
setProgress(&progress, 1.0, stdout)
os.Remove("/data/output.log")
outlog, err := os.OpenFile("/data/output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("can't create sath log file: %+v\n", err)
}
defer outlog.Close()
for {
n, err := stdoutIn.Read(buf[:])
if n > 0 {
d := buf[:n]
for _, b := range d {
if b == byte('*') {
setProgress(&progress, progress+98.0/51.0, stdout)
}
}
if _, err := outlog.Write(d); err != nil {
return errors.WithStack(err)
}
fmt.Print(string(d))
}
if err != nil {
// Read returns io.EOF at the end of file, which is not an error for us
if err == io.EOF {
err = nil
} else {
return errors.WithStack(err)
}
break
}
}
errMsg, _ := io.ReadAll(stdoutErr)
if err = cmd.Wait(); err != nil {
return errors.New(string(errMsg))
}
setProgress(&progress, 100.0, stdout)
return nil
}
func main() {
var program string
flag.StringVar(&program, "program", "", "docking program")
flag.Parse()
os.Remove("/data/sath.log")
stdout, err := os.OpenFile("/data/sath.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("can't create sath log file: %+v\n", err)
}
defer stdout.Close()
os.Remove("/data/sath.err")
stderr, err := os.OpenFile("/data/sath.err", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("can't create sath err file: %+v\n", err)
}
defer stderr.Close()
err = runVinaDock(stdout, program)
if err != nil {
errStr := fmt.Sprintf("%+v\n", err)
fmt.Println(errStr)
stderr.WriteString(errStr)
os.Exit(1)
}
}