Skip to content

Commit

Permalink
fix #4 windows user compatiable
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Oct 23, 2018
1 parent 7519019 commit 55e0e8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 3 additions & 3 deletions advisor/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func init() {
"OK": {
Item: "OK",
Severity: "L0",
Summary: "✔️", // heavy check mark unicode
Content: `✔️`,
Case: "✔️",
Summary: "OK",
Content: `OK`,
Case: "OK",
Func: (*Query4Audit).RuleOK,
},
"ALI.001": {
Expand Down
19 changes: 16 additions & 3 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io/ioutil"
"os"
"regexp"
"runtime"
"strings"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -120,6 +121,14 @@ type Configration struct {
MaxPrettySQLLength int `yaml:"max-pretty-sql-length"` // 超出该长度的SQL会转换成指纹输出
}

// getDefaultLogOutput get default log-output by runtime.GOOS
func getDefaultLogOutput() string {
if runtime.GOOS == "windows" {
return "nul"
}
return os.Stderr.Name()
}

// Config 默认设置
var Config = &Configration{
OnlineDSN: &dsn{
Expand Down Expand Up @@ -158,7 +167,7 @@ var Config = &Configration{
SpaghettiQueryLength: 2048,
AllowDropIndex: false,
LogLevel: 3,
LogOutput: os.Stderr.Name(),
LogOutput: getDefaultLogOutput(),
ReportType: "markdown",
ReportCSS: "",
ReportJavascript: "",
Expand Down Expand Up @@ -538,7 +547,7 @@ func readCmdFlags() error {
maxPrettySQLLength := flag.Int("max-pretty-sql-length", Config.MaxPrettySQLLength, "MaxPrettySQLLength, 超出该长度的SQL会转换成指纹输出")
// 一个不存在log-level,用于更新usage。
// 因为vitess里面也用了flag,这些vitess的参数我们不需要关注
if !Config.Verbose {
if !Config.Verbose && runtime.GOOS != "windows" {
flag.Usage = usage
}
flag.Parse()
Expand Down Expand Up @@ -570,7 +579,11 @@ func readCmdFlags() error {
if BaseDir == "" {
Config.LogOutput = *logOutput
} else {
Config.LogOutput = BaseDir + "/" + *logOutput
if runtime.GOOS == "windows" {
Config.LogOutput = *logOutput
} else {
Config.LogOutput = BaseDir + "/" + *logOutput
}
}
}
Config.ReportType = strings.ToLower(*reportType)
Expand Down
12 changes: 12 additions & 0 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ package common

import (
"flag"
"runtime"
"testing"

"github.com/kr/pretty"
)

var update = flag.Bool("update", false, "update .golden files")

func TestGetDefaultLogOutput(t *testing.T) {
output := getDefaultLogOutput()
if runtime.GOOS == "windows" && output != "nul" {
t.Error("windows default -log-output not nul")
}

if runtime.GOOS != "windows" && output != "/dev/stderr" {
t.Error("default -log-output not /dev/stderr")
}
}

func TestParseConfig(t *testing.T) {
err := ParseConfig("")
if err != nil {
Expand Down

0 comments on commit 55e0e8a

Please sign in to comment.