Skip to content

Commit

Permalink
initial code (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz authored Apr 3, 2021
1 parent 4754336 commit 10a7a7f
Show file tree
Hide file tree
Showing 18 changed files with 971 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: l3uddz
157 changes: 157 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Build

on:
push:
branches:
- '*'
tags:
- 'v*'

jobs:
build:
runs-on: ubuntu-latest
steps:
# dependencies
- name: goreleaser
run: |
curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sudo sh -s -- -b /usr/local/bin
- name: qemu
uses: docker/setup-qemu-action@v1

- name: buildx
uses: docker/setup-buildx-action@v1

# checkout
- name: checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

# setup go
- name: go
uses: actions/setup-go@v1
with:
go-version: 1.16

- name: go info
run: |
go version
go env
# cache
- name: cache
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# vendor
- name: vendor
run: |
make vendor
# git status
- name: git status
run: git status

# build
- name: build
if: startsWith(github.ref, 'refs/tags/') == false
run: |
make snapshot
# publish
- name: publish
if: startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REF: ${{ github.ref }}
run: |
make publish
# artifacts
- name: artifact_linux
if: ${{!github.event.repository.private}}
uses: actions/upload-artifact@v2-preview
with:
name: build_linux
path: dist/*linux*

- name: artifact_darwin
if: ${{!github.event.repository.private}}
uses: actions/upload-artifact@v2-preview
with:
name: build_darwin
path: dist/*darwin*

- name: artifact_windows
if: ${{!github.event.repository.private}}
uses: actions/upload-artifact@v2-preview
with:
name: build_windows
path: dist/*windows*

# docker login
- name: docker login
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin
# docker build (latest & tag)
- name: release tag
if: startsWith(github.ref, 'refs/tags/') == true
uses: little-core-labs/[email protected]
id: releasetag
with:
tagRegex: "v?(.+)"

- name: docker - build release
if: startsWith(github.ref, 'refs/tags/') == true
uses: docker/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
platforms: linux/amd64,linux/arm64,linux/arm/v7
pull: true
push: true
tags: |
cloudb0x/cvm:${{ steps.releasetag.outputs.tag }}
cloudb0x/cvm:latest
# docker build (branch)
- name: branch name
if: startsWith(github.ref, 'refs/tags/') == false
id: branch-name
uses: tj-actions/[email protected]

- name: docker tag
if: startsWith(github.ref, 'refs/tags/') == false
uses: frabert/replace-string-action@master
id: dockertag
with:
pattern: '[:\.\/]+'
string: "${{ steps.branch-name.outputs.current_branch }}"
replace-with: '-'
flags: 'g'

- name: docker - build branch
if: startsWith(github.ref, 'refs/tags/') == false
uses: docker/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
platforms: linux/amd64,linux/arm64,linux/arm/v7
pull: true
push: true
tags: |
cloudb0x/cvm:${{ steps.dockertag.outputs.replaced }}
# cleanup
- name: cleanup
run: |
rm -f ${HOME}/.docker/config.json
26 changes: 26 additions & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Docker Cleanup

on: delete

jobs:
cleanup_branch:
if: startsWith(github.event.ref_type, 'branch') == true
runs-on: ubuntu-latest
steps:
- name: docker tag
uses: frabert/replace-string-action@master
id: dockertag
with:
pattern: '[:\.\/]+'
string: "${{ github.event.ref }}"
replace-with: '-'
flags: 'g'

- name: remove docker tag
shell: bash
env:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tag: ${{ steps.dockertag.outputs.replaced }}
run: |
docker run --rm lumir/remove-dockerhub-tag --user "$username" --password "$password" cloudb0x/cvm:$tag
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# misc
/.idea/
/.vscode/

# configuration
config.yml

# vendor files
/vendor/

# dist folder
/dist/
49 changes: 49 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# https://goreleaser.com
project_name: cvm

# Build
builds:
-
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
main: ./cmd/cvm
goarch:
- amd64
- arm64
- arm
goarm:
- 7
ldflags:
- -s -w
- -X "github.com/Cloudbox/cvm/build.Version={{ .Version }}"
- -X "github.com/Cloudbox/cvm/build.GitCommit={{ .ShortCommit }}"
- -X "github.com/Cloudbox/cvm/build.Timestamp={{ .Timestamp }}"
flags:
- -trimpath

# Archive
archives:
-
name_template: "{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}"
format: "binary"

# Checksum
checksum:
name_template: "checksums.txt"
algorithm: sha512

# Snapshot
snapshot:
name_template: "{{ .Major }}.{{ .Minor }}.{{ .Patch }}-dev+{{ .ShortCommit }}"

# Changelog
changelog:
filters:
exclude:
- "^docs:"
- "^test:"
- "^Merge branch"
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.DEFAULT_GOAL := build
CMD := cvm
TARGET := $(shell go env GOOS)_$(shell go env GOARCH)
DIST_PATH := dist
BUILD_PATH := ${DIST_PATH}/${CMD}_${TARGET}
GO_FILES := $(shell find . -path ./vendor -prune -or -type f -name '*.go' -print)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
TIMESTAMP := $(shell date +%s)
VERSION ?= 0.0.0-dev
CGO := 0

# Deps
.PHONY: check_goreleaser
check_goreleaser:
@command -v goreleaser >/dev/null || (echo "goreleaser is required."; exit 1)

.PHONY: test
test: ## Run tests
go test ./... -cover -v -race ${GO_PACKAGES}

.PHONY: vendor
vendor: ## Vendor files and tidy go.mod
go mod vendor
go mod tidy

.PHONY: vendor_update
vendor_update: ## Update vendor dependencies
go get -u ./...
${MAKE} vendor

.PHONY: build
build: vendor ${BUILD_PATH}/${CMD} ## Build application

# Binary
${BUILD_PATH}/${CMD}: ${GO_FILES} go.sum
@echo "Building for ${TARGET}..." && \
mkdir -p ${BUILD_PATH} && \
CGO_ENABLED=${CGO} go build \
-mod vendor \
-trimpath \
-ldflags "-s -w -X github.com/Cloudbox/cvm/build.Version=${VERSION} -X github.com/Cloudbox/cvm/build.GitCommit=${GIT_COMMIT} -X github.com/Cloudbox/cvm/build.Timestamp=${TIMESTAMP}" \
-o ${BUILD_PATH}/${CMD} \
./cmd/cvm

.PHONY: release
release: check_goreleaser ## Generate a release, but don't publish
goreleaser --skip-validate --skip-publish --rm-dist

.PHONY: publish
publish: check_goreleaser ## Generate a release, and publish
goreleaser --rm-dist

.PHONY: snapshot
snapshot: check_goreleaser ## Generate a snapshot release
goreleaser --snapshot --skip-publish --rm-dist

.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
15 changes: 15 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package build

import "fmt"

var (
Version string
Timestamp string
GitCommit string

UserAgent string
)

func init() {
UserAgent = fmt.Sprintf("cvm/%s", Version)
}
45 changes: 45 additions & 0 deletions cmd/cvm/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"os"
"path/filepath"
)

func GetDefaultConfigDirectory(app string, filename string) string {
// binary path
bcd := getBinaryPath()
if _, err := os.Stat(filepath.Join(bcd, filename)); err == nil {
// there is a config file in the binary path
// so use this directory as the default
return bcd
}

// config dir
ucd, err := os.UserConfigDir()
if err != nil {
panic(fmt.Sprintf("userconfigdir: %v", err))
}

acd := filepath.Join(ucd, app)
if _, err := os.Stat(acd); os.IsNotExist(err) {
if err := os.MkdirAll(acd, os.ModePerm); err != nil {
panic(fmt.Sprintf("mkdirall: %v", err))
}
}

return acd
}

func getBinaryPath() string {
// get current binary path
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
// get current working dir
if dir, err = os.Getwd(); err != nil {
panic(fmt.Sprintf("getwd: %v", err))
}
}

return dir
}
Loading

0 comments on commit 10a7a7f

Please sign in to comment.