-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathapp.go
125 lines (104 loc) · 2.39 KB
/
app.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
package app
import (
"bufio"
"fmt"
"log"
"net/url"
"os"
"strings"
"github.com/bjesus/pipet/common"
"github.com/bjesus/pipet/parsers"
"github.com/google/shlex"
)
func ParseSpecFile(e *common.PipetApp, filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var currentBlock *common.Block
for scanner.Scan() {
line := scanner.Text()
if line == "" {
if currentBlock != nil {
e.Blocks = append(e.Blocks, *currentBlock)
currentBlock = nil
}
continue
}
if strings.HasPrefix(line, "//") {
continue
}
if currentBlock == nil {
if strings.HasPrefix(line, "curl ") {
currentBlock = &common.Block{Type: "curl", Command: line}
} else if strings.HasPrefix(line, "playwright ") {
currentBlock = &common.Block{Type: "playwright", Command: line}
} else {
return fmt.Errorf("invalid block start: %s", line)
}
} else {
if strings.HasPrefix(line, "> ") {
currentBlock.NextPage = strings.TrimPrefix(line, ">")
} else {
currentBlock.Queries = append(currentBlock.Queries, line)
}
}
}
log.Println("Found block", currentBlock)
if currentBlock != nil {
e.Blocks = append(e.Blocks, *currentBlock)
}
return scanner.Err()
}
func ExecuteBlocks(e *common.PipetApp) error {
for _, block := range e.Blocks {
var data interface{}
var err error
var nextPageURL string
for page := 0; page < e.MaxPages; page++ {
if block.Type == "curl" {
data, nextPageURL, err = parsers.ExecuteCurlBlock(block)
} else if block.Type == "playwright" {
data, err = parsers.ExecutePlaywrightBlock(block)
}
if err != nil {
return err
}
e.Data = append(e.Data, data)
if nextPageURL == "" {
break
}
var parts []string
switch cmd := block.Command.(type) {
case string:
parts, _ = shlex.Split(cmd)
case []string:
parts = cmd
default:
}
for i, u := range parts {
if len(u) >= 4 && u[:4] == "http" {
parts[i] = concatenateURLs(parts[i], nextPageURL)
break
}
}
block.Command = parts
}
}
return nil
}
func concatenateURLs(base, ref string) string {
baseURL, err := url.Parse(base)
if err != nil {
panic(err)
}
refURL, err := url.Parse(ref)
if err != nil {
panic(err)
}
// Resolve reference URL relative to the base URL
fullURL := baseURL.ResolveReference(refURL)
return fullURL.String()
}