Skip to content

Commit f5d6cb0

Browse files
authored
Merge pull request #76 from davidovich/implement-fs
Implement fs
2 parents d6a3c34 + c259051 commit f5d6cb0

38 files changed

+386
-350
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version: 2
66
jobs:
77
build:
88
docker:
9-
- image: circleci/golang:1.15
9+
- image: circleci/golang:1.17
1010
environment:
1111
GO111MODULE=on
1212
steps:

Makefile

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
SHELL=/bin/bash
22

3-
HAS_PACKR2 := $(shell command -v packr2)
4-
HAS_GOBIN := $(shell command -v gobin)
53
HAS_GOCOVERUTIL := $(shell command -v gocoverutil)
64

7-
ifndef HAS_GOBIN
8-
$(shell GO111MODULE=off go get -u github.com/myitcv/gobin 2>/dev/null)
9-
endif
10-
115
export GO111MODULE := on
126

137
SCAFFOLD_BIN := bin/scaffold
14-
PACKR_FILE := internal/scaffold/scaffold-packr.go
8+
SCAFFOLD_SRCS := $(shell GO111MODULE=on go list -f '{{ $$dir := .Dir}}{{range .GoFiles}}{{ printf "%s/%s\n" $$dir . }}{{end}}' github.com/davidovich/summon/scaffold/...)
159
COVERAGE_PERCENT_FILE := $(CURDIR)/build/coverage-percent.txt
1610

1711
DOC_REPO_NAME := davidovich.github.io
1812
DOC_REPO := git@github.com:davidovich/$(DOC_REPO_NAME).git
1913
SUMMON_BADGE_JSON_FILE := $(DOC_REPO_NAME)/shields/summon/summon.json
2014

21-
ASSETS := $(shell find internal/templates/scaffold)
15+
ASSETS := $(shell find internal/scaffold/templates/scaffold)
2216

23-
all: $(PACKR_FILE) test $(SCAFFOLD_BIN)
17+
all: test $(SCAFFOLD_BIN)
2418

2519
.PHONY: bin
26-
bin: $(PACKR_FILE) $(SCAFFOLD_BIN)
20+
bin: $(SCAFFOLD_BIN)
2721

2822
.PHONY: $(SCAFFOLD_BIN)
29-
$(SCAFFOLD_BIN): $(PACKR_FILE)
30-
@mkdir -p bin
23+
$(SCAFFOLD_BIN): $(ASSETS) $(SCAFFOLD_SRCS)
3124
go build -o $@ $(@F)/$(@F).go
3225

33-
$(PACKR_FILE): $(ASSETS)
34-
ifndef HAS_PACKR2
35-
gobin github.com/gobuffalo/packr/v2/packr2
36-
endif
37-
cd internal/scaffold && packr2
38-
39-
GO_TESTS := pkg internal cmd
40-
COVERAGE := $(foreach t,$(GO_TESTS),build/coverage/report/$(t))
26+
COVERAGE := build/coverage/report/summon
4127
MERGED_COVERAGE := build/coverage/report/cover.merged.out
4228
HTML_COVERAGE := build/coverage/html/index.html
4329

@@ -57,7 +43,7 @@ $(COVERAGE_PERCENT_FILE): $(MERGED_COVERAGE)
5743

5844
$(MERGED_COVERAGE): $(COVERAGE)
5945
ifndef HAS_GOCOVERUTIL
60-
gobin github.com/AlekSi/gocoverutil@v0.2.0
46+
go install github.com/AlekSi/gocoverutil@v0.2.0
6147
endif
6248
gocoverutil -coverprofile=$@ merge $^
6349

@@ -67,7 +53,7 @@ $(HTML_COVERAGE): $(MERGED_COVERAGE)
6753

6854
$(COVERAGE):
6955
@mkdir -p $(@D)
70-
go test ./$(@F)/... --cover -coverprofile $@ -v
56+
go test ./... --cover -coverprofile $@ -v
7157

