forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 35
/
nosqlcomment.go
39 lines (36 loc) · 1 KB
/
nosqlcomment.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
// The program deletes COMMENT in sql file. It creates filename-new.sql file.
func main() {
sql := flag.String("sql", `sql.sql`, "Initial sql file.")
flag.Parse()
outfile := filepath.Base(*sql)
ext := filepath.Ext(outfile)
outfile = filepath.Dir(*sql) + `/` + outfile[:len(outfile)-len(ext)] + `-new` + ext
if sqlText, err := ioutil.ReadFile(*sql); err != nil {
fmt.Println(err.Error())
} else {
tmp := strings.Replace(string(sqlText), ` COMMENT=`, ` COMMENT =`, -1)
parts := strings.Split(strings.Replace(tmp, ` comment=`, ` COMMENT =`, -1), ` COMMENT`)
pattern := regexp.MustCompile(`(?i)^[=\s]+'[^']*'`) //`^\s*"[^"]*"\s*,`)
output := ``
for _, item := range parts {
found := pattern.FindStringIndex(item)
if found == nil {
output += item
} else {
output += item[found[1]:]
}
}
if err := ioutil.WriteFile(outfile, []byte(output), 0644); err != nil {
fmt.Println(err.Error())
}
}
}