支持run
This commit is contained in:
116
cmd/saastool/task_upload.go
Normal file
116
cmd/saastool/task_upload.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"e.coding.net/rta/public/saasapi"
|
||||
"e.coding.net/rta/public/saasapi/pkg/saashttp"
|
||||
)
|
||||
|
||||
type uploadTaskParams struct {
|
||||
taskSha256 string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunTaskUpload(args ...string) error {
|
||||
fs := flag.NewFlagSet("upload", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
sha256 := paramSha256(fs)
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
fmt.Println("command line parse error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if fs.NArg() > 0 || len(*sha256) == 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Println("LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
uploadTaskParams := uploadTaskParams{
|
||||
taskSha256: *sha256,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: &cfg.ApiUrls,
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doTaskUpload(uploadTaskParams)
|
||||
|
||||
}
|
||||
func doTaskUpload(uploadTaskParams uploadTaskParams) error {
|
||||
infoTaskParams := infoTaskParams{
|
||||
taskSha256: uploadTaskParams.taskSha256,
|
||||
saasHttp: uploadTaskParams.saasHttp,
|
||||
}
|
||||
taskInfo, err := doTaskInfo(infoTaskParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
totalFiles := len(taskInfo.GetTaskFileInfos())
|
||||
fi := 0
|
||||
for _, finfo := range taskInfo.GetTaskFileInfos() {
|
||||
|
||||
fi++
|
||||
var f *os.File
|
||||
offset := int64(0)
|
||||
totalBlocks := len(finfo.GetFileBlocks())
|
||||
bi := 0
|
||||
for _, binfo := range finfo.GetFileBlocks() {
|
||||
bi++
|
||||
if !binfo.GetUploaded() {
|
||||
if f == nil {
|
||||
f, err = os.Open(finfo.GetFileName())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
blockRes, err := uploadTaskParams.saasHttp.TaskUpload(
|
||||
binfo.GetBlockSha256(),
|
||||
f,
|
||||
offset,
|
||||
int(binfo.GetBlockLength()),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if blockRes.GetCode() != saasapi.ErrorCode_SUCC {
|
||||
return fmt.Errorf("upload block error, code %d, msg %s", blockRes.GetCode(), blockRes.GetStatus())
|
||||
} else {
|
||||
fmt.Printf("upload block success. file: %v, sha256 %v. block %v/%v, file %v/%v\n",
|
||||
finfo.GetFileName(), binfo.GetBlockSha256(),
|
||||
bi, totalBlocks, fi, totalFiles,
|
||||
)
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Printf("uploaded block. file: %v, sha256 %v. block %v/%v, file %v/%v\n",
|
||||
finfo.GetFileName(), binfo.GetBlockSha256(),
|
||||
bi, totalBlocks, fi, totalFiles,
|
||||
)
|
||||
}
|
||||
offset += int64(binfo.GetBlockLength())
|
||||
|
||||
}
|
||||
|
||||
if f != nil {
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user