Skip to content

Instantly share code, notes, and snippets.

@vishal1132
Last active October 11, 2022 16:25
Show Gist options
  • Save vishal1132/f3143e5202d12f23a6e5c108e31cc169 to your computer and use it in GitHub Desktop.
Save vishal1132/f3143e5202d12f23a6e5c108e31cc169 to your computer and use it in GitHub Desktop.
go utils
og(){
	case "$1" in
		modv) go mod edit -json | jq -r .Go ;;
		chart) go mod graph | gmchart ;; # go install github.com/PaulXu-cn/go-mod-graph-chart/gmchart@latest
		graph) go mod graph | modgraphviz | dot -Tsvg -o mod-graph.svg; open mod-graph.svg ;;
		pgraph) go list -deps -json ./... | jq -c 'select(.Standard!=true) | {from: .ImportPath, to: .Imports[]}' | jsonl-graph  | grep -v 'Cannot iterate over nul' | dot -Tsvg > package-graph.svg; open package-graph.svg;;
		test) tst ${@:2};; # go install gotest.tools/gotestsum@latest
		cov) covf $2 ;;
	    cov-tree) covf total; go-cover-treemap -coverprofile cover.out > out.svg; open out.svg ;;  # go install github.com/nikolaydubina/go-cover-treemap@latest
	    files) go list -json ./... | jq -rc '[.ImportPath, (.GoFiles | length | tostring)] | join(" ")' | perl -lane 'print (" " x (20 - $F[1]), "=" x $F[1], " ", $F[1], "\t", $F[0])';; # install jq for this
	    richtest) RICHGO_FORCE_COLOR=1 richgo test ./... ;; # go install github.com/kyoh86/richgo@latest
	    richreport) go test -v ./... 2>&1 | tee >(richgo testfilter) | go-junit-report -out test.xml ;; # go install github.com/jstemmer/go-junit-report/v2@latest
	    notests) packages_without_tests;;
		fmt) go fmt ./...;; # fmt me 
		lint) golangci-lint run -v ./... ;; # install golangci-lint before using this
		gen) grep -rnw "go:generate" -E -l "${1:-*.go}" . | xargs -L1 dirname | sort -u | xargs -P 8 -I{} go generate {} ;;
		bintree) go tool nm -size $2 | go-binsize-treemap > binsize.svg ;;
		srv) fileserve $2;;
	esac
}

fileserve(){
	echo "package main

import \"net/http\"

func main() { http.ListenAndServe(\":9000\", http.FileServer(http.Dir(\"$1\"))) }" > strange.go; go run strange.go &; ngrok http 9000;  rm strange.go; portslay 9000;
}

portslay () {
    kill -9 `lsof -i tcp:$1 | tail -1 | awk '{ print $2;}'`
}

packages_without_tests(){
        package_hist=$(go list -json ./... | jq -rc '[.ImportPath, (.GoFiles | length | tostring)] | join(" ")' | perl -lane 'print (" " x (20 - $F[1]), "=" x $F[1], " ", $F[1], "\t", $F[0])')
        no_test_packages=$(go list -json ./... | jq -rc 'select((.TestGoFiles | length)==0) | .ImportPath' | rg -v 'test|mock' | xargs -I {} echo {}"$")
        echo $package_hist > hist.txt
        echo $no_test_packages > tests.txt
        rg -w -f tests.txt hist.txt
        rm hist.txt tests.txt
}

tst(){
	if [ $# -eq 0 ]; then
		gotestsum --rerun-fails --packages="./..." --format=testname -- -count=2
	else
		gotestsum --rerun-fails --packages="./..." --format=$1 -- -count=2
	fi
}

func covf(){
        case $1 in
        total) go test -covermode=count  -p 1  -coverpkg=./... ./... -coverprofile=cover.out; go tool cover -func=cover.out | grep -i total: ;;
        html) go test -covermode=count  -p 1  -coverpkg=./... ./... -coverprofile=cover.out; go tool cover -html=cover.out ;;
        esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment