Merge pull request #27 from drone-plugins/refactor-and-fix

Refactor, fixes and latest Hugo version
This commit is contained in:
Thomas Boerger 2020-05-15 07:57:30 +02:00 committed by GitHub
commit 054249e93a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 232 additions and 170 deletions

View File

@ -10,7 +10,7 @@ platform:
steps: steps:
- name: vet - name: vet
pull: always pull: always
image: golang:1.11-windowsservercore-1803 image: golang:1.14-windowsservercore-1803
commands: commands:
- go vet ./... - go vet ./...
environment: environment:
@ -21,7 +21,7 @@ steps:
- name: test - name: test
pull: always pull: always
image: golang:1.11-windowsservercore-1803 image: golang:1.14-windowsservercore-1803
commands: commands:
- go test -cover ./... - go test -cover ./...
environment: environment:
@ -52,7 +52,7 @@ platform:
steps: steps:
- name: build-push - name: build-push
pull: always pull: always
image: golang:1.11-windowsservercore-1803 image: golang:1.14-windowsservercore-1803
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe" - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment: environment:
@ -65,7 +65,7 @@ steps:
- name: build-tag - name: build-tag
pull: always pull: always
image: golang:1.11-windowsservercore-1803 image: golang:1.14-windowsservercore-1803
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe" - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment: environment:
@ -77,7 +77,7 @@ steps:
- name: executable - name: executable
pull: always pull: always
image: golang:1.11-windowsservercore-1803 image: golang:1.14-windowsservercore-1803
commands: commands:
- ./release/windows/amd64/drone-hugo.exe --help - ./release/windows/amd64/drone-hugo.exe --help
@ -148,7 +148,7 @@ platform:
steps: steps:
- name: build-push - name: build-push
pull: always pull: always
image: golang:1.11-windowsservercore-1809 image: golang:1.14-windowsservercore-1809
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe" - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment: environment:
@ -161,7 +161,7 @@ steps:
- name: build-tag - name: build-tag
pull: always pull: always
image: golang:1.11-windowsservercore-1809 image: golang:1.14-windowsservercore-1809
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe" - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment: environment:
@ -173,7 +173,7 @@ steps:
- name: executable - name: executable
pull: always pull: always
image: golang:1.11-windowsservercore-1809 image: golang:1.14-windowsservercore-1809
commands: commands:
- ./release/windows/amd64/drone-hugo.exe --help - ./release/windows/amd64/drone-hugo.exe --help

View File

@ -9,7 +9,7 @@ platform:
steps: steps:
- name: vet - name: vet
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- go vet ./... - go vet ./...
environment: environment:
@ -20,7 +20,7 @@ steps:
- name: test - name: test
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- go test -cover ./... - go test -cover ./...
environment: environment:
@ -50,7 +50,7 @@ platform:
steps: steps:
- name: build-push - name: build-push
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/amd64/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/amd64/drone-hugo"
environment: environment:
@ -63,7 +63,7 @@ steps:
- name: build-tag - name: build-tag
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/amd64/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/amd64/drone-hugo"
environment: environment:
@ -75,7 +75,7 @@ steps:
- name: executable - name: executable
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- ./release/linux/amd64/drone-hugo --help - ./release/linux/amd64/drone-hugo --help
@ -134,7 +134,7 @@ platform:
steps: steps:
- name: build-push - name: build-push
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm64/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm64/drone-hugo"
environment: environment:
@ -147,7 +147,7 @@ steps:
- name: build-tag - name: build-tag
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm64/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm64/drone-hugo"
environment: environment:
@ -159,7 +159,7 @@ steps:
- name: executable - name: executable
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- ./release/linux/arm64/drone-hugo --help - ./release/linux/arm64/drone-hugo --help
@ -218,7 +218,7 @@ platform:
steps: steps:
- name: build-push - name: build-push
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm/drone-hugo"
environment: environment:
@ -231,7 +231,7 @@ steps:
- name: build-tag - name: build-tag
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm/drone-hugo" - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm/drone-hugo"
environment: environment:
@ -243,7 +243,7 @@ steps:
- name: executable - name: executable
pull: always pull: always
image: golang:1.11 image: golang:1.14
commands: commands:
- ./release/linux/arm/drone-hugo --help - ./release/linux/arm/drone-hugo --help

View File