7258
.PHONY: update-coverage-badge
7359
update-coverage-badge: $(COVERAGE_PERCENT_FILE)
@@ -78,12 +64,11 @@ else
7864
rm -rf /tmp/$(DOC_REPO_NAME)
7965
git -C /tmp clone $(DOC_REPO)
8066
cd /tmp/$(DOC_REPO_NAME) && \
81-
gobin -run github.com/davidovich/summon-example-assets/summon shields.io/coverage.json.gotmpl --json "{\"Coverage\": \"$$(cat $(COVERAGE_PERCENT_FILE))\"}" -o- > /tmp/$(SUMMON_BADGE_JSON_FILE)
67+
go run github.com/davidovich/summon-example-assets@3c2e66d7 shields.io/coverage.json.gotmpl --json "{\"Coverage\": \"$$(cat $(COVERAGE_PERCENT_FILE))\"}" -o- > /tmp/$(SUMMON_BADGE_JSON_FILE)
8268
git -C /tmp/$(DOC_REPO_NAME) diff-index --quiet HEAD || git -C /tmp/$(DOC_REPO_NAME) -c user.email=automation@davidovich.com -c user.name=automation commit -am "automated coverage commit of $$(cat $(COVERAGE_PERCENT_FILE)) %" || true
8369
git -C /tmp/$(DOC_REPO_NAME) push
8470
endif
8571

8672
.PHONY: clean
8773
clean:
88-
packr2 clean
8974
rm -rf bin build

