Commit 7e265479 by shenyizhong

* code review

1 parent 1638a80a
......@@ -12,4 +12,6 @@ const (
ERR_JSON_PARSE ErrCode = -10006
ERR_HTTP_REQUEST ErrCode = -10007
ERR_INVALID_PATH ErrCode = -10008
ERR_NOT_DIR ErrCode = -10009
ERR_INVALID_HOST ErrCode = -10010
)
......@@ -47,6 +47,8 @@ func init() {
log.Println(err)
return
}
nodetree.StrHost = conf.Host
nodetree.IntPort = conf.Port
host = conf.Host
port = conf.Port
}
......
......@@ -16,8 +16,8 @@ import (
"minsync/travremot"
_ "minsync/upload"
_ "net/http"
"path/filepath"
"os"
"path/filepath"
"runtime"
"strings"
)
......@@ -58,6 +58,10 @@ func main() {
nodetree.StrDst = os.Args[3]
}
if nodetree.StrHost == "" {
fmt.Println("parse config error")
return
}
if runtime.GOOS == "windows" {
nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1)
nodetree.StrDst = strings.Replace(nodetree.StrDst, "\\", "/", -1)
......@@ -102,32 +106,13 @@ func main() {
nodetree.StrToken = string(data)
}
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
file, err := os.Open(pwd + "/config.json")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
decoder := json.NewDecoder(file)
conf := nodetree.Configuration{}
err = decoder.Decode(&conf)
if err != nil {
fmt.Println(err)
return
}
if nodetree.StrOp == "upload" {
nodetree.StrDst = "data" + nodetree.StrDst
nodetree.StrSrc, err = filepath.Abs(nodetree.StrSrc)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(err)
return
}
tmpstr := nodetree.StrSrc[:len(nodetree.StrSrc)-1]
idx := strings.LastIndex(tmpstr, "/")
......@@ -166,9 +151,9 @@ func main() {
nodetree.StrDst, err = filepath.Abs(nodetree.StrDst)
nodetree.StrSrc = "data" + nodetree.StrSrc
if err != nil {
fmt.Println(err)
return
}
fmt.Println(err)
return
}
// TODO, check if nodetree.StrDst is FILE or DIR
......
......@@ -90,6 +90,8 @@ var StrOp string
var StrSrc string
var StrDst string
var StrToken string
var StrHost string
var IntPort int
func init() {
......
......@@ -4,6 +4,7 @@ import (
_ "fmt"
"io/ioutil"
"log"
"minsync/def"
"minsync/httpcli"
"minsync/nodetree"
"os"
......@@ -35,25 +36,24 @@ func Explorer(option nodetree.OptionLoc, op string) (nodetree.NodeLoc, error) {
return root, nil
}
func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc, op string) {
func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc, op string) (def.ErrCode, error) {
p, err := os.Stat(node.Path)
if err != nil {
log.Println(err)
return
return def.ERR_SYSTEM, err
}
node.Name = p.Name()
node.IsDir = p.IsDir()
if !p.IsDir() {
return
return def.SUCCESS, nil
}
sub, err := ioutil.ReadDir(node.Path)
if err != nil {
info := "dir is not exist, or open failed"
log.Printf("%v: %v", info, err)
return
log.Println(err)
return def.ERR_SYSTEM, err
}
for _, f := range sub {
......@@ -90,6 +90,7 @@ func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc, op str
}
}
}
return def.SUCCESS, nil
}
func IsInSlice(slice []string, s string) bool {
......
......@@ -6,30 +6,17 @@ import (
"io/ioutil"
"log"
"minsync/auth"
"minsync/def"
"minsync/httpcli"
"minsync/nodetree"
"net/http"
"os"
_ "os"
"strconv"
"strings"
)
var host string
var port int
func init() {
log.Println("minsync.travremot.init() called")
file, _ := os.Open("./config.json")
defer file.Close()
decoder := json.NewDecoder(file)
conf := nodetree.Configuration{}
err := decoder.Decode(&conf)
if err != nil {
log.Println("Error:", err)
}
host = conf.Host
port = conf.Port
}
func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
......@@ -53,55 +40,73 @@ func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
return root, nil
}
func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) (def.ErrCode, error) {
log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
// "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix="
url := "http://"
url += host
url += nodetree.StrHost
url += ":"
url += strconv.Itoa(port)
url += strconv.Itoa(nodetree.IntPort)
url += "/front/data/query_list?pages=1&pagesize=1&prefix="
url += node.Name
req, _ := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
req.Header.Add("token", auth.GetToken())
var resp *http.Response
resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return def.ERR_SYSTEM, err
}
var jsonRes nodetree.DataRemot
json.Unmarshal(body, &jsonRes)
url = "http://"
url += host
url += nodetree.StrHost
url += ":"
url += strconv.Itoa(port)
url += strconv.Itoa(nodetree.IntPort)
url += "/front/data/query_list?pages=1&pagesize="
url += strconv.Itoa(jsonRes.Data.Total)
url += "&prefix="
url += node.Name
log.Println(url)
req, _ = http.NewRequest("GET", url, nil)
req, err = http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
req.Header.Add("token", auth.GetToken())
var respAll *http.Response
respAll, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
defer respAll.Body.Close()
bodyAll, _ := ioutil.ReadAll(respAll.Body)
bodyAll, err := ioutil.ReadAll(respAll.Body)
if err != nil {
log.Println(err)
return def.ERR_SYSTEM, err
}
var jsonResAll nodetree.DataRemot
json.Unmarshal(bodyAll, &jsonResAll)
......@@ -133,5 +138,5 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
node.Children = append(node.Children, &child)
}
}
return def.SUCCESS, nil
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!