@ -1,3 +1,11 @@
FROM amd64/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-amd64 FROM plugins/base:linux-amd64
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=64bit
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=64bit
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/amd64/drone-hugo /bin/ ADD release/linux/amd64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm32v6/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-arm FROM plugins/base:linux-arm
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=arm
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=arm
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm/drone-hugo /bin/ ADD release/linux/arm/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm64v8/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-arm64 FROM plugins/base:linux-arm64
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=arm64
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=arm64
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm64/drone-hugo /bin/ ADD release/linux/arm64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -6,22 +6,71 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"github.com/pkg/errors"
) )
var ( var (
_downloadURL = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_Linux-%s.tar.gz" url = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_%s-%s.tar.gz"
) )
func downloadURL(version string, extended bool) string { // Get will download the specified hugo verion
var archType string func Get(version string, extended bool) (string, error) {
var binary string resp, err := http.Get(download(version, extended))
if err != nil {
return "", err
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", err
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := tempfile()
if err != nil {
return "", err
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", fmt.Errorf("no hugo binary found")
}
if err != nil {
return "", err
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
return "", err
}
return hugoPath, nil
}
}
}
func download(version string, extended bool) string {
var (
binary string
osName string
archType string
)
if extended { if extended {
binary = "hugo_extended" binary = "hugo_extended"
@ -29,6 +78,15 @@ func downloadURL(version string, extended bool) string {
binary = "hugo" binary = "hugo"
} }
switch runtime.GOOS {
case "linux":
osName = "Linux"
case "windows":
osName = "Windows"
default:
osName = "unsupported"
}
switch runtime.GOARCH { switch runtime.GOARCH {
case "amd64": case "amd64":
archType = "64bit" archType = "64bit"
@ -41,55 +99,17 @@ func downloadURL(version string, extended bool) string {
default: default:
archType = "unsupported" archType = "unsupported"
} }
return fmt.Sprintf(_downloadURL, version, binary, version, archType)
return fmt.Sprintf(url, version, binary, version, osName, archType)
} }
func getTempFile() (string, io.WriteCloser, error) { func tempfile() (string, io.WriteCloser, error) {
d, err := ioutil.TempDir("", "") d, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
return "", nil, errors.Wrap(err, "") return "", nil, err
} }
f, err := ioutil.TempFile(d, "") f, err := ioutil.TempFile(d, "")
return f.Name(), f, err return f.Name(), f, err
} }
// Get will download the specified hugo verion
func Get(version string, extended bool) (string, error) {
resp, err := http.Get(downloadURL(version, extended))
if err != nil {
return "", errors.Wrap(err, "")
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", errors.Wrap(err, "")
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := getTempFile()
if err != nil {
log.Printf("ERROR: %s", err)
return "", errors.Wrap(err, "")
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", errors.New("no hugo binary found")
}
if err != nil {
return "", errors.Wrap(err, "")
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
log.Fatal(err)
}
return hugoPath, nil
}
}
}

View File

