Repository: goreleaser/goreleaser
Pull request #984 (2019-03-14 12:41:18)

feat: added Plugs attribute to snapcraft.Metadata
check  #980

cc/ @cjimti 

codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=h1) Report
> Merging [#984](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/f56b9a8509e5c3f3c33bd5d3574c7030666674dc?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/984/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #984      +/-   ##
==========================================
+ Coverage   88.05%   88.05%   +<.01%     
==========================================
  Files          52       52              
  Lines        2503     2504       +1     
==========================================
+ Hits         2204     2205       +1     
  Misses        236      236              
  Partials       63       63
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/984/diff?src=pr&el=tree#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `100% <ø> (ø)` | :arrow_up: |
| [internal/pipe/snapcraft/snapcraft.go](https://codecov.io/gh/goreleaser/goreleaser/pull/984/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9zbmFwY3JhZnQvc25hcGNyYWZ0Lmdv) | `87.9% <100%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=footer). Last update [f56b9a8...0b80257](https://codecov.io/gh/goreleaser/goreleaser/pull/984?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go
index 76b24685..39f4f8cb 100644
--- a/internal/pipe/snapcraft/snapcraft.go
+++ b/internal/pipe/snapcraft/snapcraft.go
@@ -39,6 +39,7 @@ type Metadata struct {
 	Confinement   string `yaml:",omitempty"`
 	Architectures []string
 	Apps          map[string]AppMetadata
+	Plugs         map[string]interface{} `yaml:",omitempty"`
 }
 
 // AppMetadata for the binaries that will be in the snap package
@@ -168,6 +169,7 @@ func create(ctx *context.Context, arch string, binaries []artifact.Artifact) err
 			}, " ")
 		}
 		metadata.Apps[binary.Name] = appMetadata
+		metadata.Plugs = ctx.Config.Snapcraft.Plugs
 
 		destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path))
 		log.WithField("src", binary.Path).
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 0346c549..7596dd7c 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -228,6 +228,7 @@ type Snapcraft struct {
 	Grade       string                          `yaml:",omitempty"`
 	Confinement string                          `yaml:",omitempty"`
 	Apps        map[string]SnapcraftAppMetadata `yaml:",omitempty"`
+	Plugs       map[string]interface{}          `yaml:",omitempty"`
 }
 
 // Snapshot config
diff --git a/www/content/snapcraft.md b/www/content/snapcraft.md
index 992b1f90..6f970be5 100644
--- a/www/content/snapcraft.md
+++ b/www/content/snapcraft.md
@@ -82,7 +82,7 @@ snapcraft:
       # You can read the documentation about the available plugs and the
       # things they allow:
       # https://snapcraft.io/docs/reference/interfaces.
-      plugs: ["home", "network"]
+      plugs: ["home", "network", "personal-files"]
 
       # If you want your app to be autostarted and to always run in the
       # background, you can make it a simple daemon.
@@ -91,6 +91,16 @@ snapcraft:
       # If you any to pass args to your binary, you can add them with the
       # args option.
       args: --foo
+  # Allows plugs to be configured. Plugs like system-files and personal-files
+  # require this.
+  # Default is empty.
+  plugs:
+    personal-files:
+      read:
+      - $HOME/.foo
+      write:
+      - $HOME/.foo
+      - $HOME/.foobar
 ```
 
 > Learn more about the [name template engine](/templates).


Test patch:
diff --git a/internal/pipe/snapcraft/snapcraft_test.go b/internal/pipe/snapcraft/snapcraft_test.go
index 90409bc1..8cf7cba7 100644
--- a/internal/pipe/snapcraft/snapcraft_test.go
+++ b/internal/pipe/snapcraft/snapcraft_test.go
@@ -109,9 +109,9 @@ func TestRunPipeWithName(t *testing.T) {
 	var metadata Metadata
 	err = yaml.Unmarshal(yamlFile, &metadata)
 	assert.NoError(t, err)
-	assert.Equal(t, metadata.Name, "testsnapname")
-	assert.Equal(t, metadata.Apps["mybin"].Command, "mybin")
-	assert.Equal(t, metadata.Apps["testsnapname"].Command, "mybin")
+	assert.Equal(t, "testsnapname", metadata.Name)
+	assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
+	assert.Equal(t, "mybin", metadata.Apps["testsnapname"].Command)
 }
 
 func TestRunPipeMetadata(t *testing.T) {
@@ -129,11 +129,16 @@ func TestRunPipeMetadata(t *testing.T) {
 			Description:  "test description",
 			Apps: map[string]config.SnapcraftAppMetadata{
 				"mybin": {
-					Plugs:  []string{"home", "network"},
+					Plugs:  []string{"home", "network", "personal-files"},
 					Daemon: "simple",
 					Args:   "--foo --bar",
 				},
 			},
+			Plugs: map[string]interface{}{
+				"personal-files": map[string]interface{}{
+					"read": []string{"$HOME/test"},
+				},
+			},
 		},
 	})
 	ctx.Git.CurrentTag = "v1.2.3"
@@ -145,12 +150,13 @@ func TestRunPipeMetadata(t *testing.T) {
 	var metadata Metadata
 	err = yaml.Unmarshal(yamlFile, &metadata)
 	assert.NoError(t, err)
-	assert.Equal(t, metadata.Apps["mybin"].Plugs, []string{"home", "network"})
-	assert.Equal(t, metadata.Apps["mybin"].Daemon, "simple")
-	assert.Equal(t, metadata.Apps["mybin"].Command, "mybin --foo --bar")
-	assert.Equal(t, metadata.Apps["testprojectname"].Plugs, []string{"home", "network"})
-	assert.Equal(t, metadata.Apps["testprojectname"].Daemon, "simple")
-	assert.Equal(t, metadata.Apps["testprojectname"].Command, "mybin --foo --bar")
+	assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["mybin"].Plugs)
+	assert.Equal(t, "simple", metadata.Apps["mybin"].Daemon)
+	assert.Equal(t, "mybin --foo --bar", metadata.Apps["mybin"].Command)
+	assert.Equal(t, []string{"home", "network", "personal-files"}, metadata.Apps["testprojectname"].Plugs)
+	assert.Equal(t, "simple", metadata.Apps["testprojectname"].Daemon)
+	assert.Equal(t, "mybin --foo --bar", metadata.Apps["testprojectname"].Command)
+	assert.Equal(t, map[interface{}]interface{}(map[interface{}]interface{}{"read": []interface{}{"$HOME/test"}}), metadata.Plugs["personal-files"])
 }
 
 func TestNoSnapcraftInPath(t *testing.T) {