From 581b346de97a9c6bc6bc19a98349973e7c3e7e49 Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Mon, 4 Apr 2022 19:00:40 +0200 Subject: [PATCH] improved Makefile cleanup --- .gitignore | 4 +++- Makefile | 28 ++++++++++++++++++---------- cmd/fan/rpm.go | 3 --- cmd/fan/speed.go | 3 --- cmd/root.go | 2 ++ cmd/sensor/sensor.go | 3 --- cmd/version.go | 12 +++++++++++- 7 files changed, 34 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 74b04fd..87edec1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ bin -fan2go.db +dist +fan2go*.db +fan2go*.yaml test.db \ No newline at end of file diff --git a/Makefile b/Makefile index ed96b15..7bb9508 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,24 @@ -BINARY_NAME=fan2go -OUTPUT_DIR=bin/ +GO_FLAGS ?= +NAME := fan2go +OUTPUT_BIN ?= bin/${NAME} +PACKAGE := github.com/markusressel/$(NAME) +GIT_REV ?= $(shell git rev-parse --short HEAD) +SOURCE_DATE_EPOCH ?= $(shell date +%s) +DATE ?= $(shell date -u -d @${SOURCE_DATE_EPOCH} +"%Y-%m-%dT%H:%M:%SZ") +VERSION ?= 0.6.0 -build: - go build -o ${OUTPUT_DIR}${BINARY_NAME} main.go +test: ## Run all tests + @go clean --testcache && go test -v ./... -run: - go build -o ${OUTPUT_DIR}${BINARY_NAME} main.go - ./${OUTPUT_DIR}${BINARY_NAME} +build: ## Builds the CLI + @go build ${GO_FLAGS} \ + -ldflags "-w -s -X ${PACKAGE}/cmd.version=${VERSION} -X ${PACKAGE}/cmd.commit=${GIT_REV} -X ${PACKAGE}/cmd.date=${DATE}" \ + -a -tags netgo -o ${OUTPUT_BIN} main.go -test: - sudo go test -v ./... +run: + go build -o ${OUTPUT_BIN} main.go + ./${OUTPUT_BIN} clean: go clean - rm ${OUTPUT_DIR}${BINARY_NAME} \ No newline at end of file + rm ${OUTPUT_BIN} \ No newline at end of file diff --git a/cmd/fan/rpm.go b/cmd/fan/rpm.go index e72b2e6..9c424f2 100644 --- a/cmd/fan/rpm.go +++ b/cmd/fan/rpm.go @@ -14,9 +14,6 @@ var rpmCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { pterm.DisableOutput() - fanIdFlag := cmd.Flag("id") - fanId := fanIdFlag.Value.String() - fan, err := getFan(fanId) if err != nil { return err diff --git a/cmd/fan/speed.go b/cmd/fan/speed.go index c1ca2a3..75b83f4 100644 --- a/cmd/fan/speed.go +++ b/cmd/fan/speed.go @@ -15,9 +15,6 @@ var speedCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { pterm.DisableOutput() - fanIdFlag := cmd.Flag("id") - fanId := fanIdFlag.Value.String() - fan, err := getFan(fanId) if err != nil { return err diff --git a/cmd/root.go b/cmd/root.go index e5d315e..13e3426 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,6 +15,8 @@ import ( ) var ( + version, commit, date = "dev", "dev", "" + cfgFile string noColor bool noStyle bool diff --git a/cmd/sensor/sensor.go b/cmd/sensor/sensor.go index 59a4fc8..5abb372 100644 --- a/cmd/sensor/sensor.go +++ b/cmd/sensor/sensor.go @@ -23,9 +23,6 @@ var Command = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { pterm.DisableOutput() - sensorIdFlag := cmd.Flag("id") - sensorId := sensorIdFlag.Value.String() - sensor, err := getSensor(sensorId) if err != nil { return err diff --git a/cmd/version.go b/cmd/version.go index c0de09f..f0d808d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -5,15 +5,25 @@ import ( "github.com/spf13/cobra" ) +var long bool + var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of fan2go", Long: `All software has versions. This is fan2go's`, Run: func(cmd *cobra.Command, args []string) { - ui.Printfln("0.6.0") + if verbose { + ui.Printfln("%s-%s-%s", version, commit, date) + } else if long { + ui.Printfln("%s-%s", version, commit) + } else { + ui.Printfln("%s", version) + } }, } func init() { + versionCmd.Flags().BoolVarP(&long, "long", "l", false, "Show the long version") + rootCmd.AddCommand(versionCmd) }