@ -6,14 +6,14 @@ import (
func TestDownloadURL(t *testing.T) { func TestDownloadURL(t *testing.T) {
want := "https://github.com/gohugoio/hugo/releases/download/v1.0/hugo_1.0_Linux-64bit.tar.gz" want := "https://github.com/gohugoio/hugo/releases/download/v1.0/hugo_1.0_Linux-64bit.tar.gz"
if got := downloadURL("1.0", false); got != want { if got := download("1.0", false); got != want {
t.Errorf("Download url is not correct, got: %s, want: %s", got, want) t.Errorf("Download url is not correct, got: %s, want: %s", got, want)
} }
} }
func TestDownloadURLExtended(t *testing.T) { func TestDownloadURLExtended(t *testing.T) {
want := "https://github.com/gohugoio/hugo/releases/download/v0.55.4/hugo_extended_0.55.4_Linux-64bit.tar.gz" want := "https://github.com/gohugoio/hugo/releases/download/v0.55.4/hugo_extended_0.55.4_Linux-64bit.tar.gz"
if got := downloadURL("0.55.4", true); got != want { if got := download("0.55.4", true); got != want {
t.Errorf("Download url is not correct, got: %s, want: %s", got, want) t.Errorf("Download url is not correct, got: %s, want: %s", got, want)
} }
} }

35
main.go
View File

@ -19,24 +19,24 @@ func main() {
app.Version = version app.Version = version
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "buildDrafts", Name: "drafts",
Usage: " include content marked as draft", Usage: " include content marked as draft",
EnvVar: "PLUGIN_BUILDDRAFTS", EnvVar: "PLUGIN_BUILDDRAFTS,PLUGIN_DRAFTS",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "buildExpired", Name: "expired",
Usage: "include expired content", Usage: "include expired content",
EnvVar: "PLUGIN_BUILDEXPIRED", EnvVar: "PLUGIN_BUILDEXPIRED,PLUGIN_EXPIRED",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "buildFuture", Name: "future",
Usage: "include content with publishdate in the future", Usage: "include content with publishdate in the future",
EnvVar: "PLUGIN_BUILDFUTURE", EnvVar: "PLUGIN_BUILDFUTURE,PLUGIN_FUTURE",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "cacheDir", Name: "cache",
Usage: "change cache directory (useful when using caching plugins)", Usage: "change cache directory (useful when using caching plugins)",
EnvVar: "PLUGIN_CACHEDIR", EnvVar: "PLUGIN_CACHEDIR,PLUGIN_CACHE",
Value: "", Value: "",
}, },
cli.StringFlag{ cli.StringFlag{
@ -87,13 +87,13 @@ func main() {
EnvVar: "PLUGIN_VALIDATE", EnvVar: "PLUGIN_VALIDATE",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "hugoVersion", Name: "hugoversion",
Usage: "the hugo version to be used", Usage: "the hugo version to be used",
EnvVar: "PLUGIN_HUGO_VERSION", EnvVar: "PLUGIN_HUGO_VERSION,PLUGIN_VERSION",
Value: "", Value: "",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "hugoExtended", Name: "extended",
Usage: "If the hugo extended package should be used", Usage: "If the hugo extended package should be used",
EnvVar: "PLUGIN_EXTENDED", EnvVar: "PLUGIN_EXTENDED",
}, },
@ -106,11 +106,10 @@ func main() {
func run(c *cli.Context) error { func run(c *cli.Context) error {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
HugoVersion: c.String("hugoVersion"), URL: c.String("url"),
HugoExtended: c.Bool("hugoExtended"), Drafts: c.Bool("drafts"),
BuildDrafts: c.Bool("buildDrafts"), Expired: c.Bool("expired"),
BuildExpired: c.Bool("buildExpired"), Future: c.Bool("future"),
BuildFuture: c.Bool("buildFuture"),
Validate: c.Bool("validate"), Validate: c.Bool("validate"),
Config: c.String("config"), Config: c.String("config"),
Content: c.String("content"), Content: c.String("content"),
@ -118,9 +117,11 @@ func run(c *cli.Context) error {
Output: c.String("output"), Output: c.String("output"),
Source: c.String("source"), Source: c.String("source"),
Theme: c.String("theme"), Theme: c.String("theme"),
Url: c.String("url"), Version: c.String("hugoversion"),
Extended: c.Bool("extended"),
}, },
BuildInVersion: os.Getenv("PLUGIN_HUGO_SHIPPED_VERSION"), BuildInVersion: os.Getenv("PLUGIN_HUGO_SHIPPED_VERSION"),
} }
return plugin.Exec() return plugin.Exec()
} }

View File

