Commit 54cdc383 authored by Addshore's avatar Addshore 🏄 Committed by Jeena Huneidi
Browse files

Output more useful version info

This will make it easier for people to give feedback while
using various development builds
(This will also be useful when we do real releases)

This required a bunch of changes to the MakeFile to
actually get the build dependencies loading when required.
The way "staticfiles" was done in the past wasn't great.

Change-Id: Icdbe959dcf429305ebcec57cbe64bd05b0ded737
parent ed8c6e6b
GOPATH=$(shell pwd)/vendor:$(shell pwd)
GOBIN=$(shell pwd)/bin
GONAME=mw
SHELL := /bin/bash
RELEASE_DIR ?= ./_release
TARGETS ?= darwin/amd64 linux/amd64 linux/386 linux/arm linux/arm64 linux/ppc64le windows/amd64
......@@ -7,57 +11,55 @@ PACKAGE := gerrit.wikimedia.org/r/mediawiki/tools/cli
GO_LIST_GOFILES := '{{range .GoFiles}}{{printf "%s/%s\n" $$.Dir .}}{{end}}{{range .XTestGoFiles}}{{printf "%s/%s\n" $$.Dir .}}{{end}}'
GO_PACKAGES := $(shell go list ./...)
all: get-dev generate get build
GO_LDFLAGS := \
-X $(PACKAGE)/meta.Version=$(shell cat VERSION) \
-X $(PACKAGE)/meta.GitCommit=$(shell git rev-parse --short HEAD)
get:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get .
# go build/install commands
#
GO_BUILD := go build -v -ldflags "$(GO_LDFLAGS)" -o bin/mw
GO_INSTALL := go install -v -ldflags "$(GO_LDFLAGS)"
get-dev:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get github.com/ahmetb/govvv@v0.3.0
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get bou.ke/staticfiles@v0.0.0-20210106104248-dd04075d4104
all: code internal/mwdd/files/files.go mw-cli
build:
@echo "Building $(GOFILES) to ./bin"
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -v -ldflags "$(shell ./bin/govvv -flags)" -o bin/cli ./
mw-cli:
$(GO_BUILD) ./cmd/cli
release:
GOPATH=$(GOPATH) GOBIN=$(GOBIN) gox -output="$(RELEASE_DIR)/{{.OS}}-{{.Arch}}/mw" -osarch='$(TARGETS)' -ldflags '$(GO_LDFLAGS)' $(GO_PACKAGES)
cp LICENSE "$(RELEASE_DIR)"
for f in "$(RELEASE_DIR)"/*/mw; do \
shasum -a 256 "$${f}" | awk '{print $$1}' > "$${f}.sha256"; \
done
code:
go generate $(GO_PACKAGES)
install: all
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install -v $(GO_PACKAGES)
generate: internal/mwdd/files/files.go
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(GO_PACKAGES)
internal/mwdd/files/files.go: static/mwdd/*
rm -f internal/mwdd/files/files.go || true
./bin/staticfiles -o internal/mwdd/files/files.go static/mwdd/
clean:
go clean $(GO_PACKAGES)
rm -f bin/mw || true
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean $(GO_PACKAGES)
rm -rf bin || true
rm -rf _releases || true
rm internal/mwdd/files/files.go || true
install: all
$(GO_INSTALL) $(GO_PACKAGES)
test: get-dev generate unit lint
release:
gox -output="$(RELEASE_DIR)/{{.OS}}-{{.Arch}}/mw" -osarch='$(TARGETS)' -ldflags '$(GO_LDFLAGS)' $(GO_PACKAGES)
cp LICENSE "$(RELEASE_DIR)"
for f in "$(RELEASE_DIR)"/*/mw; do \
shasum -a 256 "$${f}" | awk '{print $$1}' > "$${f}.sha256"; \
done
unit:
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go test -cover -ldflags "$(shell ./bin/govvv -flags)" $(GO_PACKAGES)
lint:
@echo > .lint-gofmt.diff
@go list -f $(GO_LIST_GOFILES) $(GO_PACKAGES) | while read f; do \
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go list -f $(GO_LIST_GOFILES) $(GO_PACKAGES) | while read f; do \
gofmt -e -d "$${f}" >> .lint-gofmt.diff; \
done
@test -z "$(grep '[^[:blank:]]' .lint-gofmt.diff)" || (echo "gofmt found errors:"; cat .lint-gofmt.diff; exit 1)
golint -set_exit_status $(GO_PACKAGES)
go vet -composites=false $(GO_PACKAGES)
unit:
go test -cover -ldflags "$(GO_LDFLAGS)" $(GO_PACKAGES)
GOPATH=$(GOPATH) GOBIN=$(GOBIN) golint -set_exit_status $(GO_PACKAGES)
GOPATH=$(GOPATH) GOBIN=$(GOBIN) go vet -composites=false $(GO_PACKAGES)
test: internal/mwdd/files/files.go unit lint
internal/mwdd/files/files.go: static/mwdd/*
go get bou.ke/staticfiles@v0.0.0-20210106104248-dd04075d4104
rm -f internal/mwdd/files/files.go
staticfiles -o internal/mwdd/files/files.go static/mwdd/
.PHONY: install release
......@@ -17,10 +17,9 @@ Clone this repository to your `$GOPATH` (probably `~/go`), so it would be at
Within the `~/go/src/gerrit.wikimedia.org/r/mediawiki/tools/cli/cmd` directory:
- run `go mod download` to download the required modules
- run `go mod vendor` to copy the required modules into a vendor directory for the project
- run `make` to download dependencies and build an initial binary
Execute the tool from any directory by running the `./dev.sh` script.
Execute the tool without building from any directory by running the `./dev.sh` script.
### Packages & Directories
......@@ -45,9 +44,9 @@ No naming structured is enforced in CI but a convention exists that should be fo
### Using a binary
Make a binary by running `make install`
Make a binary by running `make`
Execute the binary from any directory with `~/go/src/gerrit.wikimedia.org/r/mediawiki/tools/cli/bin/mw`
Execute the binary from any directory with `~/go/src/gerrit.wikimedia.org/r/mediawiki/tools/cli/bin/cli`
## Support
......
......@@ -53,13 +53,33 @@ var Env []string
// Workdir run the docker command with this working directory
var Workdir string
// GitCommit holds short commit hash of source tree
var GitCommit string
// GitBranch holds current branch name the code is built off
var GitBranch string
// GitState shows whether there are uncommitted changes
var GitState string
// GitSummary holds output of git describe --tags --dirty --always
var GitSummary string
// BuildDate holds RFC3339 formatted UTC date (build time)
var BuildDate string
// Version holds contents of ./VERSION file, if exists, or the value passed via the -version option
var Version string
var rootCmd = &cobra.Command{
Use: "mw",
Short: "Developer utilities for working with MediaWiki",
}
/*Execute the root command*/
func Execute() {
func Execute(GitCommitIn string, GitBranchIn string, GitStateIn string, GitSummaryIn string, BuildDateIn string, VersionIn string) {
GitCommit = GitCommitIn
GitBranch = GitBranchIn
GitState = GitStateIn
GitSummary = GitSummaryIn
BuildDate = BuildDateIn
Version = VersionIn
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
......
/*Package cmd is used for command line.
Copyright © 2020 Kosta Harlan <kosta@kostaharlan.net>
Copyright © 2020 Addshore
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
......@@ -24,9 +24,15 @@ import (
var versionCmd = &cobra.Command{
Use: "version",
Short: "Output the version",
Short: "Output the version infomation",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("0.1.0")
fmt.Printf(`GitCommit: %s
GitBranch: %s
GitState: %s
GitSummary: %s
BuildDate: %s
Version: %s
`, GitCommit, GitBranch, GitState, GitSummary, BuildDate, Version)
},
}
......
......@@ -10,4 +10,4 @@ make internal/mwdd/files/files.go
# Run from source from the origional directory
cd $WORKINGPATH
go run ${SCRIPTPATH}/cmd/cli/main.go $@
\ No newline at end of file
go run ${SCRIPTPATH}/main.go $@
\ No newline at end of file
module gerrit.wikimedia.org/r/mediawiki/tools/cli
require (
bou.ke/staticfiles v0.0.0-20210106104248-dd04075d4104
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ahmetb/govvv v0.3.0
github.com/briandowns/spinner v1.11.1
github.com/containerd/containerd v1.4.4 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
......@@ -20,4 +22,4 @@ require (
gotest.tools/v3 v3.0.3 // indirect
)
go 1.16
go 1.13
......@@ -18,6 +18,21 @@ package main
import "gerrit.wikimedia.org/r/mediawiki/tools/cli/cmd"
// Following variables will be statically linked at the time of compiling
/*GitCommit holds short commit hash of source tree*/
var GitCommit string
/*GitBranch holds current branch name the code is built off*/
var GitBranch string
/*GitState shows whether there are uncommitted changes*/
var GitState string
/*GitSummary holds output of git describe --tags --dirty --always*/
var GitSummary string
/*BuildDate holds RFC3339 formatted UTC date (build time)*/
var BuildDate string
/*Version holds contents of ./VERSION file, if exists, or the value passed via the -version option*/
var Version string
func main() {
cmd.Execute()
cmd.Execute(GitCommit, GitBranch, GitState, GitSummary, BuildDate, Version)
}
......@@ -46,6 +46,7 @@ assert() {
title "Build mwcli"
{
make
mv $BASE/bin/cli $BASE/bin/mw
}
# get mediawiki
......
// +build tools
package tools
import (
_ "bou.ke/staticfiles"
_ "github.com/ahmetb/govvv"
)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment