Skip to content

Commit

Permalink
feat(project): initializing project
Browse files Browse the repository at this point in the history
  • Loading branch information
zackijack committed Jun 18, 2021
0 parents commit ccd36be
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Build output
go-project

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Common
.DS_Store
.idea
*.log
.vscode
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
SHELL = /bin/bash

APP_NAME = go-project
VERSION := $(shell git describe --always --tags)
GIT_COMMIT = $(shell git rev-parse HEAD)
BUILD_DATE = $(shell date '+%Y-%m-%d-%H:%M:%S')

.PHONY: default
default: help

.PHONY: help
help:
@echo 'Make commands for ${APP_NAME}:'
@echo
@echo 'Usage:'
@echo ' make build Compile the project.'
@echo ' make run ARGS= Run with supplied arguments.'
@echo ' make test Run tests on a compiled project.'
@echo ' make clean Clean the directory tree.'

@echo

.PHONY: build
build:
@echo "Building ${APP_NAME} ${VERSION}"
go build -ldflags "-w -X github.com/zackijack/go-project/internal/version.Version=${VERSION} -X github.com/zackijack/go-project/internal/version.GitCommit=${GIT_COMMIT} -X github.com/zackijack/go-project/internal/version.BuildDate=${BUILD_DATE}" -o ${APP_NAME}

.PHONY: run
run: build
@echo "Running ${APP_NAME} ${VERSION}"
bin/${APP_NAME} ${ARGS}

.PHONY: test
test:
@echo "Testing ${APP_NAME} ${VERSION}"
go mod tidy
go test ./...

.PHONY: clean
clean:
@echo "Removing ${APP_NAME} ${VERSION}"
@test ! -e bin/${APP_NAME} || rm bin/${APP_NAME}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# go-project
Go project template
65 changes: 65 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

const completionDesc = `
To load completions:
Bash:
$ source <(go-project completion bash)
# To load completions for each session, execute once:
Linux:
$ go-project completion bash > /etc/bash_completion.d/go-project
MacOS:
$ go-project completion bash > /usr/local/etc/bash_completion.d/go-project
Zsh:
# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ go-project completion zsh > "${fpath[1]}/_go-project"
# You will need to start a new shell for this setup to take effect.
Fish:
$ go-project completion fish | source
# To load completions for each session, execute once:
$ go-project completion fish > ~/.config/fish/completions/go-project.fish
`

var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: completionDesc,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}

func init() {
rootCmd.AddCommand(completionCmd)
}
26 changes: 26 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-project",
Short: "Go project template",
}

// 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() {
}
43 changes: 43 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"

"github.com/zackijack/go-project/internal/version"
"github.com/spf13/cobra"
)

const versionDesc = `
Showing who exactly go-project is.
This will print a representation profile of go-project.
The output will look something like this:
Version: v1.0.0
Git Commit: ff52399e51bb880526e9cd0ed8386f6433b74da1
Build Date: 2020-07-27-17:07:02
Go Version: go1.14.6
OS Arch: darwin amd64
- Version is the semantic version of the release.
- Git Commit is the SHA for the commit that this version was built from.
- Build Date is the date when the application was built.
- Go Version is the version of the Go compiler used.
- OS Arch is the version of the operating system and architecture was built for.
`

var versionCmd = &cobra.Command{
Use: "version",
Short: "Reveal go-project personal information",
Long: versionDesc,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Version:", version.Version)
fmt.Println("Git Commit:", version.GitCommit)
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Go Version:", version.GoVersion)
fmt.Println("OS Arch:", version.OsArch)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/zackijack/go-project

go 1.16

require github.com/spf13/cobra v1.1.3
Loading

0 comments on commit ccd36be

Please sign in to comment.