add webdav2archive
This commit is contained in:
		
						commit
						f84f227554
					
				|  | @ -0,0 +1,8 @@ | |||
| #!/bin/bash | ||||
| 
 | ||||
| result="$(gofmt -s -l . | grep -v '^vendor/' )" | ||||
| if [ -n "$result" ]; then | ||||
|   echo "Go code is not formatted, run 'gofmt -s -w .'" >&2 | ||||
|   echo "$result" | ||||
|   exit 1 | ||||
| fi | ||||
|  | @ -0,0 +1,25 @@ | |||
| #!/usr/bin/env python | ||||
| # checks if every desired package has test files | ||||
| 
 | ||||
| import os | ||||
| import re | ||||
| import sys | ||||
| 
 | ||||
| source_re = re.compile(".*\.go") | ||||
| test_re   = re.compile(".*_test\.go") | ||||
| missing   = False | ||||
| 
 | ||||
| for root, dirs, files in os.walk("."): | ||||
|   # ignore some paths | ||||
|   if root == "." or root.startswith("./vendor") or root.startswith("./."): | ||||
|     continue | ||||
| 
 | ||||
|   # source files but not test files? | ||||
|   if len(filter(source_re.match, files)) > 0 and len(filter(test_re.match, files)) == 0: | ||||
|     print("no test files for {}".format(root)) | ||||
|     missing = True | ||||
| 
 | ||||
| if missing: | ||||
|   sys.exit(1) | ||||
| else: | ||||
|   print("every package has test files") | ||||
|  | @ -0,0 +1,36 @@ | |||
| image: golang:latest | ||||
| stages: | ||||
|   - build | ||||
|   - test | ||||
| 
 | ||||
| before_script: | ||||
|   - mkdir -p "/go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE/" | ||||
|   - cp -R "/builds/$CI_PROJECT_PATH" "/go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE/" | ||||
|   - cd "/go/src/dev.sum7.eu/$CI_PROJECT_PATH" | ||||
|   - go get -d -t ./... | ||||
| 
 | ||||
| build-my-project: | ||||
|   stage: build | ||||
|   script: | ||||
|     - go install "dev.sum7.eu/$CI_PROJECT_PATH/..." | ||||
|     - mv "/go/bin/$CI_PROJECT_NAME" "/builds/$CI_PROJECT_PATH" | ||||
|   artifacts: | ||||
|     paths: | ||||
|       - "$CI_PROJECT_NAME" | ||||
| 
 | ||||
| test-my-project: | ||||
|   stage: test | ||||
|   script: | ||||
|     - go get github.com/client9/misspell/cmd/misspell | ||||
|     - misspell -error . | ||||
|     - ./.ci/check-gofmt | ||||
|     - ./.ci/check-testfiles | ||||
|     - go test $(go list ./... | grep -v /vendor/) -v -coverprofile .testCoverage.txt | ||||
|   artifacts: | ||||
|     paths: | ||||
|      - .testCoverage.txt | ||||
| 
 | ||||
| test-race-my-project: | ||||
|   stage: test | ||||
|   script: | ||||
|     - go test -race ./... | ||||
|  | @ -0,0 +1,95 @@ | |||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	"sync" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"strconv" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"flag" | ||||
| 
 | ||||
| 	"github.com/bdlm/log" | ||||
| 	"github.com/studio-b12/gowebdav" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	root = "https://cloud.sum7.eu/remote.php/webdav/" | ||||
| 	user = "genofire" | ||||
| 	password = os.Getenv("WEBDAV_PASSWORD") | ||||
| 
 | ||||
| 	fromDir = "AutoUpload" | ||||
| 	exportDir = "download" | ||||
| 	deleteOnDownload = false | ||||
| 
 | ||||
| 	client *gowebdav.Client | ||||
| 	wg = &sync.WaitGroup{} | ||||
| ) | ||||
| 
 | ||||
| func handleFile(file os.FileInfo) { | ||||
| 	defer wg.Done() | ||||
| 
 | ||||
| 	filepath := path.Join(fromDir, file.Name()) | ||||
| 	logger := log.WithField("from", filepath) | ||||
| 
 | ||||
| 	mtime := file.ModTime() | ||||
| 	 | ||||
| 	exportpathDir := path.Join(exportDir, strconv.Itoa(mtime.Year()), fmt.Sprintf("%.2d", mtime.Month())) | ||||
| 	os.MkdirAll(exportpathDir, os.ModePerm) | ||||
| 	exportpath := path.Join(exportpathDir, file.Name()) | ||||
| 	logger = logger.WithField("to", exportpath) | ||||
| 
 | ||||
| 	reader, _ := client.ReadStream(filepath) | ||||
| 
 | ||||
| 	f, err := os.Create(exportpath) | ||||
| 	if err != nil { | ||||
| 		logger.Warnf("unable to create dest: %s", err) | ||||
| 		return | ||||
| 	} | ||||
| 	defer f.Close() | ||||
| 
 | ||||
| 	if _, err = io.Copy(f, reader); err != nil { | ||||
| 		logger.Warnf("unable to download: %s", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if deleteOnDownload { | ||||
| 		if err = client.Remove(filepath); err != nil { | ||||
| 			logger.Warnf("unable to remove: %s", err) | ||||
| 			return | ||||
| 		} | ||||
| 		logger = logger.WithField("deleted", true) | ||||
| 	} | ||||
| 	 | ||||
| 
 | ||||
| 	logger.Info("downloaded") | ||||
| }  | ||||
| 
 | ||||
| func main() { | ||||
| 	flag.StringVar(&user, "u", user, "username to auth at webdav server") | ||||
| 	flag.StringVar(&password, "p", password, "password to auth at webdav server (use environment variable WEBDAV_PASSWORD for security reasen)") | ||||
| 	flag.StringVar(&root, "h", root, "address of webdav server") | ||||
| 
 | ||||
| 	flag.StringVar(&fromDir, "i", fromDir, "import path from webdave") | ||||
| 	flag.StringVar(&exportDir, "e", exportDir, "export path on local") | ||||
| 	flag.BoolVar(&deleteOnDownload, "f", deleteOnDownload, "delete on download") | ||||
| 	flag.Parse() | ||||
| 	 | ||||
| 	client = gowebdav.NewClient(root, user, password) | ||||
| 
 | ||||
| 	if client == nil { | ||||
| 		log.Panic("cloud not connect to webdav client") | ||||
| 	} | ||||
| 
 | ||||
| 	files, _ := client.ReadDir(fromDir) | ||||
| 
 | ||||
| 	for _, file := range files { | ||||
| 		if file.IsDir() { | ||||
| 			continue | ||||
| 		} | ||||
| 		wg.Add(1) | ||||
| 		go handleFile(file) | ||||
| 	} | ||||
| 	wg.Wait() | ||||
| 	log.Info("done") | ||||
| } | ||||
		Loading…
	
		Reference in New Issue