diff --git a/main.go b/main.go index 3f33f93..c9e433d 100644 --- a/main.go +++ b/main.go @@ -10,22 +10,20 @@ import ( "net/http" "net/url" "os" + "runtime" "strings" - "github.com/BurntSushi/toml" "github.com/antonholmquist/jason" "github.com/fatih/color" ) -var VERSION string = "0.0-src" //set with ldflags - -type Config struct { - YDAppId string +var ( + VERSION string = "0.0-src" //set with ldflags + YDAppId string // must set on ldflags YDAppSec string -} +) var debug bool = false -var config Config func httpGet(url string) *jason.Object { resp, err := http.Get(url) @@ -117,11 +115,11 @@ func ydAPI(query string, from string) string { salt := rand.Int31() // assume query is in utf-8 - signstr := fmt.Sprintf("%s%s%d%s", config.YDAppId, query, salt, config.YDAppSec) + signstr := fmt.Sprintf("%s%s%d%s", YDAppId, query, salt, YDAppSec) sign := md5.Sum([]byte(signstr)) uri := fmt.Sprintf( "https://openapi.youdao.com/api?appKey=%s&q=%s&from=%s&to=zh-CHS&salt=%d&sign=%x", - config.YDAppId, url.QueryEscape(query), from, salt, sign) + YDAppId, url.QueryEscape(query), from, salt, sign) if debug { log.Printf("Req: %s", uri) @@ -163,12 +161,11 @@ func showHelp() { func main() { var interative, help bool - var configfile, from string + var from string flag.BoolVar(&help, "h", false, "show this help.") flag.BoolVar(&interative, "i", false, "interative mode, :q \\q EOF or Ctrl+C to exit.") flag.BoolVar(&debug, "d", false, "log api request") flag.StringVar(&from, "f", "EN", "translate-from language, default: EN") - flag.StringVar(&configfile, "c", "~/.ydgo", "translate-from language, default: EN") flag.Parse() if help { @@ -176,16 +173,6 @@ func main() { os.Exit(0) } - configfile = NormalizePath(configfile) - _, err := os.Stat(configfile) - if err != nil { - log.Fatal("Config file is missing: ", configfile) - } - - if _, err := toml.DecodeFile(configfile, &config); err != nil { - log.Fatal(err) - } - if interative || flag.NArg() == 0 { interativeMode(from) return @@ -193,4 +180,8 @@ func main() { query := strings.Join(flag.Args(), " ") printExplain(query, httpGet(ydAPI(query, from))) + if runtime.GOOS == "windows" { + fmt.Println("\nPress 'Enter' to continue...") + bufio.NewReader(os.Stdin).ReadBytes('\n') + } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..df693d5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,126 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/antonholmquist/jason" +) + +func Test_httpGet(t *testing.T) { + ret := []byte(`{ + "errorCode": "0", + "query": "good", + "translation": [ + "好" + ], + "basic": { + "phonetic": "gʊd", + "uk-phonetic": "gʊd", + "us-phonetic": "ɡʊd", + "uk-speech": "XXXX", + "us-speech": "XXXX", + "explains": [ + "好处", + "好的", + "好" + ] + }, + "web": [ + { + "key": "good", + "value": [ + "良好", + "善", + "美好" + ] + } + ], + "dict": { + "url": "yddict://m.youdao.com/dict?le=eng&q=good" + }, + "webdict": { + "url": "http://m.youdao.com/dict?le=eng&q=good" + }, + "l": "EN2zh-CHS", + "tSpeakUrl": "XXX", + "speakUrl": "XXX" +}`) + obj, _ := jason.NewObjectFromBytes(ret) + type args struct { + url string + } + tests := []struct { + name string + args args + want *jason.Object + }{ + // TODO: Add test cases. + {"mock", args{"http://www.mocky.io/v2/5d11ba66310000913e08cdcd"}, obj}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := httpGet(tt.args.url); !reflect.DeepEqual(got, tt.want) { + t.Errorf("httpGet() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_printExplain(t *testing.T) { + ret := []byte(`{ + "errorCode": "0", + "query": "good", + "translation": [ + "好" + ], + "basic": { + "phonetic": "gʊd", + "uk-phonetic": "gʊd", + "us-phonetic": "ɡʊd", + "uk-speech": "XXXX", + "us-speech": "XXXX", + "explains": [ + "好处", + "好的", + "好" + ] + }, + "web": [ + { + "key": "good", + "value": [ + "良好", + "善", + "美好" + ] + } + ], + "dict": { + "url": "yddict://m.youdao.com/dict?le=eng&q=good" + }, + "webdict": { + "url": "http://m.youdao.com/dict?le=eng&q=good" + }, + "l": "EN2zh-CHS", + "tSpeakUrl": "XXX", + "speakUrl": "XXX" +}`) + obj, _ := jason.NewObjectFromBytes(ret) + type args struct { + q string + v *jason.Object + } + tests := []struct { + name string + args args + }{ + // TODO: Add test cases. + {"good", args{"good", obj}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + printExplain(tt.args.q, tt.args.v) + }) + } +} diff --git a/make_release.sh b/make_release.sh index 50979f1..8beb5e2 100644 --- a/make_release.sh +++ b/make_release.sh @@ -1,19 +1,17 @@ #!/bin/bash +YDAPPID="" +YDAPPSEC="" +source .ydkey # -GITVER=$(git rev-parse --short HEAD) -VER=$1 - -if [[ -z $VER ]]; then - VER=$GITVER -fi - +VER=$(git describe --tags || git rev-parse --short HEAD) +LDFLAGS="-s -w -X main.VERSION=$VER -X main.YDAppId=$YDAPPID -X main.YDAppSec=$YDAPPSEC" BIN=ydgo rm -fv ydgo_* # for normal unix env for OS in darwin linux; do - CGO_ENABLED=0 GOARCH=amd64 GOOS=$OS go build -o ${BIN}_${OS} -ldflags "-s -w -X main.VERSION=$VER" + CGO_ENABLED=0 GOARCH=amd64 GOOS=$OS go build -o ${BIN}_${OS} -ldflags "${LDFLAGS}" done -CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -o ${BIN}_windows.exe -ldflags "-s -w -X main.VERSION=$VER" +CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -o ${BIN}_windows.exe -ldflags "${LDFLAGS}"