aboutsummaryrefslogtreecommitdiff
path: root/cmd/monwork/main.go
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2018-12-14 14:02:58 +0100
committerGibheer <gibheer+git@zero-knowledge.org>2018-12-14 14:02:58 +0100
commit10fb89a017edb4fe35d87d22f1ff96d79e78c185 (patch)
treebc1ed9c2692a214e1ded8b47de85f68cdc221582 /cmd/monwork/main.go
parentff79584084436b74151e7549f70ee8f0a1d3aebe (diff)
monwork - readd stringToShellFields
This function was needed as bytes.Fields had some problems with the quoting. Now there are test cases too so that errors can be found more easily.
Diffstat (limited to 'cmd/monwork/main.go')
-rw-r--r--cmd/monwork/main.go37
1 files changed, 36 insertions, 1 deletions
diff --git a/cmd/monwork/main.go b/cmd/monwork/main.go
index d3b852a..81bc347 100644
--- a/cmd/monwork/main.go
+++ b/cmd/monwork/main.go
@@ -167,7 +167,7 @@ func startConfigGen(db *sql.DB, checkInterval time.Duration) {
time.Sleep(checkInterval)
continue
}
- if _, err := tx.Exec(SQLRefreshActiveCheck, check_id, pq.Array(bytes.Fields(cmd.Bytes()))); err != nil {
+ if _, err := tx.Exec(SQLRefreshActiveCheck, check_id, pq.Array(stringToShellFields(cmd.Bytes()))); err != nil {
tx.Rollback()
log.Printf("could not refresh check '%d': %s", check_id, err)
continue
@@ -184,6 +184,41 @@ func startConfigGen(db *sql.DB, checkInterval time.Duration) {
}
}
+func stringToShellFields(in []byte) [][]byte {
+ if len(in) == 0 {
+ return [][]byte{}
+ }
+ fields := bytes.Fields(in)
+ result := [][]byte{}
+
+ var quote byte
+
+ for _, field := range fields {
+ if quote == 0 && (field[0] != '\'' && field[0] != '"') {
+ result = append(result, field)
+ continue
+ }
+ if quote == 0 && (field[0] == '\'' || field[0] == '"') {
+ quote = field[0]
+ if field[len(field)-1] == quote {
+ result = append(result, field[1:len(field)-1])
+ quote = 0
+ continue
+ }
+ result = append(result, field[1:])
+ continue
+ }
+ idx := len(result) - 1
+ if bytes.HasSuffix(field, []byte{quote}) {
+ result[idx] = append(result[idx], append([]byte(" "), field[:len(field)-1]...)...)
+ quote = 0
+ continue
+ }
+ result[idx] = append(result[idx], append([]byte(" "), field...)...)
+ }
+ return result
+}
+
var (
SQLGetConfigUpdates = `select c.id, co.command, c.options
from checks c