-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·193 lines (175 loc) · 5.21 KB
/
main.go
File metadata and controls
executable file
·193 lines (175 loc) · 5.21 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"slices"
"sync"
"github.com/Nydauron/avocado2sciolyff/parsers"
"github.com/Nydauron/avocado2sciolyff/sciolyff"
"github.com/Nydauron/avocado2sciolyff/writers"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
const (
inputOverallFlag = "inputOverall"
inputGroupFlag = "inputGroup"
outputFlag = "output"
csvFlag = "csv"
stdoutCLIName = "-"
)
var build string
var semanticVersion = "v0.2.0-dev" + build
func cliHandle(inputLocation string, inputByGroupLocation string, outputWriter io.Writer, isCSVFile bool) error {
extractData := func(fileLocation string) (*parsers.Table, error) {
var htmlBodyReader io.ReadCloser
if u, err := url.ParseRequestURI(fileLocation); err == nil {
if !slices.Contains([]string{"http", "https"}, u.Scheme) {
return nil, fmt.Errorf("URL is not of HTTP schema (got %q instead)", u.Scheme)
}
fmt.Fprintln(os.Stderr, "URL detected")
rawURL := u.String()
resp, err := http.Get(rawURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error occurred when trying to fetch page: %v\n", err)
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("invalid HTTP status code received: %v", resp.Status)
}
defer resp.Body.Close()
contentType := resp.Header.Get("content-type")
expectedContent := "text/html; charset=UTF-8"
if contentType != expectedContent {
fmt.Fprintf(os.Stderr, "Page content recieved is not text/html UTF-8. Got instead %q\n", contentType)
}
htmlBodyReader = resp.Body
} else if f, err := os.Open(fileLocation); err == nil {
fmt.Fprintln(os.Stderr, "File detected")
defer f.Close()
htmlBodyReader = f
} else {
return nil, fmt.Errorf("provided input was neither a valid URL or a path to existing file: %v", fileLocation)
}
var table *parsers.Table
if isCSVFile {
var err error
table, err = parsers.ParseCSV(htmlBodyReader)
if err != nil {
fmt.Fprintf(os.Stderr, "Cell did not contain number: %v\n", err)
os.Exit(4)
}
} else {
var err error
table, err = parsers.ParseHTML(htmlBodyReader)
if err != nil {
fmt.Fprintf(os.Stderr, "Cell did not contain number: %v\n", err)
os.Exit(4)
}
}
return table, nil
}
var overallResTable *parsers.Table = nil
var groupResTable *parsers.Table = nil
err_ch := make(chan error, 2)
continue_ch := make(chan struct{})
wg := sync.WaitGroup{}
dataParser := func(err_channel chan<- error, inputPath string, table **parsers.Table) {
t, err := extractData(inputPath)
*table = t
if err != nil {
err_channel <- err
return
}
wg.Done()
}
wg.Add(1)
go dataParser(err_ch, inputLocation, &overallResTable)
if inputByGroupLocation != "" {
wg.Add(1)
go dataParser(err_ch, inputByGroupLocation, &groupResTable)
}
go func() {
defer close(continue_ch)
wg.Wait()
}()
select {
case err := <-err_ch:
fmt.Fprintf(os.Stderr, "Error during parsing: %v", err)
return err
case <-continue_ch:
}
sciolyffDump := sciolyff.GenerateSciolyFF(*overallResTable, groupResTable)
outputWriter.Write([]byte("###\n# This YAML file was auto-generated by avocado2sciolyff " + semanticVersion + "\n###\n"))
yamlEncoder := yaml.NewEncoder(outputWriter)
yamlEncoder.SetIndent(2)
err := yamlEncoder.Encode(&sciolyffDump)
if err != nil {
fmt.Fprintf(os.Stderr, "Encoding to YAML failed: %v", err)
os.Exit(3)
return nil
}
err = yamlEncoder.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Encoding to YAML failed on close: %v", err)
os.Exit(3)
return nil
}
return nil
}
func main() {
var inputOverallLocation string
inputByGroupLocation := ""
outputLocation := ""
isCSV := false
app := &cli.App{
Name: "avocado2sciolyff",
Usage: "A tool to turn table results on Avogadro to sciolyff results",
Version: semanticVersion,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: csvFlag,
Usage: "File passed in is a CSV rather than an HTML file",
Destination: &isCSV,
},
&cli.StringFlag{
Name: inputOverallFlag,
Aliases: []string{"iO"},
Usage: "The URL or path to the HTML file containing the table of overall results to convert",
Destination: &inputOverallLocation,
Required: true,
},
&cli.StringFlag{
Name: inputGroupFlag,
Aliases: []string{"iG"},
Usage: "The URL or path to the HTML file containing the table of results by grouping/track to convert",
Destination: &inputByGroupLocation,
},
&cli.StringFlag{
Name: outputFlag,
Aliases: []string{"o"},
Usage: "The location to write the YAML result. Can be a file path or \"-\" (for stdout).",
Required: true,
Destination: &outputLocation,
},
},
Action: func(cCtx *cli.Context) error {
if outputLocation == "" {
return fmt.Errorf("output not set")
}
var outputWriter io.WriteCloser = os.Stdout
if outputLocation != stdoutCLIName {
outputWriter = writers.NewLazyWriteCloser(func() (io.WriteCloser, error) {
return os.OpenFile(outputLocation, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
})
}
return cliHandle(inputOverallLocation, inputByGroupLocation, outputWriter, isCSV)
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}