@ -5,7 +5,7 @@ local test_pipeline_name = 'testing';
local windows(os) = os == 'windows'; local windows(os) = os == 'windows';
local golang_image(os, version) = local golang_image(os, version) =
'golang:' + '1.11' + if windows(os) then '-windowsservercore-' + version else ''; 'golang:' + '1.14' + if windows(os) then '-windowsservercore-' + version else '';
{ {
test(os='linux', arch='amd64', version=''):: test(os='linux', arch='amd64', version='')::

106
plugin.go
View File

@ -16,43 +16,53 @@ type (
} }
Config struct { Config struct {
BuildDrafts bool URL string
BuildExpired bool Drafts bool
BuildFuture bool Expired bool
CacheDir string Future bool
Validate bool
Cache string
Config string Config string
Content string Content string
Layout string Layout string
Output string Output string
Source string Source string
Theme string Theme string
Url string Version string
HugoVersion string Extended bool
HugoExtended bool
Validate bool
} }
) )
var hugoExecutable = "hugo" var (
hugoExecutable = "hugo"
)
// Exec executes the plugins functionality // Exec executes the plugins functionality
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
var cmds = make([]*exec.Cmd, 0) var cmds = make([]*exec.Cmd, 0)
// Check if buildIn plugin version equals if p.Config.Extended {
// plugin version declared in drone.yml hugoExecutable = "hugo-extended"
if !versionsEqual(p.BuildInVersion, p.Config.HugoVersion, p.Config.HugoExtended) {
hugoVersion := p.Config.HugoVersion
if hugoVersion == "" {
hugoVersion = p.BuildInVersion
} }
hugoPath, err := download.Get(hugoVersion, p.Config.HugoExtended) // Check if buildIn plugin version equals
// plugin version declared in drone.yml
if !versionsEqual(p.BuildInVersion, p.Config.Version) {
version := p.Config.Version
if version == "" {
version = p.BuildInVersion
}
executable, err := download.Get(version, p.Config.Extended)
if err != nil { if err != nil {
return err return err
} }
hugoExecutable = hugoPath
hugoExecutable = executable
} }
if p.Config.Validate { if p.Config.Validate {
cmds = append(cmds, commandValidate(p.Config)) cmds = append(cmds, commandValidate(p.Config))
} }
@ -63,75 +73,66 @@ func (p Plugin) Exec() error {
func commandValidate(config Config) *exec.Cmd { func commandValidate(config Config) *exec.Cmd {
args := []string{"check"} args := []string{"check"}
if config.Config != "" { if config.Config != "" {
args = append(args, "--config", config.Config) args = append(args, "--config", config.Config)
} }
return exec.Command(hugoExecutable, args...) return exec.Command(hugoExecutable, args...)
} }
func commandBuild(config Config) *exec.Cmd { func commandBuild(config Config) *exec.Cmd {
var args = make([]string, 0) var args = make([]string, 0)
// add bool args if config.Drafts {
if config.BuildDrafts {
args = append(args, "-D") args = append(args, "-D")
} }
if config.BuildExpired {
if config.Expired {
args = append(args, "-E") args = append(args, "-E")
} }
if config.BuildFuture {
if config.Future {
args = append(args, "-F") args = append(args, "-F")
} }
// add string args
if config.CacheDir != "" { if config.Cache != "" {
args = append(args, "--cacheDir", config.CacheDir) args = append(args, "--cacheDir", config.Cache)
} }
if config.Config != "" { if config.Config != "" {
args = append(args, "--config", config.Config) args = append(args, "--config", config.Config)
} }
if config.Content != "" { if config.Content != "" {
args = append(args, "--contentDir", config.Content) args = append(args, "--contentDir", config.Content)
} }
if config.Layout != "" { if config.Layout != "" {
args = append(args, "--layoutDir", config.Layout) args = append(args, "--layoutDir", config.Layout)
} }
if config.Output != "" { if config.Output != "" {
args = append(args, "--destination", config.Output) args = append(args, "--destination", config.Output)
} }
if config.Source != "" { if config.Source != "" {
args = append(args, "--source", config.Source) args = append(args, "--source", config.Source)
} }
if config.Theme != "" { if config.Theme != "" {
args = append(args, "--theme", config.Theme) args = append(args, "--theme", config.Theme)
} }
if config.Url != "" {
args = append(args, "--baseURL", config.Url) if config.URL != "" {
args = append(args, "--baseURL", config.URL)
} }
return exec.Command(hugoExecutable, args...) return exec.Command(hugoExecutable, args...)
} }
// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}
func versionsEqual(version string, toCompare string, extended bool) bool {
if extended {
return false
}
if toCompare == version || toCompare == "" {
return true
} else {
return false
}
}
// execAll executes a slice of commands as a batch job // execAll executes a slice of commands as a batch job
func execAll(cmds []*exec.Cmd) error { func execAll(cmds []*exec.Cmd) error {
// Execute all commands
for _, cmd := range cmds { for _, cmd := range cmds {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -141,5 +142,20 @@ func execAll(cmds []*exec.Cmd) error {
return err return err
} }
} }
return nil return nil
} }
func versionsEqual(version string, toCompare string) bool {
if toCompare == version || toCompare == "" {
return true
}
return false
}
// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}

View File

@ -1,8 +1,9 @@
package main package main
import ( import (
"github.com/pkg/errors"
"testing" "testing"
"github.com/pkg/errors"
) )
func TestCommandValidate(t *testing.T) { func TestCommandValidate(t *testing.T) {
@ -25,12 +26,12 @@ func TestCommandValidate(t *testing.T) {
func TestVersionEqual(t *testing.T) { func TestVersionEqual(t *testing.T) {
want := true want := true
if got := versionsEqual("1.0", "1.0", false); want != got { if got := versionsEqual("1.0", "1.0"); want != got {
t.Errorf("want: %t, got: %t", want, got) t.Errorf("want: %t, got: %t", want, got)
} }
want = false want = false
if got := versionsEqual("1.5", "1.0", false); want != got { if got := versionsEqual("1.5", "1.0"); want != got {
t.Errorf("want: %t, got: %t", want, got) t.Errorf("want: %t, got: %t", want, got)
} }
} }