From 114845577eaae8faab0550f6ac0c43fc5374a9cc Mon Sep 17 00:00:00 2001 From: Christian Claus Date: Thu, 12 Apr 2018 20:52:18 +0200 Subject: [PATCH] Replace Makefile with magefile and add some libs --- Gopkg.lock | 20 +++- Makefile | 10 -- examples/config.yaml | 9 ++ magefile.go | 256 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 284 insertions(+), 11 deletions(-) delete mode 100644 Makefile create mode 100644 examples/config.yaml create mode 100644 magefile.go diff --git a/Gopkg.lock b/Gopkg.lock index d15c491..aef1a56 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -19,6 +19,18 @@ packages = [".","hcl/ast","hcl/parser","hcl/printer","hcl/scanner","hcl/strconv","hcl/token","json/parser","json/scanner","json/token"] revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168" +[[projects]] + name = "github.com/inconshreveable/mousetrap" + packages = ["."] + revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" + version = "v1.0" + +[[projects]] + name = "github.com/magefile/mage" + packages = ["mg","types"] + revision = "ab3ca2f6f85577d7ec82e0a6df721147a2e737f9" + version = "v2.0.1" + [[projects]] name = "github.com/magiconair/properties" packages = ["."] @@ -55,6 +67,12 @@ revision = "8965335b8c7107321228e3e3702cab9832751bac" version = "v1.2.0" +[[projects]] + name = "github.com/spf13/cobra" + packages = ["."] + revision = "a1f051bc3eba734da4772d60e2d677f47cf93ef4" + version = "v0.0.2" + [[projects]] branch = "master" name = "github.com/spf13/jwalterweatherman" @@ -106,6 +124,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "0d9d00d277680f498770509dc5439fd9bb741fbbf465313b2a0c679b8fdd91c1" + inputs-digest = "6e64b577abda36a4b7f73579db83d543cc98c0553d74f2d78f9469c0a4d81296" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Makefile b/Makefile deleted file mode 100644 index 6ba410a..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -.PHONY: fmt check - -SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*") - -fmt: - @gofmt -s -l -w $(SRC) - -check: - @for i in $$(go list ./... | grep -v /vendor/); do golint $${i}; done - @go tool vet ${SRC} diff --git a/examples/config.yaml b/examples/config.yaml new file mode 100644 index 0000000..b9378c7 --- /dev/null +++ b/examples/config.yaml @@ -0,0 +1,9 @@ +address: "0.0.0.0" # the bind address +port: "8000" # the listening port +dir: "/home/webdav" # the provided base dir +users: + user: # with password 'foo' and jailed access to '/home/webdav/user' + password: "$2a$10$yITzSSNJZAdDZs8iVBQzkuZCzZ49PyjTiPIrmBUKUpB0pwX7eySvW" + subdir: "/user" + admin: # with password 'foo' and access to '/home/webdav' + password: "$2a$10$DaWhagZaxWnWAOXY0a55.eaYccgtMOL3lGlqI3spqIBGyM0MD.EN6" diff --git a/magefile.go b/magefile.go new file mode 100644 index 0000000..954b50c --- /dev/null +++ b/magefile.go @@ -0,0 +1,256 @@ +// +build mage + +package main + +import ( + "archive/zip" + "fmt" + "io" + "os" + "os/exec" + + "github.com/magefile/mage/mg" + "path/filepath" + "strings" +) + +// Default target to run when none is specified +// If not set, running mage will list available targets +// var Default = Build + +const ( + // DIST is the name of the dist directory + DIST = "dist" +) + +type target struct { + goos string + goarch string +} + +// Build Builds swd and swdcli and moves it to the dist directory +func Build() error { + //mg.Deps(InstallDeps) + mg.Deps(Clean) + + if _, err := os.Stat(DIST); os.IsNotExist(err) { + os.Mkdir(DIST, os.ModePerm) + fmt.Printf("Created dist dir: %s\n", DIST) + } + + fmt.Println("Building...") + + buildSpecific(target{}) + + fmt.Printf("Compiled files moved to folder: %s\n", DIST) + + return nil +} + +// BuildReleases Builds swd and swdcli for different OS and package them to a zip file for each os +func BuildReleases() error { + mg.Deps(Clean) + + targets := []target{ + {"windows", "amd64"}, + {"windows", "386"}, + {"darwin", "amd64"}, + {"linux", "amd64"}, + {"linux", "386"}, + } + + for _, t := range targets { + fmt.Printf("Building for OS %s and architecture %s\n", t.goos, t.goarch) + swd, swdCli, _ := buildSpecific(t) + + files := []string{ + swd, + swdCli, + "Readme.md", + filepath.Join("examples", "config.yaml"), + } + + archiveName := fmt.Sprintf("swd-%s-%s.zip", t.goos, t.goarch) + zipFiles(filepath.Join("dist", archiveName), files) + + os.Remove(swd) + os.Remove(swdCli) + } + + return nil +} + +// Fmt Formats the code via gofmt +func Fmt() error { + fmt.Println("Formatting code ...") + + fileList, err := goFileList() + if err != nil { + return err + } + + for _, file := range fileList { + err = exec.Command("gofmt", "-s", "-l", "-w", file).Run() + + if err != nil { + return err + } + } + + return nil +} + +// Check Runs golint and go tool vet on each .go file. +func Check() error { + fmt.Println("Checking code ...") + + fileList, err := goFileList() + if err != nil { + return err + } + + for _, file := range fileList { + lintOut, err := exec.Command("golint", file).Output() + if err != nil { + return err + } + + if len(lintOut) > 0 { + fmt.Println(string(lintOut)) + } + + vetOut, err := exec.Command("go", "tool", "vet", file).Output() + if len(vetOut) > 0 { + fmt.Println(string(vetOut)) + } + + if err != nil { + return err + } + } + + return nil +} + +// Install Installs swd and swdcli to your $GOPATH/bin folder +func Install() error { + //mg.Deps(InstallDeps) + + fmt.Println("Installing...") + return exec.Command("go", "install", "./...").Run() +} + +// InstallDeps Runs dep ensure and installs additional dependencies. +func InstallDeps() error { + fmt.Println("Installing Deps...") + err := exec.Command("dep", "ensure").Run() + if err != nil { + return err + } + + // Install necessary dependency for windows compilation + return exec.Command("go", "get", "-u", "github.com/inconshreveable/mousetrap").Run() +} + +// Clean Removes the dist directory +func Clean() { + fmt.Println("Cleaning...") + os.RemoveAll(DIST) +} + +func goFileList() ([]string, error) { + fileList := make([]string, 0) + err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error { + if !strings.HasPrefix(path, "vendor") && strings.HasSuffix(path, ".go") { + fileList = append(fileList, path) + } + + return err + }) + + return fileList, err +} + +func buildSpecific(t target) (string, string, error) { + env := os.Environ() + + if t.goos != "" && t.goarch != "" { + env = append(env, fmt.Sprintf("GOOS=%s", t.goos)) + env = append(env, fmt.Sprintf("GOARCH=%s", t.goarch)) + } + + swdSource := filepath.Join("cmd", "swd", "main.go") + swdExe := filepath.Join(DIST, "swd") + if t.goos == "windows" { + swdExe += ".exe" + } + swdCommand := exec.Command("go", "build", "-o", swdExe, swdSource) + swdCommand.Env = env + err := swdCommand.Run() + if err != nil { + return "", "", err + } + + swdCliSource := filepath.Join("cmd", "swdcli", "main.go") + swdCliExe := filepath.Join(DIST, "swdcli") + if t.goos == "windows" { + swdCliExe += ".exe" + } + swdCliCommand := exec.Command("go", "build", "-o", swdCliExe, swdCliSource) + swdCliCommand.Env = env + err = swdCliCommand.Run() + if err != nil { + return "", "", err + } + + return swdExe, swdCliExe, nil +} + +// zipFiles compresses one or many files into a single zip archive file. +// The original code was published under MIT licence under https://golangcode.com/create-zip-files-in-go/ +func zipFiles(filename string, files []string) error { + + newfile, err := os.Create(filename) + if err != nil { + return err + } + defer newfile.Close() + + zipWriter := zip.NewWriter(newfile) + defer zipWriter.Close() + + // Add files to zip + for _, file := range files { + + zipfile, err := os.Open(file) + if err != nil { + return err + } + defer zipfile.Close() + + // Get the file information + info, err := zipfile.Stat() + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + // Change to deflate to gain better compression + // see http://golang.org/pkg/archive/zip/#pkg-constants + header.Method = zip.Deflate + + writer, err := zipWriter.CreateHeader(header) + if err != nil { + return err + } + _, err = io.Copy(writer, zipfile) + if err != nil { + return err + } + } + return nil +}