Skip to content

Commit 8e9e3fb

Browse files
authored
Add contextual vars forked from main instance (#3)
1 parent 6e134bd commit 8e9e3fb

11 files changed

Lines changed: 198 additions & 21 deletions

File tree

.github/workflows/cloc.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This script is provided by github.com/bool64/dev.
2+
name: cloc
3+
on:
4+
pull_request:
5+
jobs:
6+
cloc:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v2
11+
with:
12+
path: pr
13+
- name: Checkout base code
14+
uses: actions/checkout@v2
15+
with:
16+
ref: ${{ github.event.pull_request.base.sha }}
17+
path: base
18+
- name: Count Lines Of Code
19+
id: loc
20+
run: |
21+
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
22+
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
23+
echo "${OUTPUT}"
24+
OUTPUT="${OUTPUT//$'\n'/%0A}"
25+
echo "::set-output name=diff::$OUTPUT"
26+
27+
- name: Comment Code Lines
28+
continue-on-error: true
29+
uses: marocchino/sticky-pull-request-comment@v2
30+
with:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
header: LOC
33+
message: |
34+
### Lines Of Code
35+
36+
${{ steps.loc.outputs.diff }}

.github/workflows/golangci-lint.yml

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This script is provided by github.com/bool64/dev.
12
name: lint
23
on:
34
push:
@@ -14,10 +15,10 @@ jobs:
1415
steps:
1516
- uses: actions/checkout@v2
1617
- name: golangci-lint
17-
uses: golangci/golangci-lint-action@v2.5.1
18+
uses: golangci/golangci-lint-action@v2.5.2
1819
with:
1920
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
20-
version: v1.38.0
21+
version: v1.43.0
2122

2223
# Optional: working directory, useful for monorepos
2324
# working-directory: somedir

.github/workflows/gorelease.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This script is provided by github.com/bool64/dev.
2+
name: gorelease
3+
on:
4+
pull_request:
5+
env:
6+
GO_VERSION: 1.17.x
7+
jobs:
8+
gorelease:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Install Go stable
12+
if: env.GO_VERSION != 'tip'
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: ${{ env.GO_VERSION }}
16+
- name: Install Go tip
17+
if: env.GO_VERSION == 'tip'
18+
run: |
19+
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
20+
ls -lah gotip.tar.gz
21+
mkdir -p ~/sdk/gotip
22+
tar -C ~/sdk/gotip -xzf gotip.tar.gz
23+
~/sdk/gotip/bin/go version
24+
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
25+
- name: Checkout code
26+
uses: actions/checkout@v2
27+
- name: Gorelease cache
28+
uses: actions/cache@v2
29+
with:
30+
path: |
31+
~/go/bin/gorelease
32+
key: ${{ runner.os }}-gorelease
33+
- name: Gorelease
34+
id: gorelease
35+
run: |
36+
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
37+
OUTPUT=$(gorelease || exit 0)
38+
echo "${OUTPUT}"
39+
OUTPUT="${OUTPUT//$'\n'/%0A}"
40+
echo "::set-output name=report::$OUTPUT"
41+
- name: Comment Report
42+
continue-on-error: true
43+
uses: marocchino/sticky-pull-request-comment@v2
44+
with:
45+
header: gorelease
46+
message: |
47+
### Go API Changes
48+
49+
<pre>
50+
${{ steps.gorelease.outputs.report }}
51+
</pre>

.github/workflows/test-unit.yml

100644100755
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This script is provided by github.com/bool64/dev.
12
name: test-unit
23
on:
34
push:
@@ -8,17 +9,28 @@ on:
89
env:
910
GO111MODULE: "on"
1011
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
12+
COV_GO_VERSION: 1.17.x # Version of Go to collect coverage
1113
jobs:
1214
test:
1315
strategy:
1416
matrix:
15-
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x ]
17+
go-version: [ 1.16.x, 1.17.x, tip ]
1618
runs-on: ubuntu-latest
1719
steps:
18-
- name: Install Go
20+
- name: Install Go stable
21+
if: matrix.go-version != 'tip'
1922
uses: actions/setup-go@v2
2023
with:
2124
go-version: ${{ matrix.go-version }}
25+
- name: Install Go tip
26+
if: matrix.go-version == 'tip'
27+
run: |
28+
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
29+
ls -lah gotip.tar.gz
30+
mkdir -p ~/sdk/gotip
31+
tar -C ~/sdk/gotip -xzf gotip.tar.gz
32+
~/sdk/gotip/bin/go version
33+
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
2234
- name: Checkout code
2335
uses: actions/checkout@v2
2436
- name: Go cache
@@ -34,42 +46,43 @@ jobs:
3446
restore-keys: |
3547
${{ runner.os }}-go-cache
3648
- name: Restore base test coverage
37-
if: matrix.go-version == '1.16.x'
49+
id: base-coverage
50+
if: matrix.go-version == env.COV_GO_VERSION
3851
uses: actions/cache@v2
3952
with:
4053
path: |
4154
unit-base.txt
4255
# Use base sha for PR or new commit hash for master/main push in test result key.
4356
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
4457
- name: Checkout base code
45-
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
58+
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
4659
uses: actions/checkout@v2
4760
with:
4861
ref: ${{ github.event.pull_request.base.sha }}
4962
path: __base
5063
- name: Run test for base code
51-
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
64+
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
5265
run: |
5366
cd __base
54-
make test-unit
55-
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
67+
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
5668
- name: Test
5769
id: test
5870
run: |
5971
make test-unit
6072
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
6173
OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
62-
OUTPUT="${OUTPUT//'%'/'%25'}"
63-
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
64-
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
74+
echo "${OUTPUT}"
75+
OUTPUT="${OUTPUT//$'\n'/%0A}"
6576
TOTAL=$(grep 'total:' unit.txt)
77+
echo "${TOTAL}"
6678
echo "::set-output name=diff::$OUTPUT"
6779
echo "::set-output name=total::$TOTAL"
6880
- name: Store base coverage
6981
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
7082
run: cp unit.txt unit-base.txt
7183
- name: Comment Test Coverage
72-
if: matrix.go-version == '1.16.x'
84+
continue-on-error: true
85+
if: matrix.go-version == env.COV_GO_VERSION
7386
uses: marocchino/sticky-pull-request-comment@v2
7487
with:
7588
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -85,7 +98,7 @@ jobs:
8598
</details>
8699
87100
- name: Upload code coverage
88-
if: matrix.go-version == '1.16.x'
101+
if: matrix.go-version == env.COV_GO_VERSION
89102
uses: codecov/codecov-action@v1
90103
with:
91104
file: ./unit.coverprofile

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/.idea
2-
/unit.coverprofile
2+
/*.coverprofile
3+
/.vscode
4+
/bench-*.txt
5+
/vendor

.golangci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ linters:
2828
- paralleltest
2929
- forbidigo
3030
- exhaustivestruct
31-
- interfacer
31+
- interfacer # deprecated
3232
- forcetypeassert
33+
- scopelint # deprecated
34+
- ifshort # too many false positives
35+
- golint # deprecated
36+
- varnamelen
37+
- tagliatelle
38+
- errname
39+
- ireturn
3340

3441
issues:
3542
exclude-use-default: false

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.38.0" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.43.0" # Optional configuration to pinpoint golangci-lint version.
22

33
# The head of Makefile determines location of dev-go to include standard targets.
44
GO ?= go
@@ -23,7 +23,7 @@ ifeq ($(DEVGO_PATH),)
2323
DEVGO_PATH := $(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m github.com/bool64/dev)
2424
ifeq ($(DEVGO_PATH),)
2525
$(info Module github.com/bool64/dev not found, downloading.)
26-
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) mod tidy && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
26+
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) get github.com/bool64/dev && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
2727
endif
2828
endif
2929

@@ -33,5 +33,7 @@ endif
3333
-include $(DEVGO_PATH)/makefiles/bench.mk
3434
-include $(DEVGO_PATH)/makefiles/reset-ci.mk
3535

36+
# Add your custom targets here.
37+
3638
## Run tests
3739
test: test-unit

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/bool64/shared
33
go 1.11
44

55
require (
6-
github.com/bool64/dev v0.1.25
6+
github.com/bool64/dev v0.2.5
77
github.com/stretchr/testify v1.4.0
88
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
github.com/bool64/dev v0.1.25 h1:iSfcaS1FP6F2TlMI1daAvqk6PYl23XDf6oZu7WRXB8Y=
2-
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
1+
github.com/bool64/dev v0.1.43 h1:McAhPS6dBrNSN53K9+4dj4ntCjEf9pw/r8AwaXRKgX8=
2+
github.com/bool64/dev v0.1.43/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
3+
github.com/bool64/dev v0.2.5 h1:H0bylghwcjDBBhEwSFTjArEO9Dr8cCaB54QSOF7esOA=
4+
github.com/bool64/dev v0.2.5/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
35
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
46
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

vars.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package shared
33

44
import (
5+
"context"
56
"strings"
67
"sync"
78
)
@@ -26,6 +27,31 @@ func (v *Vars) Reset() {
2627
v.onSet = nil
2728
}
2829

30+
// Fork instruments context with a copy of Vars or returns Vars that are already in context.
31+
func (v *Vars) Fork(ctx context.Context) (context.Context, *Vars) {
32+
if vc, ok := ctx.Value(ctxKey{}).(*Vars); ok {
33+
return ctx, vc
34+
}
35+
36+
v.mu.Lock()
37+
defer v.mu.Unlock()
38+
39+
vc := &Vars{
40+
VarPrefix: v.VarPrefix,
41+
onSet: v.onSet,
42+
}
43+
44+
if len(v.vars) > 0 {
45+
vc.vars = make(map[string]interface{})
46+
47+
for k, v := range v.vars {
48+
vc.vars[k] = v
49+
}
50+
}
51+
52+
return context.WithValue(ctx, ctxKey{}, vc), vc
53+
}
54+
2955
// IsVar checks if string looks like a variable name.
3056
func (v *Vars) IsVar(s string) bool {
3157
varPrefix := v.VarPrefix
@@ -36,6 +62,8 @@ func (v *Vars) IsVar(s string) bool {
3662
return strings.HasPrefix(s, varPrefix)
3763
}
3864

65+
type ctxKey struct{}
66+
3967
// Get returns variable value if is exists.
4068
func (v *Vars) Get(s string) (interface{}, bool) {
4169
v.mu.Lock()

0 commit comments

Comments
 (0)