Commit 7e265479 by shenyizhong

* code review

1 parent 1638a80a
...@@ -12,4 +12,6 @@ const ( ...@@ -12,4 +12,6 @@ const (
ERR_JSON_PARSE ErrCode = -10006 ERR_JSON_PARSE ErrCode = -10006
ERR_HTTP_REQUEST ErrCode = -10007 ERR_HTTP_REQUEST ErrCode = -10007
ERR_INVALID_PATH ErrCode = -10008 ERR_INVALID_PATH ErrCode = -10008
ERR_NOT_DIR ErrCode = -10009
ERR_INVALID_HOST ErrCode = -10010
) )
...@@ -47,6 +47,8 @@ func init() { ...@@ -47,6 +47,8 @@ func init() {
log.Println(err) log.Println(err)
return return
} }
nodetree.StrHost = conf.Host
nodetree.IntPort = conf.Port
host = conf.Host host = conf.Host
port = conf.Port port = conf.Port
} }
......
...@@ -16,8 +16,8 @@ import ( ...@@ -16,8 +16,8 @@ import (
"minsync/travremot" "minsync/travremot"
_ "minsync/upload" _ "minsync/upload"
_ "net/http" _ "net/http"
"path/filepath"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
) )
...@@ -58,6 +58,10 @@ func main() { ...@@ -58,6 +58,10 @@ func main() {
nodetree.StrDst = os.Args[3] nodetree.StrDst = os.Args[3]
} }
if nodetree.StrHost == "" {
fmt.Println("parse config error")
return
}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1) nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1)
nodetree.StrDst = strings.Replace(nodetree.StrDst, "\\", "/", -1) nodetree.StrDst = strings.Replace(nodetree.StrDst, "\\", "/", -1)
...@@ -102,25 +106,6 @@ func main() { ...@@ -102,25 +106,6 @@ func main() {
nodetree.StrToken = string(data) 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" { if nodetree.StrOp == "upload" {
nodetree.StrDst = "data" + nodetree.StrDst nodetree.StrDst = "data" + nodetree.StrDst
nodetree.StrSrc, err = filepath.Abs(nodetree.StrSrc) nodetree.StrSrc, err = filepath.Abs(nodetree.StrSrc)
......
...@@ -90,6 +90,8 @@ var StrOp string ...@@ -90,6 +90,8 @@ var StrOp string
var StrSrc string var StrSrc string
var StrDst string var StrDst string
var StrToken string var StrToken string
var StrHost string
var IntPort int
func init() { func init() {
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
_ "fmt" _ "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"minsync/def"
"minsync/httpcli" "minsync/httpcli"
"minsync/nodetree" "minsync/nodetree"
"os" "os"
...@@ -35,25 +36,24 @@ func Explorer(option nodetree.OptionLoc, op string) (nodetree.NodeLoc, error) { ...@@ -35,25 +36,24 @@ func Explorer(option nodetree.OptionLoc, op string) (nodetree.NodeLoc, error) {
return root, nil 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) p, err := os.Stat(node.Path)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return def.ERR_SYSTEM, err
} }
node.Name = p.Name() node.Name = p.Name()
node.IsDir = p.IsDir() node.IsDir = p.IsDir()
if !p.IsDir() { if !p.IsDir() {
return return def.SUCCESS, nil
} }
sub, err := ioutil.ReadDir(node.Path) sub, err := ioutil.ReadDir(node.Path)
if err != nil { if err != nil {
info := "dir is not exist, or open failed" log.Println(err)
log.Printf("%v: %v", info, err) return def.ERR_SYSTEM, err
return
} }
for _, f := range sub { for _, f := range sub {
...@@ -90,6 +90,7 @@ func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc, op str ...@@ -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 { func IsInSlice(slice []string, s string) bool {
......
...@@ -6,30 +6,17 @@ import ( ...@@ -6,30 +6,17 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"minsync/auth" "minsync/auth"
"minsync/def"
"minsync/httpcli" "minsync/httpcli"
"minsync/nodetree" "minsync/nodetree"
"net/http" "net/http"
"os" _ "os"
"strconv" "strconv"
"strings" "strings"
) )
var host string
var port int
func init() { func init() {
log.Println("minsync.travremot.init() called") 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) { func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
...@@ -53,55 +40,73 @@ func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) { ...@@ -53,55 +40,73 @@ func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
return root, nil 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) log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
// "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix=" // "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix="
url := "http://" url := "http://"
url += host url += nodetree.StrHost
url += ":" url += ":"
url += strconv.Itoa(port) url += strconv.Itoa(nodetree.IntPort)
url += "/front/data/query_list?pages=1&pagesize=1&prefix=" url += "/front/data/query_list?pages=1&pagesize=1&prefix="
url += node.Name 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()) req.Header.Add("token", auth.GetToken())
var resp *http.Response var resp *http.Response
resp, err := http.DefaultClient.Do(req) resp, err = http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_HTTP_REQUEST, err
} }
defer resp.Body.Close() 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 var jsonRes nodetree.DataRemot
json.Unmarshal(body, &jsonRes) json.Unmarshal(body, &jsonRes)
url = "http://" url = "http://"
url += host url += nodetree.StrHost
url += ":" url += ":"
url += strconv.Itoa(port) url += strconv.Itoa(nodetree.IntPort)
url += "/front/data/query_list?pages=1&pagesize=" url += "/front/data/query_list?pages=1&pagesize="
url += strconv.Itoa(jsonRes.Data.Total) url += strconv.Itoa(jsonRes.Data.Total)
url += "&prefix=" url += "&prefix="
url += node.Name url += node.Name
log.Println(url) 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()) req.Header.Add("token", auth.GetToken())
var respAll *http.Response var respAll *http.Response
respAll, err = http.DefaultClient.Do(req) respAll, err = http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_HTTP_REQUEST, err
} }
defer respAll.Body.Close() 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 var jsonResAll nodetree.DataRemot
json.Unmarshal(bodyAll, &jsonResAll) json.Unmarshal(bodyAll, &jsonResAll)
...@@ -133,5 +138,5 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) { ...@@ -133,5 +138,5 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
node.Children = append(node.Children, &child) 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!