Skip to content

Commit

Permalink
Merge pull request #22 from davidovich/20-allow-summoning-of-subdirs
Browse files Browse the repository at this point in the history
allow summoning of subdirs
  • Loading branch information
davidovich authored Mar 31, 2019
2 parents a3d57ce + a91002f commit df2caea
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 31 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ It solves the maintenance problem of multiple copies of same
code snippets distributed in many repos, leveraging go modules and version
management.

You can make an analogy with a data singleton which always has the desired
state (packed scripts or pinned versions of binaries).

> NOTE: This is still a WIP and experimental. This readme is a design document and
not every feature is implemented yet.

Expand All @@ -21,7 +24,7 @@ In summon you leverage go execution to bootstrap in one phase. So your data can
```
go run github.com/davidovich/summon-example-assets/summon --help
# or list the data deliverables
go run github.com/davidovich/summon-example-assets/summon list
go run github.com/davidovich/summon-example-assets/summon ls
# or
# let summon configure the path so it can invoke a go executable
# (here go-gettable-executable is a reference to a go gettable repo), and will
Expand Down Expand Up @@ -67,6 +70,8 @@ The `assets/summon.config.yaml` contains an (optional) configuration file to cus
```yaml
version: 1
outputdir: .summoned
aliases:
simple-handle: a/file/in/asset-dir
# exec section declares invokables with their handle
# a same handle name cannot be in two invokers at the same time
exec:
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newListCmd(driver summon.Interface) *cobra.Command {
driver: driver,
}
lcmd := &cobra.Command{
Use: "list",
Use: "ls",
Short: "list all summonables",
RunE: func(cmd *cobra.Command, args []string) error {
listCmd.out = cmd.OutOrStdout()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/davidovich/summon

require (
github.com/gobuffalo/packr/v2 v2.0.7
github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lithammer/dedent v1.1.0
github.com/pkg/errors v0.8.1
Expand Down
34 changes: 18 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
github.com/gobuffalo/depgen v0.0.0-20190315122043-8442b3fa16db/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/envy v1.6.15 h1:OsV5vOpHYUpP7ZLS6sem1y40/lNX1BZj+ynMiRi21lQ=
github.com/gobuffalo/envy v1.6.15/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
github.com/gobuffalo/flect v0.1.0 h1:EJvbvZlo9exJk5UK02lz5pTs3XEpIM5/4o691KfvtkM=
github.com/gobuffalo/flect v0.1.0/go.mod h1:d2ehjJqGOH/Kjqcoz+F7jHTBbmDb38yXA598Hb50EGs=
github.com/gobuffalo/flect v0.1.1/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI=
github.com/gobuffalo/genny v0.0.0-20190315121735-8b38fb089e88 h1:gJb0C7JcowRQ6bFIjhhwUhqXZ6d1CdBYZyliCRTLbRs=
github.com/gobuffalo/genny v0.0.0-20190315121735-8b38fb089e88/go.mod h1:rWs4Z12d1Zbf19rlsn0nurr75KqhYp52EAGGxTbBhNk=
github.com/gobuffalo/genny v0.0.0-20190315124720-e16e52a93c79 h1:5AXgPPMHJYsMAoBnp+QHqIRl5JJ8Sq9opm50O/BrU9E=
github.com/gobuffalo/genny v0.0.0-20190315124720-e16e52a93c79/go.mod h1:nKeefjbhYowo36ys9nG9VUvD9FRIS0p3BC2JFfcOucM=
github.com/gobuffalo/genny v0.0.0-20190329151137-27723ad26ef9/go.mod h1:rWs4Z12d1Zbf19rlsn0nurr75KqhYp52EAGGxTbBhNk=
github.com/gobuffalo/genny v0.0.0-20190329153409-7be1b23270eb h1:IUtUgwYRIQOHgmCP8uAUjKQNJKUxICmptd1gMggAytA=
github.com/gobuffalo/genny v0.0.0-20190329153409-7be1b23270eb/go.mod h1:WGiyExKBGcMizwBYYmNL3LIyTRMP6wJQaD7MEYuM7Zk=
github.com/gobuffalo/gitgen v0.0.0-20190315122116-cc086187d211/go.mod h1:vEHJk/E9DmhejeLeNt7UVvlSGv3ziL+djtTr3yyzcOw=
github.com/gobuffalo/gogen v0.0.0-20190315121717-8f38393713f5 h1:f3Fpd5AqsFuTHUEhUeEMIFJkX8FpVnzdW+GpYxIyXkA=
github.com/gobuffalo/gogen v0.0.0-20190315121717-8f38393713f5/go.mod h1:V9QVDIxsgKNZs6L2IYiGR8datgMhB577vzTDqypH360=
github.com/gobuffalo/logger v0.0.0-20190315122211-86e12af44bc2 h1:8thhT+kUJMTMy3HlX4+y9Da+BNJck+p109tqqKp7WDs=
github.com/gobuffalo/logger v0.0.0-20190315122211-86e12af44bc2/go.mod h1:QdxcLw541hSGtBnhUc4gaNIXRjiDppFGaDqzbrBd3v8=
github.com/gobuffalo/mapi v1.0.1 h1:JRuTiZzDEZhBHkFiHTxJkYRT6CbYuL0K/rn+1byJoEA=
github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc=
github.com/gobuffalo/packd v0.0.0-20190315122247-83d601d65093 h1:ESaUc+p2J31vealRlSsQpvrt/WuqzLx0cS2akYj/kQE=
github.com/gobuffalo/packd v0.0.0-20190315122247-83d601d65093/go.mod h1:LpEu7OkoplvlhztyAEePkS6JwcGgANdgGL5pB4Knxaw=
github.com/gobuffalo/mapi v1.0.2 h1:fq9WcL1BYrm36SzK6+aAnZ8hcp+SrmnDyAxhNx8dvJk=
github.com/gobuffalo/mapi v1.0.2/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc=
github.com/gobuffalo/packd v0.0.0-20190315124812-a385830c7fc0 h1:P6naWPiHm/7R3eYx/ub3VhaW9G+1xAMJ6vzACePaGPI=
github.com/gobuffalo/packd v0.0.0-20190315124812-a385830c7fc0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4=
github.com/gobuffalo/packr/v2 v2.0.6 h1:iCzGL6YeqMNrrch5mwZpHWaUlFhzjf5G8YzsBrbAjTo=
github.com/gobuffalo/packr/v2 v2.0.6/go.mod h1:/TYKOjadT7P9jRWZtj4BRTgeXy2tIYntifGkD+aM2KY=
github.com/gobuffalo/packr/v2 v2.0.7 h1:8Gah4OwoqvIZgsUHa07qyXNl/LtZRa4gPjtXux4fVlY=
github.com/gobuffalo/packr/v2 v2.0.7/go.mod h1:1SBFAIr3YnxYdJRyrceR7zhOrhV/YhHzOjDwA9LLZ5Y=
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0 h1:GBRbBt01wGbGD8IrEAM5DGTQX9ZFkwyErNujW08x4dI=
github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0/go.mod h1:pcdZvfinlUBkDIFERw5fspLkUwwjq4OCekeXTCjtruM=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754 h1:tpom+2CJmpzAWj5/VEHync2rJGi+epHNIeRSWjzGA+4=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand All @@ -37,6 +34,8 @@ github.com/karrick/godirwalk v1.8.0 h1:ycpSqVon/QJJoaT1t8sae0tp1Stg21j+dyuS7Ooag
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -73,18 +72,21 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 h1:hOY+O8MxdkPV10pNf7/XEHaySCiPKxixMKUshfHsGn0=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190315044204-8b67d361bba2 h1:UW7QBl+AuPLMXy5g3iGofcPmCH+CajjwwKguuuJCS3E=
golang.org/x/tools v0.0.0-20190315044204-8b67d361bba2/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190318200714-bb1270c20edf h1:OVQ7iQXiQQT4WuYg+7S/bOVVlASHvL1Chsc15Qtkogo=
golang.org/x/tools v0.0.0-20190318200714-bb1270c20edf/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190330163440-a68386b9fffe h1:TSwzZtGyXzhKPLhVz/4DSkDWaEwA5tC5kFS0Y4QXaSE=
golang.org/x/tools v0.0.0-20190330163440-a68386b9fffe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
35 changes: 28 additions & 7 deletions pkg/summon/summon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,28 @@ func (s *Summoner) Summon(opts ...Option) (string, error) {

if s.opts.all {
return s.opts.destination, s.box.Walk(func(path string, info file.File) error {
_, err := s.copyOneFile(info)
_, err := s.copyOneFile(info, "")
return err
})
}

filename := s.resolveAlias(s.opts.filename)
filename := filepath.Clean(s.opts.filename)
filename = s.resolveAlias(filename)

// User wants to extract a subdirectory
if s.box.HasDir(filename) {
return s.opts.destination,
s.box.WalkPrefix(filename, func(path string, info file.File) error {
_, err := s.copyOneFile(info, filename)
return err
})
}

boxedFile, err := s.box.Open(filename)
if err != nil {
return "", err
}
return s.copyOneFile(boxedFile)
return s.copyOneFile(boxedFile, "")
}

func renderTemplate(tmpl string, data *map[string]interface{}) (string, error) {
Expand All @@ -72,16 +82,27 @@ func (s *Summoner) resolveAlias(alias string) string {
return alias
}

func (s *Summoner) copyOneFile(boxedFile http.File) (string, error) {
func (s *Summoner) copyOneFile(boxedFile http.File, rootDir string) (string, error) {
destination := s.opts.destination
// Write the file and print it's path
stat, _ := boxedFile.Stat()
stat, err := boxedFile.Stat()
if err != nil {
return "", err
}
filename := stat.Name()

filename, _ = renderTemplate(filename, s.opts.data)
filename, err = renderTemplate(filename, s.opts.data)
if err != nil {
return "", err
}

filename, err = filepath.Rel(rootDir, filename)
if err != nil {
return "", err
}

summonedFile := filepath.Join(destination, filename)
err := appFs.MkdirAll(filepath.Dir(summonedFile), os.ModePerm)
err = appFs.MkdirAll(filepath.Dir(summonedFile), os.ModePerm)
if err != nil {
return "", err
}
Expand Down
31 changes: 28 additions & 3 deletions pkg/summon/summon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,32 @@ func TestOneFileInstanciation(t *testing.T) {
a.Equal("this is a text", string(bytes))
}

func TestTemplateRendering(t *testing.T) {
func TestSubfolderHierarchy(t *testing.T) {
defer testutil.ReplaceFs()()
a := assert.New(t)

box := packr.New("hierarchy", "testdata")

// create a summoner to summon a complete hierarchy
s, err := New(box, Filename("subdir/"), Dest("o"), JSON(`{"TemplatedName":"b", "Content":"b content"}`))
a.NoError(err)

path, err := s.Summon()

a.NoError(err)
a.Equal("o", path)

a.True(afero.IsDir(GetFs(), "o/a"))
a.True(afero.IsDir(GetFs(), "o/b"))
a.True(afero.Exists(GetFs(), "o/b/b.txt"))

bytes, err := afero.ReadFile(GetFs(), "o/b/b.txt")
a.NoError(err)

a.Equal("b content", string(bytes))
}

func TestSummonScenarios(t *testing.T) {
defer testutil.ReplaceFs()()
assert := assert.New(t)

Expand Down Expand Up @@ -101,8 +126,8 @@ func TestTemplateRendering(t *testing.T) {
{
desc: "alias",
filename: "a",
expectedFileName: "overridden_dir/a/a.txt",
expectedContent: "a",
expectedFileName: "overridden_dir/subdir/a/a.txt",
expectedContent: "this is a.txt",
},
{
desc: "error in json input",
Expand Down
1 change: 0 additions & 1 deletion pkg/summon/testdata/a/a.txt

This file was deleted.

1 change: 1 addition & 0 deletions pkg/summon/testdata/subdir/a/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ .Content }}
2 changes: 1 addition & 1 deletion pkg/summon/testdata/summon.config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 1
aliases:
renderableFileName: "{{.FileName}}"
a: "a/a.txt"
a: "subdir/a/a.txt"
outputdir: "overridden_dir"
exec:
bash:
Expand Down

0 comments on commit df2caea

Please sign in to comment.