Skip to content

Commit

Permalink
embed key with ldflags/pause on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Jun 25, 2019
1 parent e48d6ef commit dbcfb57
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 30 deletions.
33 changes: 12 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -163,34 +161,27 @@ 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 {
showHelp()
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
}

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')
}
}
126 changes: 126 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
16 changes: 7 additions & 9 deletions make_release.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit dbcfb57

Please sign in to comment.