-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (51 loc) · 1.23 KB
/
main.go
File metadata and controls
65 lines (51 loc) · 1.23 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
package main
import (
internal "embedcss_compiler/internal"
"embedcss_compiler/logger"
"encoding/json"
"fmt"
)
type Input struct {
Code string
Options internal.CompilerOptions
}
type Result struct {
Code string
Styles string
}
func (r *Result) FromMap(m map[string]interface{}) {
r.Code = m["Code"].(string)
r.Styles = m["Styles"].(string)
}
func (r Result) ToMap() map[string]interface{} {
return map[string]interface{}{
"Code": r.Code,
"Styles": r.Styles,
}
}
func main() {
service := internal.NewService()
service.Command("compile", func(req internal.Request) (map[string]interface{}, error) {
logger.Debug("received command: compile")
if len(req.Args) != 2 {
return nil, fmt.Errorf("compile expected 2 arguments, got %d", len(req.Args))
}
codeArg := req.Args[0]
optionsArg := req.Args[1]
options := &internal.CompilerOptions{}
err := json.Unmarshal([]byte(optionsArg), options)
if err != nil {
return nil, fmt.Errorf("compile failed to parse options: %v", err)
}
newCode, styles, err := internal.Parse(codeArg, options)
if err != nil {
return nil, err
}
return Result{
Code: newCode,
Styles: styles,
}.ToMap(), nil
})
logger.Debug("starting service")
service.Start()
}