mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat: add version command (#26)
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
GO := go
|
GO := go
|
||||||
BIN_DIR := bin
|
BIN_DIR := bin
|
||||||
BIN := $(BIN_DIR)/pmg
|
BIN := $(BIN_DIR)/pmg
|
||||||
|
SHELL := /bin/bash
|
||||||
|
GITCOMMIT := $(shell git rev-parse HEAD)
|
||||||
|
VERSION := "$(shell git describe --tags --abbrev=0)-$(shell git rev-parse --short HEAD)"
|
||||||
|
|
||||||
|
GO_CFLAGS=-X 'github.com/safedep/pmg/cmd/version.commit=$(GITCOMMIT)' -X 'github.com/safedep/pmg/cmd/version.version=$(VERSION)'
|
||||||
|
GO_LDFLAGS=-ldflags "-w $(GO_CFLAGS)"
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
|
|
||||||
all: pmg
|
all: pmg
|
||||||
|
|
||||||
pmg: create_bin
|
pmg: create_bin
|
||||||
$(GO) build -o $(BIN) main.go
|
$(GO) build ${GO_LDFLAGS} -o $(BIN) main.go
|
||||||
|
|
||||||
create_bin:
|
create_bin:
|
||||||
mkdir -p $(BIN_DIR)
|
mkdir -p $(BIN_DIR)
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
runtimeDebug "runtime/debug"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
version string
|
||||||
|
commit string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if version == "" {
|
||||||
|
buildInfo, _ := runtimeDebug.ReadBuildInfo()
|
||||||
|
version = buildInfo.Main.Version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVersionCommand() *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Short: "Show version and build information",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
fmt.Fprintf(os.Stdout, "Version: %s\n", version)
|
||||||
|
fmt.Fprintf(os.Stdout, "CommitSHA: %s\n", commit)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/safedep/dry/log"
|
"github.com/safedep/dry/log"
|
||||||
"github.com/safedep/pmg/cmd/npm"
|
"github.com/safedep/pmg/cmd/npm"
|
||||||
|
"github.com/safedep/pmg/cmd/version"
|
||||||
"github.com/safedep/pmg/config"
|
"github.com/safedep/pmg/config"
|
||||||
"github.com/safedep/pmg/internal/ui"
|
"github.com/safedep/pmg/internal/ui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -76,6 +77,7 @@ func main() {
|
|||||||
|
|
||||||
cmd.AddCommand(npm.NewNpmCommand())
|
cmd.AddCommand(npm.NewNpmCommand())
|
||||||
cmd.AddCommand(npm.NewPnpmCommand())
|
cmd.AddCommand(npm.NewPnpmCommand())
|
||||||
|
cmd.AddCommand(version.NewVersionCommand())
|
||||||
|
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user