Commit c8f64d52 by shenyizhong

* gofmt

1 parent 18bd56ba
package auth package auth
import ( import (
"fmt" "fmt"
"os"
"log" "log"
"os"
) )
func init() { func init() {
......
package download package download
import ( import (
"os" "github.com/sirupsen/logrus"
"log" "log"
"github.com/sirupsen/logrus" "minsync/nodetree"
"minsync/nodetree" "os"
) )
func init() { func init() {
log.Println("minsync.download.init() called") log.Println("minsync.download.init() called")
var log01 = logrus.New() var log01 = logrus.New()
log01.Formatter = new(logrus.JSONFormatter) log01.Formatter = new(logrus.JSONFormatter)
log01.Formatter = new(logrus.TextFormatter) //default log01.Formatter = new(logrus.TextFormatter) //default
log01.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors log01.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
log01.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output log01.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
log01.Level = logrus.TraceLevel log01.Level = logrus.TraceLevel
log01.Out = os.Stdout log01.Out = os.Stdout
log01.Debug("minsync.download.init() called, from logrus") log01.Debug("minsync.download.init() called, from logrus")
} }
func Download(src string, dst string) (nodetree.Task, error) { func Download(src string, dst string) (nodetree.Task, error) {
var task nodetree.Task var task nodetree.Task
return task, nil return task, nil
} }
func QueryProgress(task_id string) (nodetree.Task, error) { func QueryProgress(task_id string) (nodetree.Task, error) {
var task nodetree.Task var task nodetree.Task
return task, nil return task, nil
} }
package encrypt package encrypt
import ( import (
_"fmt" _ "fmt"
"log" "log"
) )
func init() { func init() {
log.Println("minsync.encrypt.init() called") log.Println("minsync.encrypt.init() called")
} }
package httpcli package httpcli
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "log"
"log" "net/http"
) )
func init() { func init() {
log.Println("minsync.httpcli.init() called") log.Println("minsync.httpcli.init() called")
} }
func Get(url string) ([]byte, error) { func Get(url string) ([]byte, error) {
fmt.Println(url) fmt.Println(url)
resp, err := http.Get(url) resp, err := http.Get(url)
defer resp.Body.Close() defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
return body, err return body, err
} }
func Post(postdata string) ([]byte, error) { func Post(postdata string) ([]byte, error) {
return nil, nil return nil, nil
} }
func Delete(postdata string) ([]byte, error) { func Delete(postdata string) ([]byte, error) {
return nil, nil return nil, nil
} }
func Update(postdata string) ([]byte, error) { func Update(postdata string) ([]byte, error) {
return nil, nil return nil, nil
} }
...@@ -2,7 +2,6 @@ package main ...@@ -2,7 +2,6 @@ package main
import ( import (
"bytes" "bytes"
"os"
"encoding/json" "encoding/json"
_ "fmt" _ "fmt"
_ "io/ioutil" _ "io/ioutil"
...@@ -16,6 +15,7 @@ import ( ...@@ -16,6 +15,7 @@ import (
"minsync/travremot" "minsync/travremot"
_ "minsync/upload" _ "minsync/upload"
_ "net/http" _ "net/http"
"os"
"runtime" "runtime"
) )
......
package nodetree package nodetree
import ( import (
_"fmt" _ "fmt"
"log" "log"
) )
type NodeLoc struct { type NodeLoc struct {
Name string `json:"name"` Name string `json:"name"`
Path string `json:"path"` Path string `json:"path"`
Children []*NodeLoc `json:"children"` Children []*NodeLoc `json:"children"`
IsDir bool `json:"isDir"` IsDir bool `json:"isDir"`
} }
type OptionLoc struct { type OptionLoc struct {
RootPath []string `yaml:"rootPath"` RootPath []string `yaml:"rootPath"`
SubFlag bool `yaml:"subFlag"` // if traverse sub dir SubFlag bool `yaml:"subFlag"` // if traverse sub dir
IgnorePath []string `yaml:"ignorePath"` IgnorePath []string `yaml:"ignorePath"`
IgnoreFile []string `yaml:"ignoreFile"` IgnoreFile []string `yaml:"ignoreFile"`
} }
type NodeRemot struct { type NodeRemot struct {
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"` Type string `json:"type"`
UpdtDate string `json:"update_date"` UpdtDate string `json:"update_date"`
Creator string `json:"creator"` Creator string `json:"creator"`
Size int `json:"size"` Size int `json:"size"`
Children []*NodeRemot `json:"children"` Children []*NodeRemot `json:"children"`
} }
type ListRemot struct { type ListRemot struct {
List []*NodeRemot `json:"list"` List []*NodeRemot `json:"list"`
Total int `json:"total"` Total int `json:"total"`
PageSize int `json:"pagesize"` PageSize int `json:"pagesize"`
Pages int `json:"pages"` Pages int `json:"pages"`
} }
type DataRemot struct { type DataRemot struct {
Data ListRemot `json:"data"` Data ListRemot `json:"data"`
Msg string `json:"msg"` Msg string `json:"msg"`
} }
type OptionRemot struct { type OptionRemot struct {
RootPath []string `yaml:"rootPath"` RootPath []string `yaml:"rootPath"`
SubFlag bool `yaml:"subFlag"` // if traverse sub dir SubFlag bool `yaml:"subFlag"` // if traverse sub dir
PageSize int `yaml:"pagesize"` PageSize int `yaml:"pagesize"`
Pages int `yaml:"pages"` Pages int `yaml:"pages"`
Token string `yaml:"token"` Token string `yaml:"token"`
} }
type Task struct { type Task struct {
Id string `json:"id"` Id string `json:"id"`
Progress int `json:"progress"` Progress int `json:"progress"`
State int `json:"state"` State int `json:"state"`
} }
func init() { func init() {
log.Println("minsync.nodetree init() called") log.Println("minsync.nodetree init() called")
} }
package travloc package travloc
import ( import (
_"fmt" _ "fmt"
"strings" "io/ioutil"
"os" "log"
"io/ioutil" "minsync/nodetree"
"path" "os"
"log" "path"
"minsync/nodetree" "strings"
) )
func init() { func init() {
log.Println("minsync.travloc.init() called") log.Println("minsync.travloc.init() called")
} }
func Explorer(option nodetree.OptionLoc) (nodetree.NodeLoc, error) { func Explorer(option nodetree.OptionLoc) (nodetree.NodeLoc, error) {
var root nodetree.NodeLoc var root nodetree.NodeLoc
for _, p := range option.RootPath { for _, p := range option.RootPath {
if strings.TrimSpace(p) == "" { if strings.TrimSpace(p) == "" {
continue continue
} }
var child nodetree.NodeLoc var child nodetree.NodeLoc
child.Path = p child.Path = p
exploreRecursive(&child, &option) exploreRecursive(&child, &option)
root.Children = append(root.Children, &child) root.Children = append(root.Children, &child)
} }
return root, nil return root, nil
} }
func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc) { func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc) {
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
} }
node.Name = p.Name() node.Name = p.Name()
node.IsDir = p.IsDir() node.IsDir = p.IsDir()
if !p.IsDir() { if !p.IsDir() {
return return
} }
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" info := "dir is not exist, or open failed"
log.Printf("%v: %v", info, err) log.Printf("%v: %v", info, err)
return return
} }
for _, f := range sub { for _, f := range sub {
tmp := path.Join(node.Path, f.Name()) tmp := path.Join(node.Path, f.Name())
var child nodetree.NodeLoc var child nodetree.NodeLoc
child.Path = tmp child.Path = tmp
child.IsDir = f.IsDir() child.IsDir = f.IsDir()
if f.IsDir() { if f.IsDir() {
if option.SubFlag { if option.SubFlag {
if !IsInSlice(option.IgnorePath, f.Name()) { if !IsInSlice(option.IgnorePath, f.Name()) {
node.Children = append(node.Children, &child) node.Children = append(node.Children, &child)
exploreRecursive(&child, option) exploreRecursive(&child, option)
} }
} else { } else {
if !IsInSlice(option.IgnoreFile, f.Name()) { if !IsInSlice(option.IgnoreFile, f.Name()) {
child.Name = f.Name() child.Name = f.Name()
node.Children = append(node.Children, &child) node.Children = append(node.Children, &child)
} }
} }
} else { } else {
if !IsInSlice(option.IgnoreFile, f.Name()) { if !IsInSlice(option.IgnoreFile, f.Name()) {
child.Name = f.Name() child.Name = f.Name()
node.Children = append(node.Children, &child) node.Children = append(node.Children, &child)
} }
} }
} }
} }
func IsInSlice(slice []string, s string) bool { func IsInSlice(slice []string, s string) bool {
if len(slice) == 0 { if len(slice) == 0 {
return false; return false
} }
isIn := false isIn := false
for _, f := range slice { for _, f := range slice {
if f == s { if f == s {
isIn = true isIn = true
break break
} }
} }
return isIn return isIn
} }
package travloc package travloc
import ( import (
"fmt" "bytes"
"bytes" "encoding/json"
"encoding/json" "fmt"
"log" "log"
"os" "minsync/nodetree"
"runtime" "os"
"testing" "runtime"
"minsync/nodetree" "testing"
) )
func TestExplorer(t *testing.T) { func TestExplorer(t *testing.T) {
var option nodetree.Option var option nodetree.Option
fmt.Println(runtime.GOOS) fmt.Println(runtime.GOOS)
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" { if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
option.RootPath = []string{ option.RootPath = []string{
`/home/syz/workshop/projects/minsync/src`, `/home/syz/workshop/projects/minsync/src`,
} }
} }
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
option.RootPath = []string{`C:\`} option.RootPath = []string{`C:\`}
} }
option.SubFlag = true option.SubFlag = true
option.IgnorePath = []string{`.git`,`.svn`} option.IgnorePath = []string{`.git`, `.svn`}
option.IgnoreFile = []string{`.DS_Store`, `.gitignore`} option.IgnoreFile = []string{`.DS_Store`, `.gitignore`}
result, err := Explorer(option) result, err := Explorer(option)
if err != nil { if err != nil {
panic(err) panic(err)
} }
OutTerminal(result) OutTerminal(result)
} }
func OutTerminal(obj interface{}) { func OutTerminal(obj interface{}) {
b, err := json.Marshal(obj) b, err := json.Marshal(obj)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
var out bytes.Buffer var out bytes.Buffer
err = json.Indent(&out, b, "", " ") err = json.Indent(&out, b, "", " ")
out.WriteTo(os.Stdout) out.WriteTo(os.Stdout)
} }
package travremot package travremot
import ( import (
"strings" "encoding/json"
"log" "io/ioutil"
"encoding/json" "log"
"io/ioutil" "minsync/auth"
"net/http" "minsync/nodetree"
"minsync/auth" "net/http"
"minsync/nodetree" "strings"
) )
func init() { func init() {
log.Println("minsync.travremot.init() called") log.Println("minsync.travremot.init() called")
} }
func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) { func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
var root nodetree.NodeRemot var root nodetree.NodeRemot
for _, p := range option.RootPath { for _, p := range option.RootPath {
if strings.TrimSpace(p) == "" { if strings.TrimSpace(p) == "" {
continue continue
} }
var child nodetree.NodeRemot var child nodetree.NodeRemot
child.Name = p child.Name = p
exploreRecursive(&child, &option) exploreRecursive(&child, &option)
root.Children = append(root.Children, &child) root.Children = append(root.Children, &child)
} }
return root, nil return root, nil
} }
func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) { func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name) log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
url := "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=20&prefix=" url := "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=20&prefix="
url += node.Name url += node.Name
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
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)
} }
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
var jsonRes nodetree.DataRemot var jsonRes nodetree.DataRemot
json.Unmarshal(body, &jsonRes) json.Unmarshal(body, &jsonRes)
for _, f := range jsonRes.Data.List { for _, f := range jsonRes.Data.List {
tmp := node.Name + f.Name tmp := node.Name + f.Name
var child nodetree.NodeRemot var child nodetree.NodeRemot
child.Name = tmp child.Name = tmp
child.Type = f.Type child.Type = f.Type
if f.Type == "folder" { if f.Type == "folder" {
child.Name += "/" child.Name += "/"
node.Children = append(node.Children, &child) node.Children = append(node.Children, &child)
exploreRecursive(&child, option) exploreRecursive(&child, option)
} else if f.Type == "file" { } else if f.Type == "file" {
node.Children = append(node.Children, &child) node.Children = append(node.Children, &child)
} }
} }
defer resp.Body.Close() defer resp.Body.Close()
} }
...@@ -11,9 +11,9 @@ import ( ...@@ -11,9 +11,9 @@ import (
func TestExplorer(t *testing.T) { func TestExplorer(t *testing.T) {
var option nodetree.OptionRemot var option nodetree.OptionRemot
option.RootPath = []string{ option.RootPath = []string{
`data/`, `data/`,
} }
option.SubFlag = true option.SubFlag = true
......
package upload package upload
import ( import (
_"fmt" _ "fmt"
"log" "log"
"minsync/nodetree" "minsync/nodetree"
) )
func init() { func init() {
log.Println("minsync.upload.init() called") log.Println("minsync.upload.init() called")
} }
func Upload(src string, dst string) (nodetree.Task, error) { func Upload(src string, dst string) (nodetree.Task, error) {
var task nodetree.Task var task nodetree.Task
return task, nil return task, nil
} }
func QueryProgress(task_id string) (nodetree.Task, error) { func QueryProgress(task_id string) (nodetree.Task, error) {
var task nodetree.Task var task nodetree.Task
return task, nil return task, nil
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!