diff --git a/posix/posix.go b/posix/posix.go index 6376668..d6c30a2 100644 --- a/posix/posix.go +++ b/posix/posix.go @@ -1 +1,3 @@ package posix + +//go:generate go run ../scripts/includetext.go --input=clone --input=clone-commit --input=clone-pull-request --input=clone-tag --package=posix --output=posix_gen.go diff --git a/posix/posix_gen.go b/posix/posix_gen.go new file mode 100644 index 0000000..e4b876c --- /dev/null +++ b/posix/posix_gen.go @@ -0,0 +1,135 @@ +package posix + +// DO NOT EDIT. This file is automatically generated. + +// Contents of clone +const Clone = `#!/bin/sh + +if [[ ! -z "${DRONE_WORKSPACE}" ]]; then + cd ${DRONE_WORKSPACE} +fi + +# if the netrc enviornment variables exist, write +# the netrc file. + +if [[ ! -z "${DRONE_NETRC_MACHINE}" ]]; then + cat < /root/.netrc +machine ${DRONE_NETRC_MACHINE} +login ${DRONE_NETRC_USERNAME} +password ${DRONE_NETRC_PASSWORD} +EOF +fi + +# if the ssh_key environment variable exists, write +# the ssh key and add the netrc machine to the +# known hosts file. + +if [[ ! -z "${SSH_KEY}" ]]; then + mkdir /root/.ssh + echo -n "$SSH_KEY" > /root/.ssh/id_rsa + chmod 600 /root/.ssh/id_rsa + + touch /root/.ssh/known_hosts + chmod 600 /root/.ssh/known_hosts + ssh-keyscan -H ${DRONE_NETRC_MACHINE} > /etc/ssh/ssh_known_hosts 2> /dev/null +fi + +# configure git global behavior and parameters via the +# following environment variables: + + +if [[ -z "${DRONE_COMMIT_AUTHOR_NAME}" ]]; then + export DRONE_COMMIT_AUTHOR_NAME=drone +fi + +if [[ -z "${DRONE_COMMIT_AUTHOR_EMAIL}" ]]; then + export DRONE_COMMIT_AUTHOR_EMAIL=drone@localhost +fi + +export GIT_AUTHOR_NAME=${DRONE_COMMIT_AUTHOR_NAME} +export GIT_AUTHOR_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL} +export GIT_COMMITTER_NAME=${DRONE_COMMIT_AUTHOR_NAME} +export GIT_COMMITTER_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL} + +# invoke the sub-script based on the drone event type. +# TODO we should ultimately look at the ref, since +# we need something compatible with deployment events. + +CLONE_TYPE=$DRONE_BUILD_EVENT +case $DRONE_COMMIT_REF in + refs/tags/* ) CLONE_TYPE=tag ;; +esac + +case $CLONE_TYPE in +pull_request) + clone-pull-request + ;; +tag) + clone-tag + ;; +*) + clone-commit + ;; +esac +` +// Contents of clone-commit +const CloneCommit = `#!/bin/sh + +FLAGS="" +if [[ ! -z "${PLUGIN_DEPTH}" ]]; then + FLAGS="--depth=${PLUGIN_DEPTH}" +fi + +if [ ! -d .git ]; then + git init + git remote add origin ${DRONE_REMOTE_URL} +fi + +set -e +set -x + +git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}: +git checkout ${DRONE_COMMIT_SHA} -b ${DRONE_COMMIT_BRANCH} +` +// Contents of clone-pull-request +const ClonePullRequest = `#!/bin/sh + +FLAGS="" +if [[ ! -z "${PLUGIN_DEPTH}" ]]; then + FLAGS="--depth=${PLUGIN_DEPTH}" +fi + +if [ ! -d .git ]; then + git init + git remote add origin ${DRONE_REMOTE_URL} +fi + +set -e +set -x + +git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}: +git checkout ${DRONE_COMMIT_BRANCH} + +git fetch origin ${DRONE_COMMIT_REF}: +git merge ${DRONE_COMMIT_SHA} +` +// Contents of clone-tag +const CloneTag = `#!/bin/sh + +FLAGS="" +if [[ ! -z "${PLUGIN_DEPTH}" ]]; then + FLAGS="--depth=${PLUGIN_DEPTH}" +fi + +if [ ! -d .git ]; then + git init + git remote add origin ${DRONE_REMOTE_URL} +fi + +set -e +set -x + +git fetch ${FLAGS} origin +refs/tags/${DRONE_TAG}: +git checkout -qf FETCH_HEAD +` + diff --git a/scripts/includetext.go b/scripts/includetext.go new file mode 100644 index 0000000..6386450 --- /dev/null +++ b/scripts/includetext.go @@ -0,0 +1,77 @@ +// +build ignore + +package main + +import ( + "bytes" + "flag" + "io/ioutil" + "log" + "strings" + "text/template" +) + +var ( + input stringSlice + output string + name string +) + +func main() { + flag.Var(&input, "input", "input files") + flag.StringVar(&output, "output", "", "output file") + flag.StringVar(&name, "package", "", "package name") + flag.Parse() + + var files []File + for _, file := range input { + out, err := ioutil.ReadFile(file) + if err != nil { + log.Fatalln(err) + } + files = append(files, File{ + Name: file, + Slug: strings.ReplaceAll(strings.Title(file), "-", ""), + Data: string(out), + }) + } + + data := map[string]interface{}{ + "Files": files, + "Package": name, + } + buf := new(bytes.Buffer) + err := tmpl.Execute(buf, data) + if err != nil { + log.Fatalln(err) + } + + ioutil.WriteFile(output, buf.Bytes(), 0644) +} + +type stringSlice []string + +func (s *stringSlice) String() string { + return strings.Join(*s, ",") +} + +func (s *stringSlice) Set(value string) error { + *s = append(*s, value) + return nil +} + +type File struct { + Name string + Data string + Slug string +} + +var tmpl = template.Must(template.New("_").Parse(`package {{ .Package }} + +// DO NOT EDIT. This file is automatically generated. + +{{ range .Files -}} +// Contents of {{ .Name }} +const {{ .Slug }} = ` + "`{{ .Data }}`" + ` +{{ end }} +`))