From 1ea90f91aaee27b96ffc68b217321d967e97ff90 Mon Sep 17 00:00:00 2001 From: nicksherron Date: Fri, 7 Feb 2020 03:02:18 -0500 Subject: [PATCH] Initial Commit. --- .gitignore | 2 ++ .travis.yml | 9 ++++++ AUTHORS.md | 9 ++++++ CONTRIBUTING.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 40 +++++++++++++++++++++++ Makefile | 61 ++++++++++++++++++++++++++++++++++ README.md | 18 +++++++++++ cmd/root.go | 43 ++++++++++++++++++++++++ cmd/version.go | 25 ++++++++++++++ go.mod | 7 ++++ main.go | 13 ++++++++ version/version.go | 21 ++++++++++++ 12 files changed, 329 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 AUTHORS.md create mode 100644 CONTRIBUTING.md create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/root.go create mode 100644 cmd/version.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 version/version.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7d73bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +vendor \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a996edb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: go + +go: + - 1.8.x + - master + +install: + - go get -v github.com/Masterminds/glide + - make get-deps \ No newline at end of file diff --git a/AUTHORS.md b/AUTHORS.md new file mode 100644 index 0000000..7ded613 --- /dev/null +++ b/AUTHORS.md @@ -0,0 +1,9 @@ +# Credits + +## Development Lead + +- Nick Sherron [nicksherron](https://github.com/nicksherron) + +## Contributors + +None yet. Why not be the first? \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c113e11 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,81 @@ +# Contributing + +Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given. + +You can contribute in many ways: + +## Types of Contributions + +### Report Bugs + +Report bugs at https://github.com/nicksherron/bashhub-server/issues. + +If you are reporting a bug, please include: + +* Your operating system name and version. +* Any details about your local setup that might be helpful in troubleshooting. +* Detailed steps to reproduce the bug. + +### Fix Bugs + +Look through the GitHub issues for bugs. Anything tagged with "bug" +is open to whoever wants to implement it. + +### Implement Features + +Look through the GitHub issues for features. Anything tagged with "feature" +is open to whoever wants to implement it. + +### Write Documentation + +bashhub-server could always use more documentation, whether as part of the +official bashhub-server docs, in docstrings, or even on the web in blog posts, +articles, and such. + +### Submit Feedback + +The best way to send feedback is to file an issue at https://github.com/nicksherron/bashhub-server/issues. + +If you are proposing a feature: + +* Explain in detail how it would work. +* Keep the scope as narrow as possible, to make it easier to implement. +* Remember that this is a volunteer-driven project, and that contributions + are welcome :) + +## Get Started! + +Ready to contribute? Here's how to set up `bashhub-server` for local development. + +1. Fork the `bashhub-server` repo on GitHub. +2. Clone your fork locally:: +```bash + $ git clone git@github.com:your_name_here/bashhub-server.git +``` +3. Create a branch for local development:: +```bash + $ git checkout -b name-of-your-bugfix-or-feature +``` + Now you can make your changes locally. + +4. When you're done making changes, check that your changes pass the tests:: +```bash + $ make test +``` +6. Commit your changes and push your branch to GitHub:: +```bash + $ git add . + $ git commit -m "Your detailed description of your changes." + $ git push origin name-of-your-bugfix-or-feature +``` +7. Submit a pull request through the GitHub website. + +Pull Request Guidelines +----------------------- + +Before you submit a pull request, check that it meets these guidelines: + +1. The pull request should include tests. +2. If the pull request adds functionality, the docs should be updated. Put + your new functionality into a function with a docstring, and add the + feature to the list in README.md. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7977720 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +# Build Stage +FROM nicksherron/bashhub-server-build:1.13 AS build-stage + +LABEL app="build-bashhub-server" +LABEL REPO="https://github.com/nicksherron/bashhub-server" + +ENV PROJPATH=/go/src/github.com/nicksherron/bashhub-server + +# Because of https://github.com/docker/docker/issues/14914 +ENV PATH=$PATH:$GOROOT/bin:$GOPATH/bin + +ADD . /go/src/github.com/nicksherron/bashhub-server +WORKDIR /go/src/github.com/nicksherron/bashhub-server + +RUN make build-alpine + +# Final Stage +FROM nicksherron/bashhub-server + +ARG GIT_COMMIT +ARG VERSION +LABEL REPO="https://github.com/nicksherron/bashhub-server" +LABEL GIT_COMMIT=$GIT_COMMIT +LABEL VERSION=$VERSION + +# Because of https://github.com/docker/docker/issues/14914 +ENV PATH=$PATH:/opt/bashhub-server/bin + +WORKDIR /opt/bashhub-server/bin + +COPY --from=build-stage /go/src/github.com/nicksherron/bashhub-server/bin/bashhub-server /opt/bashhub-server/bin/ +RUN chmod +x /opt/bashhub-server/bin/bashhub-server + +# Create appuser +RUN adduser -D -g '' bashhub-server +USER bashhub-server + +ENTRYPOINT ["/usr/bin/dumb-init", "--"] + +CMD ["/opt/bashhub-server/bin/bashhub-server"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..890ddd2 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +.PHONY: build build-alpine clean test help default + +BIN_NAME=bashhub-server + +VERSION := $(shell grep "const Version " version/version.go | sed -E 's/.*"(.+)"$$/\1/') +GIT_COMMIT=$(shell git rev-parse HEAD) +GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true) +BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S') +IMAGE_NAME := "nicksherron/bashhub-server" + +default: test + +help: + @echo 'Management commands for bashhub-server:' + @echo + @echo 'Usage:' + @echo ' make build Compile the project.' + @echo ' make get-deps runs dep ensure, mostly used for ci.' + @echo ' make build-alpine Compile optimized for alpine linux.' + @echo ' make package Build final docker image with just the go binary inside' + @echo ' make tag Tag image created by package with latest, git commit and version' + @echo ' make test Run tests on a compiled project.' + @echo ' make push Push tagged images to registry' + @echo ' make clean Clean the directory tree.' + @echo + +build: + @echo "building ${BIN_NAME} ${VERSION}" + @echo "GOPATH=${GOPATH}" + go build -ldflags "-X github.com/nicksherron/bashhub-server/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/version.BuildDate=${BUILD_DATE}" -o bin/${BIN_NAME} + +get-deps: + dep ensure + +build-alpine: + @echo "building ${BIN_NAME} ${VERSION}" + @echo "GOPATH=${GOPATH}" + go build -ldflags '-w -linkmode external -extldflags "-static" -X github.com/nicksherron/bashhub-server/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/nicksherron/bashhub-server/version.BuildDate=${BUILD_DATE}' -o bin/${BIN_NAME} + +package: + @echo "building image ${BIN_NAME} ${VERSION} $(GIT_COMMIT)" + docker build --build-arg VERSION=${VERSION} --build-arg GIT_COMMIT=$(GIT_COMMIT) -t $(IMAGE_NAME):local . + +tag: + @echo "Tagging: latest ${VERSION} $(GIT_COMMIT)" + docker tag $(IMAGE_NAME):local $(IMAGE_NAME):$(GIT_COMMIT) + docker tag $(IMAGE_NAME):local $(IMAGE_NAME):${VERSION} + docker tag $(IMAGE_NAME):local $(IMAGE_NAME):latest + +push: tag + @echo "Pushing docker image to registry: latest ${VERSION} $(GIT_COMMIT)" + docker push $(IMAGE_NAME):$(GIT_COMMIT) + docker push $(IMAGE_NAME):${VERSION} + docker push $(IMAGE_NAME):latest + +clean: + @test ! -e bin/${BIN_NAME} || rm bin/${BIN_NAME} + +test: + go test ./... + diff --git a/README.md b/README.md new file mode 100644 index 0000000..37f1ce7 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# bashhub-server + +Private BashHub server + +## Getting started + +This project requires Go to be installed. On OS X with Homebrew you can just run `brew install go`. + +Running it then should be as simple as: + +```console +$ make +$ ./bin/bashhub-server +``` + +### Testing + +``make test`` \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..31af1cf --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var cfgFile string + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "generated code example", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func init() { + cobra.OnInitialize() + + // Cobra also supports local flags, which will only run + // when this action is called directly. + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..15a1f30 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "fmt" + "github.com/lacion/cookiecutter_golang_example/version" + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of generated code example", + Long: `All software has versions. This is generated code example`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Build Date:", version.BuildDate) + fmt.Println("Git Commit:", version.GitCommit) + fmt.Println("Version:", version.Version) + fmt.Println("Go Version:", version.GoVersion) + fmt.Println("OS / Arch:", version.OsArch) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0047406 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/nicksherron/bashhub-server + +require ( + + github.com/spf13/cobra v0.0.3 + +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..043ca04 --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + + "github.com/nicksherron/bashhub-server/cmd" +) + +func main() { + + + cmd.Execute() + +} diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..0817b7b --- /dev/null +++ b/version/version.go @@ -0,0 +1,21 @@ +package version + +import ( + "fmt" + "runtime" +) + +// GitCommit returns the git commit that was compiled. This will be filled in by the compiler. +var GitCommit string + +// Version returns the main version number that is being run at the moment. +const Version = "0.1.0" + +// BuildDate returns the date the binary was built +var BuildDate = "" + +// GoVersion returns the version of the go runtime used to compile the binary +var GoVersion = runtime.Version() + +// OsArch returns the os and arch used to build the binary +var OsArch = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)