2022-10-21 18:50:38 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-04 19:02:15 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2022-11-03 23:42:42 +08:00
|
|
|
"strings"
|
2022-10-21 18:50:38 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMysql(t *testing.T) {
|
2022-11-04 19:02:15 +08:00
|
|
|
path := "/Users/slooop/go/src/github.com/1Panel/apps/mysql/5.7.39/conf/my.cnf"
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
lineBytes, err := ioutil.ReadFile(path)
|
2022-11-03 23:42:42 +08:00
|
|
|
if err != nil {
|
2022-11-04 19:02:15 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
lines = strings.Split(string(lineBytes), "\n")
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
var newLines []string
|
2022-11-03 23:42:42 +08:00
|
|
|
|
2022-11-04 19:02:15 +08:00
|
|
|
start := "[mysqld]"
|
|
|
|
isOn := false
|
|
|
|
hasKey := false
|
|
|
|
regItem, _ := regexp.Compile(`^\[*\]`)
|
|
|
|
i := 0
|
|
|
|
for _, line := range lines {
|
|
|
|
i++
|
|
|
|
if strings.HasPrefix(line, start) {
|
|
|
|
isOn = true
|
|
|
|
newLines = append(newLines, line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !isOn {
|
|
|
|
newLines = append(newLines, line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(line, "user") || strings.HasPrefix(line, "# user") {
|
|
|
|
newLines = append(newLines, "user="+"ON")
|
|
|
|
hasKey = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
isDeadLine := regItem.Match([]byte(line))
|
|
|
|
if !isDeadLine {
|
|
|
|
newLines = append(newLines, line)
|
|
|
|
continue
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
if !hasKey {
|
|
|
|
newLines = append(newLines, "user="+"ON \n")
|
|
|
|
newLines = append(newLines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString(strings.Join(newLines, "\n"))
|
2022-11-03 23:42:42 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
2022-10-25 11:41:19 +08:00
|
|
|
}
|