Replace Makefile with magefile and add some libs

This commit is contained in:
Christian Claus 2018-04-12 20:52:18 +02:00
parent 07a719c03c
commit 114845577e
4 changed files with 284 additions and 11 deletions

20
Gopkg.lock generated
View file

@ -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

View file

@ -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}

9
examples/config.yaml Normal file
View file

@ -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"

256
magefile.go Normal file
View file

@ -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
}