README.md

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [How it Works](#how-it-works)
77
- [Configuration](#configuration)
88
- [Data repository](#data-repository)
9+
- [Migration from versions prior to v0.13.0](#migration-from-versions-prior-to-v0130)
910
- [Summon config File](#summon-config-file)
1011
- [Build](#build)
1112
- [Install](#install)
@@ -45,27 +46,40 @@ state (packed scripts or pinned versions of binaries).
4546

4647
> NOTE: This project is still a WIP and experimental.
4748
48-
To install, you first need to create something to install by populating a [data repository](#Configuration). Then, this data repo is installed by using [gobin](https://github.com/myitcv/gobin) (or go install):
49+
To install, you first need to create something to install by populating a
50+
[data repository](#Configuration). Then, this data repo is installed by using
51+
the `go install` command:
4952

5053
```bash
51-
gobin [your-summon-data-repo]/summon
54+
go install [your-summon-data-repo]/summon@latest
5255
```
5356

54-
Assuming there is a my-team-utility.sh script hosted in the data repo, (see how to [configure](#Configuration) below) you can do things like:
57+
Assuming there is a my-team-utility.sh script hosted in the data repo, (see how
58+
to [configure](#Configuration) below) you can do things like:
5559

5660
```bash
5761
bash $(summon my-team-utility.sh)
5862
```
5963

6064
## How it Works
6165

62-
Summon is a library which is consumed by an asset repository (which, by default, has also the `summon` name). This asset repository, managed by your team, provides the summon executable command (it's main() function is in summon/summon.go).
63-
The library provides the command-line interface, so no coding is necessary in the assert repo.
66+
Summon is a library which is consumed by an asset repository (which, by default,
67+
has also the `summon` name). This asset repository, managed by your team,
68+
provides the summon executable command (it's main() function is in
69+
summon/summon.go). The library provides the command-line interface, so no
70+
coding is necessary in the assert repo.
6471

6572
Summon also provides a boostrapping feature in the scaffold command.
6673

67-
Summon builds upon [packr2](https://github.com/gobuffalo/packr/tree/master/v2) to convert an arbitrary tree of files in go compilable source
68-
which you then bootstrap at destination using standard go get or [gobin](https://github.com/myitcv/gobin). The invoked files are then placed locally and the summoned file path is returned so it can be consumed by other shell operations.
74+
> New in v0.13.0
75+
76+
Summon builds upon the new go 1.16 [embed.FS](https://pkg.go.dev/embed) feature
77+
used to pack assets in a go binary. You then install this at destination using
78+
standard `go install`.
79+
80+
When you invoke this binary with a contained asset path, the invoked files are
81+
placed locally and the summoned file path is returned so it can be consumed by
82+
other shell operations.
6983

7084
## Configuration
7185

@@ -76,38 +90,49 @@ Use summon's scaffold feature to create a data repository, which will become you
7690
> Scaffolding is new in v0.1.0
7791
7892
```bash
79-
gobin -run github.com/davidovich/summon/scaffold init [module name]
93+
# go run package at a version requires go 1.17 and up
94+
go run github.com/davidovich/summon/scaffold@latest init [module name]
8095
```
8196

82-
> Be sure to change the [module name] part (usually you will use a module path compatible with where you will host the data repo on a code hosting site).
97+
> Be sure to change the [module name] part (usually you will use a module path
98+
> compatible with where you will host the data repo on a code hosting site).
8399
84100
You will then have something resembling this structure:
85101

86102
```bash
87103
.
88104
├── Makefile
89105
├── README.md
90-
├── assets
91-
│ └── summon.config.yaml
92106
├── go.mod
107+
├── go.sum
93108
└── summon
109+
├── assets
110+
│ └── summon.config.yaml
94111
└── summon.go
95112
```
96113

97114
There is an example setup at https://github.com/davidovich/summon-example-assets.
98115

99-
You just need to populate the `assets` directory with your own data.
116+
You just need to populate the `summon/assets` directory with your own data.
117+
118+
The `summon/summon.go` file of the main module is the entry point to the summon
119+
library, and creates the main command executable.
100120

101-
The `summon/` directory is the entry point to the summon library, and creates the main command executable. This directory will also host
102-
[packr2](https://github.com/gobuffalo/packr/tree/master/v2) generated data files which encode asset data into go files.
121+
#### Migration from versions prior to v0.13.0
122+
123+
The v0.13.0 version uses the embed.FS and the `//go:embed assets/*` directive.
124+
Prior versions used to reference the `assets/` dir at the root of the repo.
125+
This means that the data being embeded must now be a sibling of the source
126+
file containing `package main`.
103127

104128
### Summon config File
105129

106-
The `assets/summon.config.yaml` is an (optional) configuration file to customize summon. You can define:
130+
The `summon/assets/summon.config.yaml` is an (optional) configuration file to
131+
customize summon. You can define:
107132

108-
* aliases
109-
* default output-dir
110-
* executables
133+
- aliases
134+
- default output-dir
135+
- executables
111136

112137
> Breaking in v0.11.0: Handles now take an array of params
113138
@@ -138,9 +163,8 @@ exec:
138163
# ^ handle to script (must be unique). This is what you use
139164
# to invoke the script: `summon run hello`.
140165

141-
gobin -run: # go gettable executables
142-
gobin: [github.com/myitcv/gobin@v0.0.8]
143-
gohack: [github.com/rogppepe/gohack]
166+
go run: # go gettable executables
167+
gohack: [github.com/rogppepe/gohack@latest]
144168

145169
python -c:
146170
hello-python: [print("hello from python!")]
@@ -161,7 +185,8 @@ You can invoke executables like so:
161185
summon run gohack ...
162186
```
163187

164-
This will install gohack using `gobin -run` and forward the arguments that you provide.
188+
This will call and run gohack using `go run` and forward the arguments that you
189+
provide.
165190

166191
> New in v0.10.0, summon now allows templating in the invocable section. See
167192
> [Templating](#/templating).
@@ -170,35 +195,22 @@ This will install gohack using `gobin -run` and forward the arguments that you p
170195

171196
In an empty asset data repository directory:
172197

173-
* First (and once) invoke `go run github.com/davidovich/summon/scaffold init [repo host (module name)]`
198+
- First (and once) invoke `go run github.com/davidovich/summon/scaffold@latest init [repo host (module name)]`
174199
This will create code template similar as above
175200

176201
1. Add assets that need to be shared amongst consumers
177-
2. Use the provided Makefile to invoke the packr2 process: `make`
178-
3. Commit the resulting -packr files so clients can go get the data repo
202+
2. Use the provided Makefile to create the asset executable: `make`
203+
3. Commit the all the files so clients can go get the data repo
179204
4. Tag the repo with semantic version (with the `v`) prefix.
180205
5. Push to remote.
181-
6. Install.
206+
6. On a consumer machine, install.
182207

183208
## Install
184209

185-
Install (using gobin) the asset repo which will become the summon executable.
186-
You have these alternatives:
187-
188-
* change to a directory that does not contain a go.mod. This installs globally:
189-
190-
```bash
191-
cd /tmp
192-
GO111MODULE=on go get [your-go-repo-import]/summon[@wanted-version-or-branch]
193-
cd -
194-
```
195-
196-
* use gobin to install summon in the consuming site:
210+
Install (using `go install`) the asset repo which will become the summon executable.
197211

198212
```bash
199-
GO111MODULE=off go get -u github.com/myitcv/gobin
200-
# install the data repository as summon executable at the site
201-
GOBIN=./ gobin [your-go-repo-import]/summon[@wanted-version-or-branch]
213+
go install [your-go-repo-module]/summon@latest
202214
```
203215

204216
## Use-cases
@@ -212,12 +224,13 @@ summon returns the path ot where the resource was instantiated:
212224
include $(shell summon version.mk)
213225
```
214226

215-
By default, summon will put summoned scripts at the `.summoned/` directory at root of the current directory.
227+
By default, summon will put summoned scripts at the `.summoned/` directory at
228+
the root of the current directory. This can be changed with the `-o` flag.
216229

217230
### Templating
218231

219232
Files in the asset directory can contain go templates. This allows applying
220-
customization using json data.
233+
customization using json data, just before rendering the file (and its contents).
221234

222235
> New in v0.3.0, summon now uses the [Sprig templating library](https://github.com/Masterminds/sprig), which provides many useful templating functions.
223236
@@ -230,21 +243,23 @@ For example, consider this file in a summon asset provider:
230243

231244
With this content:
232245

233-
```bash
246+
```txt
234247
Hello {{ .Name }}!
235248
```
236249

237250
`summon template.file --json '{ "Name": "David" }'`
238251

239252
You will get a summoned `template.file` file with this result:
240253

241-
```bash
254+
```shell
242255
Hello David!
243256
```
244257

245-
> New in v0.2.0, you can summon a whole asset hierarchy by using a directory reference when summoning.
258+
> New in v0.2.0, you can summon a whole asset hierarchy by using a directory
259+
> reference when summoning.
246260
247-
Templates can also be used in the filenames given in the data hierarchy. This can be useful to scaffold simple projects.
261+
Templates can also be used in the filenames given in the data hierarchy. This
262+
can be useful to scaffold simple projects.
248263

249264
```bash
250265
/assets

assets/summon.config.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# this is a sample summon config file
2+
# see https://github.com/davidovich/summon#configuration
3+
version: 1
4+
aliases: {"git-version": "bin/version.sh"}
5+
# outputdir: "overridden_dir"
6+
exec:
7+
bash:
8+
hello-bash: ['{{ summon "hello.sh" }}']
9+
10+
docker run -v {{ env "PWD" }}:/mounted-app alpine ls:
11+
list: [/mounted-app]
12+
13+
go run:
14+
gohack: [github.com/rogpeppe/gohack@v1.0.2]
15+
16+
docker {{ lower "INFO" }}: # naive example of templating
17+
docker-info:
18+
19+
? docker run -ti --rm -w /{{ env "PWD" | base }}
20+
-v {{ env "PWD" }}:/{{ env "PWD" | base }}
21+
hairyhenderson/figlet
22+
:
23+
figlet: ['hello {{ env "USER" }}']
24+
25+
# echo: # future implementation idea
26+
# - handle: echo
27+
# args: ['echoing: {{ .Message }}']
28+
# env: []
29+
30+
python3 -c:
31+
hello:
32+
- |
33+
import sys
34+
print('pyhon version:'); print(sys.version)
35+
if True:
36+
print()
37+
print(f"hello from python! args: {sys.argv}")
38+
39+
python -c:
40+
hellopy2: [print('hello from python!')]
41+
42+
bash -c:
43+
# small function to setup a temporary GOPATH compatible godoc server inside docker
44+
# see https://github.com/golang/go/issues/26827. This allows serving documentation
45+
# for a go modules enabled project.
46+
godoc:
47+
- |
48+
function _godoc() {
49+
[ ! -f "$(pwd)/go.mod" ] && echo "error: go.mod not found" ||
50+
module=$(awk 'NR==1{print $2}' go.mod) &&
51+
docker run --rm -ti -e "GOPATH=/tmp/go" -p 6060:6060 -v $(pwd):/tmp/go/src/$module golang:1.15 /bin/bash -c "GO111MODULE=on go get golang.org/x/tools/cmd/godoc && echo -e \"\nhttp://localhost:6060/pkg/$module\" && \$(go env GOPATH)/bin/godoc -http=:6060"
52+
unset -f _godoc;
53+
}
54+
_godoc
55+
56+
# kmerge merges a the default kubeconfig with a kubeconfig file passed as argement
57+
# https://github.com/kubernetes/kubernetes/issues/46381#issuecomment-461404505
58+
kmerge:
59+
- |
60+
function kmerge() {
61+
KUBECONFIG=~/.kube/config:$1 kubectl config view --flatten > ~/.kube/mergedkub && mv ~/.kube/mergedkub ~/.kube/config
62+
}
63+
kmerge
64+

cmd/completion_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import (
55
"testing"
66

77
"github.com/davidovich/summon/pkg/summon"
8-
"github.com/gobuffalo/packr/v2"
98
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestCompletionCommand(t *testing.T) {
13-
box := packr.New("testCompletion", "testdata/plain")
14-
15-
s, _ := summon.New(box)
12+
s, _ := summon.New(runCmdTestFS)
1613

1714
cmd := newCompletionCmd(s)
1815

0 commit comments

Comments
 (0)