makefile: Add generate target to generate service code
This commit is contained in:
parent
80f92f70d0
commit
b783ce5a18
19
Makefile
19
Makefile
|
@ -16,6 +16,7 @@ help:
|
|||
@echo "Please use \`make <target>\` where <target> is one of"
|
||||
@echo " all to check, build, test and release this SDK"
|
||||
@echo " check to vet and lint the SDK"
|
||||
@echo " generate to generate service code"
|
||||
@echo " build to build the SDK"
|
||||
@echo " unit to run all sort of unit tests except runtime"
|
||||
@echo " unit-test to run unit test"
|
||||
|
@ -44,6 +45,23 @@ lint:
|
|||
if [[ -n $${lint} ]]; then echo "$${lint}"; exit 1; fi
|
||||
@echo "ok"
|
||||
|
||||
generate: snips-v0.0.9.exe ../qingcloud-api-specs/package.json
|
||||
./snips-v0.0.9.exe \
|
||||
-s=${shell go env GOPATH}/src/github.com/yunify \
|
||||
-m=qingcloud-api-specs \
|
||||
-n=2013-08-30 \
|
||||
-t=./template \
|
||||
-o=./service
|
||||
go fmt ./service/...
|
||||
@echo "ok"
|
||||
|
||||
snips-v0.0.9.exe:
|
||||
go run build/get-snips-v0.0.9.go
|
||||
|
||||
../qingcloud-api-specs/package.json:
|
||||
-go get -d github.com/yunify/qingcloud-api-specs
|
||||
file %@
|
||||
|
||||
build:
|
||||
@echo "build the SDK"
|
||||
GOOS=linux GOARCH=amd64 go build ${PKGS_TO_CHECK}
|
||||
|
@ -51,6 +69,7 @@ build:
|
|||
GOOS=windows GOARCH=amd64 go build ${PKGS_TO_CHECK}
|
||||
@echo "ok"
|
||||
|
||||
|
||||
unit: unit-test unit-benchmark unit-coverage unit-race
|
||||
|
||||
unit-test:
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
// +-------------------------------------------------------------------------
|
||||
// | Copyright (C) 2017 Yunify, Inc.
|
||||
// +-------------------------------------------------------------------------
|
||||
// | Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// | you may not use this work except in compliance with the License.
|
||||
// | You may obtain a copy of the License in the LICENSE file, or at:
|
||||
// |
|
||||
// | http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// | Unless required by applicable law or agreed to in writing, software
|
||||
// | distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// | See the License for the specific language governing permissions and
|
||||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
// +build ignore
|
||||
|
||||
// download snips binrary.
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// go run get-snips.go
|
||||
// go run get-snips.go -os=windows -output=snips.exe
|
||||
//
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
flagOS = flag.String("os", runtime.GOOS, "Specify the os (windows|darwin|linux)")
|
||||
flagFileName = flag.String("output", "snips-v0.0.9.exe", "Specify the output filename")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var snipsURL string
|
||||
switch *flagOS {
|
||||
case "windows", "darwin", "linux":
|
||||
const baseURL = "https://github.com/yunify/snips/releases/"
|
||||
snipsURL = fmt.Sprintf("%s/download/v0.0.9/snips-v0.0.9-%s_amd64.tar.gz", baseURL, *flagOS)
|
||||
default:
|
||||
log.Fatalf("invalid os: %v", *flagOS)
|
||||
}
|
||||
|
||||
data, err := fetchTarGzFile(snipsURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(*flagFileName, data, 0777)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("download snips-v0.0.9 ok")
|
||||
}
|
||||
|
||||
func fetchTarGzFile(downloadURL string) ([]byte, error) {
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
resp, err := client.Get(downloadURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return readTarGz(resp.Body)
|
||||
}
|
||||
|
||||
func readTarGz(r io.Reader) ([]byte, error) {
|
||||
gr, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer gr.Close()
|
||||
|
||||
// tar read
|
||||
tr := tar.NewReader(gr)
|
||||
for {
|
||||
if _, err := tr.Next(); err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if _, err := io.Copy(&buf, tr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
Loading…
Reference in New Issue