1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-10-30 23:58:09 +02:00

feat: add support for completer in snaps (#1055)

* feat: add support for completer in snaps

* test: add completer test
This commit is contained in:
Tom Payne
2019-06-23 03:37:55 +02:00
committed by Carlos Alexandro Becker
parent 158835f281
commit c9546ec81b
5 changed files with 69 additions and 7 deletions

View File

@@ -50,9 +50,10 @@ type Metadata struct {
// AppMetadata for the binaries that will be in the snap package
type AppMetadata struct {
Command string
Plugs []string `yaml:",omitempty"`
Daemon string `yaml:",omitempty"`
Command string
Plugs []string `yaml:",omitempty"`
Daemon string `yaml:",omitempty"`
Completer string `yaml:",omitempty"`
}
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
@@ -199,6 +200,7 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
appMetadata := AppMetadata{
Command: name,
}
completerPath := ""
if configAppMetadata, ok := snap.Apps[name]; ok {
appMetadata.Plugs = configAppMetadata.Plugs
appMetadata.Daemon = configAppMetadata.Daemon
@@ -206,6 +208,10 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
appMetadata.Command,
configAppMetadata.Args,
}, " "))
if configAppMetadata.Completer != "" {
completerPath = configAppMetadata.Completer
appMetadata.Completer = filepath.Base(completerPath)
}
}
metadata.Apps[name] = appMetadata
metadata.Plugs = snap.Plugs
@@ -220,6 +226,19 @@ func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries [
if err := os.Chmod(destBinaryPath, 0555); err != nil {
return errors.Wrap(err, "failed to change binary permissions")
}
if completerPath != "" {
destCompleterPath := filepath.Join(primeDir, filepath.Base(completerPath))
log.WithField("src", completerPath).
WithField("dst", destCompleterPath).
Debug("linking")
if err := os.Link(completerPath, destCompleterPath); err != nil {
return errors.Wrap(err, "failed to link completer")
}
if err := os.Chmod(destCompleterPath, 0444); err != nil {
return errors.Wrap(err, "failed to change completer permissions")
}
}
}
if _, ok := metadata.Apps[metadata.Name]; !ok {

View File

@@ -245,7 +245,6 @@ func TestNoSnapcraftInPath(t *testing.T) {
assert.EqualError(t, Pipe{}.Run(ctx), ErrNoSnapcraft.Error())
}
func TestRunNoArguments(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(t, err)
@@ -282,6 +281,44 @@ func TestRunNoArguments(t *testing.T) {
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
}
func TestCompleter(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
require.NoError(t, err)
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, err)
var ctx = context.New(config.Project{
ProjectName: "testprojectname",
Dist: dist,
Snapcrafts: []config.Snapcraft{
{
NameTemplate: "foo_{{.Arch}}",
Summary: "test summary",
Description: "test description",
Apps: map[string]config.SnapcraftAppMetadata{
"mybin": {
Daemon: "simple",
Args: "",
Completer: "mybin-completer.bash",
},
},
Builds: []string{"foo"},
},
},
})
ctx.Git.CurrentTag = "v1.2.3"
ctx.Version = "v1.2.3"
addBinaries(t, ctx, "foo", dist, "mybin")
require.NoError(t, Pipe{}.Run(ctx))
yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "foo_amd64", "prime", "meta", "snap.yaml"))
require.NoError(t, err)
var metadata Metadata
err = yaml.Unmarshal(yamlFile, &metadata)
require.NoError(t, err)
assert.Equal(t, "mybin", metadata.Apps["mybin"].Command)
assert.Equal(t, "mybin-completer.bash", metadata.Apps["mybin"].Completer)
}
func TestDefault(t *testing.T) {
var ctx = context.New(config.Project{
Builds: []config.Build{

View File

@@ -215,9 +215,10 @@ type Sign struct {
// SnapcraftAppMetadata for the binaries that will be in the snap package
type SnapcraftAppMetadata struct {
Plugs []string
Daemon string
Args string
Plugs []string
Daemon string
Args string
Completer string `yaml:",omitempty"`
}
// Snapcraft config

View File

@@ -112,6 +112,11 @@ snapcrafts:
# If you any to pass args to your binary, you can add them with the
# args option.
args: --foo
# Bash completion snippet. More information about completion here:
# https://docs.snapcraft.io/tab-completion-for-snaps.
completer: drumroll-completion.bash
# Allows plugs to be configured. Plugs like system-files and personal-files
# require this.
# Default is empty.