-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_grep.go
More file actions
156 lines (145 loc) · 4.7 KB
/
process_grep.go
File metadata and controls
156 lines (145 loc) · 4.7 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
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
)
// wants revision to be able to use more than 1 capture group from a regular expression...
type StageResult string
type StageResults []StageResult
type MultipleStageResults map[int][]StageResults
func OpenFile(step grep_step, mPriorStageResult *StageResult) (*os.File, string, error) {
var filename string
patt := regexp.MustCompile("(.*)(<select_ref>)(.*)")
results := patt.FindAllStringSubmatch(step.OutputFilenameTemplate, -1)
if len(results) > 0 {
result := results[0]
filename = string(result[1]) + string(*mPriorStageResult) + string(result[3])
} else {
filename = step.OutputFilenameTemplate
}
mfile, err := os.Create(filename)
if err != nil {
fmt.Printf("Unable to create file %s\n", filename)
return nil, filename, err
}
return mfile, filename, nil
}
func OutputToFile(step grep_step, cmdoutput []byte, mPriorStageResult *StageResult) {
var endindex int
var byte byte
fmt.Printf("Output:\n%s\n", cmdoutput)
startindex := 0
mfile, _, err := OpenFile(step, mPriorStageResult)
defer mfile.Close()
if err != nil {
fmt.Printf("Problem creating file\n")
}
for endindex, byte = range cmdoutput {
if byte == '\n' {
nBytesWritten, err := mfile.Write(cmdoutput[startindex : endindex+1])
if err != nil {
fmt.Printf("Problem writing file\n")
}
if nBytesWritten == 0 {
fmt.Printf("Problem writing file\n")
}
startindex = endindex + 1
}
endindex++
}
}
func GetResults(mGrepPattern string, step grep_step, mPriorStageResult *StageResult) (stageresults StageResults) {
cmdoutput, err := exec.Command("grep", mGrepPattern, step.InputFile).Output()
if err != nil {
switch e := err.(type) {
case *exec.Error:
fmt.Println("failed executing:", err)
case *exec.ExitError:
fmt.Println("command exit rc =", e.ExitCode())
default:
panic(err)
}
}
if step.Output == "Memory" {
var endindex int
var byte byte
fmt.Printf("Output:\n%s\n", cmdoutput)
startindex := 0
for endindex, byte = range cmdoutput {
if byte == '\n' {
stageresults = append(stageresults, StageResult(cmdoutput[startindex:endindex]))
startindex = endindex + 1
}
endindex++
}
} else if step.Output == "File" {
OutputToFile(step, cmdoutput, mPriorStageResult)
}
return stageresults
}
func do_grep(step grep_step, mPriorStageResult *StageResult) *StageResults {
var mstageresults StageResults
var mGrepPattern string
if len(step.Select_Ref) > 0 {
// doesn't actually use the reference - which it should...
// and it should loop...
fmt.Printf("grep \"%s\" %s\n", string(*mPriorStageResult), step.InputFile)
mGrepPattern = string(*mPriorStageResult)
mstageresults = GetResults(mGrepPattern, step, mPriorStageResult)
} else {
fmt.Printf("grep \"%s\" %s\n", step.GrepPattern, step.InputFile)
mGrepPattern = step.GrepPattern
mstageresults = GetResults(mGrepPattern, step, mPriorStageResult)
}
return &mstageresults
}
func do_regexp(step grep_step, mPriorStageResult *StageResult) *StageResults {
var stageresults StageResults
//var mregexp string
//mregexp = step.RegexpPattern
patt, err := regexp.Compile(step.RegexpPattern)
if err != nil {
fmt.Printf("Problem compiling regular expression :%s\n", step.RegexpPattern)
}
results := patt.FindAllStringSubmatch(string(*mPriorStageResult), -1)
if len(results) > 0 {
result := results[0]
if step.Output == "Memory" {
stageresults = append(stageresults, StageResult(result[1]))
}
}
return &stageresults
}
func ProcessGrep(mgrep_steps grep_steps) *MultipleStageResults {
var mBlankStageResult StageResult
var mMultipleStageResults MultipleStageResults
mMultipleStageResults = make(MultipleStageResults, 5)
fmt.Printf("Welcome to Process Grep\n")
for index, step := range mgrep_steps.Steps {
if len(step.GrepPattern) > 0 {
// loop at this level if there is more than 1 prior stage result..
if index == 0 {
mNewStageResults := do_grep(step, &mBlankStageResult)
mMultipleStageResults[step.Index] = append(mMultipleStageResults[step.Index], *mNewStageResults)
} else {
for _, mStageResults := range mMultipleStageResults[step.Index-1] {
for _, mStageResult := range mStageResults {
mNewStageResults := do_grep(step, &mStageResult)
mMultipleStageResults[step.Index] = append(mMultipleStageResults[step.Index], *mNewStageResults)
}
}
}
} else if len(step.Select_Ref) > 0 {
// use the output of the named step
for _, mStageResults := range mMultipleStageResults[step.Index-1] {
for _, mStageResult := range mStageResults {
mNewStageResults := do_regexp(step, &mStageResult)
mMultipleStageResults[step.Index] = append(mMultipleStageResults[step.Index], *mNewStageResults)
}
}
}
}
return &mMultipleStageResults
}