-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
87 lines (71 loc) · 1.93 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/exercism/go-representer"
"github.com/namsral/flag"
)
type options struct {
exercise string
solutionPath string
outputPath string
}
func getOptions() (options, bool) {
var opts options
flag.StringVar(&opts.exercise, "exercise", "", "exercise slug (e.g. 'two-fer')")
flag.StringVar(&opts.solutionPath, "solution", "", "path to solution to be processed")
flag.StringVar(&opts.outputPath, "output", "./", "path for output files")
flag.Parse()
if opts.exercise == "" || opts.solutionPath == "" {
args := flag.Args()
if len(args) < 2 {
flag.Usage()
return opts, false
}
opts.exercise, opts.solutionPath = args[0], args[1]
if 2 < len(args) && opts.outputPath == "./" {
opts.outputPath = args[2]
}
}
return opts, true
}
func main() {
opts, ok := getOptions()
if !ok {
os.Exit(3)
}
err := run(opts)
if err != nil {
log.Printf("%+v", err)
os.Exit(1)
}
}
func run(opts options) error {
log.Printf("Creating representation for `%s` exercise in folder `%s`\n", opts.exercise, opts.solutionPath)
reprBts, mappingBts, err := representer.Extract(opts.solutionPath)
if err != nil {
return err
}
return writeOutput(opts, reprBts, mappingBts)
}
func writeOutput(opts options, representation []byte, mapping []byte) error {
representationFile := path.Join(opts.outputPath, "representation.txt")
if err := writeFile(representationFile, representation); err != nil {
return fmt.Errorf("%s: %w", representationFile, err)
}
mappingFile := path.Join(opts.outputPath, "mapping.json")
if err := writeFile(mappingFile, mapping); err != nil {
return fmt.Errorf("%s: %w", representationFile, err)
}
return nil
}
func writeFile(filePath string, bytes []byte) error {
if err := ioutil.WriteFile(filePath, bytes, 0644); err != nil {
return fmt.Errorf("error writing to file: %w", err)
}
log.Printf("Written to %s", filePath)
return nil
}