Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,53 @@
name-template: 'v$RESOLVED_VERSION'
tag-template: 'v$RESOLVED_VERSION'
template: |
## Changelog
$CHANGES

## Contributors
Thanks to everyone who shipped with us this release 🎉
$CONTRIBUTORS

## Installation
To install wpm v$RESOLVED_VERSION

**Linux and Mac**
```
```sh
curl -fsSL https://wpm.so/install | bash
```

**Windows**
```
```powershell
powershell -c "irm wpm.so/install.ps1|iex"
```

**Docker**
```
```sh
docker pull trywpm/cli
```

categories:
- title: 'Core'
labels:
- 'core'
- title: 'Enhancements'
labels:
- 'enhancement'
- title: 'Commands'
labels:
- 'command'
- title: 'Bug Fixes'
labels:
- 'bugfix'
- title: 'Misc'
- title: 'Documentation'
labels:
- 'misc'
- 'documentation'
- title: 'Infrastructure'
labels:
- 'infrastructure'
- title: 'Dependencies'
labels:
- 'dependencies'
collapse-after: 5
change-template: '- $TITLE (#$NUMBER)'
version-resolver:
major:
Expand Down
7 changes: 7 additions & 0 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"go.wpm.so/cli/cli/command"
"go.wpm.so/cli/cli/command/completion"
cliflags "go.wpm.so/cli/cli/flags"

"github.com/fvbommel/sortorder"
Expand All @@ -23,6 +24,12 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *c
opts := cliflags.NewClientOptions()
opts.InstallFlags(rootCmd.Flags())

_ = rootCmd.MarkFlagDirname("config")
_ = rootCmd.RegisterFlagCompletionFunc(
"log-level",
completion.FromList("debug", "info", "warn", "error", "fatal"),
)

cobra.AddTemplateFunc("add", func(a, b int) int { return a + b })
cobra.AddTemplateFunc("hasAliases", hasAliases)
cobra.AddTemplateFunc("hasSubCommands", hasSubCommands)
Expand Down
145 changes: 145 additions & 0 deletions cli/command/completion/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package completion

import (
"os"
"sort"

"go.wpm.so/cli/pkg/pm/wpmjson"
"go.wpm.so/cli/pkg/pm/wpmjson/types"
"go.wpm.so/cli/pkg/pm/wpmlock"

"github.com/spf13/cobra"
)

// PackagesFromWpmJson offers completion for package names declared in
// wpm.json's dependencies and devDependencies.
func PackagesFromWpmJson() cobra.CompletionFunc {
return Unique(func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
cwd, err := os.Getwd()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

cfg, err := wpmjson.Read(cwd)
if err != nil || cfg == nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var names []string
if cfg.Dependencies != nil {
for name := range *cfg.Dependencies {
names = append(names, name)
}
}
if cfg.DevDependencies != nil {
for name := range *cfg.DevDependencies {
names = append(names, name)
}
}
sort.Strings(names)
return names, cobra.ShellCompDirectiveNoFileComp
Comment thread
thelovekesh marked this conversation as resolved.
})
}

// PackagesFromLockfile offers completion for package names recorded in
// wpm.lock.
func PackagesFromLockfile() cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
cwd, err := os.Getwd()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

lock, err := wpmlock.Read(cwd)
if err != nil || lock == nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

names := make([]string, 0, len(lock.Packages))
for name := range lock.Packages {
names = append(names, name)
}
sort.Strings(names)
return names, cobra.ShellCompDirectiveNoFileComp
}
}

// PackageTypes offers completion for the closed set of valid package types.
func PackageTypes() cobra.CompletionFunc {
return FromList(
string(types.TypePlugin),
string(types.TypeTheme),
string(types.TypeMuPlugin),
)
}

// PackageVisibility offers completion for the closed set of valid visibility.
func PackageVisibility() cobra.CompletionFunc {
return FromList(
string(types.VisibilityPublic),
string(types.VisibilityPrivate),
)
}

// PublishTags suggests a non-exhaustive list of common dist-tags for
// `wpm publish --tag`.
func PublishTags() cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"latest", "next", "beta", "alpha"}, cobra.ShellCompDirectiveNoFileComp
}
}

// PackageLicenses suggests a non-exhaustive list of common SPDX licenses.
func PackageLicenses() cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"GPL-2.0-or-later", "GPL-3.0-or-later"}, cobra.ShellCompDirectiveNoFileComp
}
}

// FileNames opts a flag or positional argument back into the shell's default
// file completion.
func FileNames() cobra.CompletionFunc {
return func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveDefault
}
}

// FromList offers completion for the given list of options.
func FromList(options ...string) cobra.CompletionFunc {
return Unique(cobra.FixedCompletions(options, cobra.ShellCompDirectiveNoFileComp))
}

// Unique wraps a completion func and removes completion results that are
// already consumed (i.e., appear in "args").
//
// For example:
//
// # initial completion: args is empty, so all results are shown
// command <tab>
// one two three
//
// # "one" is already used so omitted
// command one <tab>
// two three
func Unique(fn cobra.CompletionFunc) cobra.CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
all, dir := fn(cmd, args, toComplete)
if len(all) == 0 || len(args) == 0 {
return all, dir
}

alreadyCompleted := make(map[string]struct{}, len(args))
for _, a := range args {
alreadyCompleted[a] = struct{}{}
}

out := make([]string, 0, len(all))
for _, c := range all {
if _, ok := alreadyCompleted[c]; !ok {
out = append(out, c)
}
}

return out, dir
}
}
4 changes: 4 additions & 0 deletions cli/command/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"unicode"

"go.wpm.so/cli/cli/command"
"go.wpm.so/cli/cli/command/completion"
"go.wpm.so/cli/pkg/output"
"go.wpm.so/cli/pkg/pm/wpmjson"
"go.wpm.so/cli/pkg/pm/wpmjson/types"
Expand Down Expand Up @@ -81,6 +82,9 @@ func NewInitCommand(wpmCli command.Cli) *cobra.Command {
flags.StringVar(&opts.license, "license", "", "Package license")
flags.StringVar(&opts.packageType, "type", "", "Package type (plugin, theme, mu-plugin)")

_ = cmd.RegisterFlagCompletionFunc("type", completion.PackageTypes())
_ = cmd.RegisterFlagCompletionFunc("license", completion.PackageLicenses())

return cmd
}

Expand Down
4 changes: 4 additions & 0 deletions cli/command/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"go.wpm.so/cli/cli"
"go.wpm.so/cli/cli/command"
"go.wpm.so/cli/cli/command/completion"
"go.wpm.so/cli/cli/version"
"go.wpm.so/cli/pkg/archive"
"go.wpm.so/cli/pkg/output"
Expand Down Expand Up @@ -56,6 +57,9 @@ func NewPublishCommand(wpmCli command.Cli) *cobra.Command {
flags.StringVarP(&opts.access, "access", "a", "private", "Set the package access level to either public or private")
flags.BoolVar(&opts.dryRun, "dry-run", false, "Perform a publish operation without actually publishing the package")

_ = cmd.RegisterFlagCompletionFunc("tag", completion.PublishTags())
_ = cmd.RegisterFlagCompletionFunc("access", completion.PackageVisibility())

return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions cli/command/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"go.wpm.so/cli/cli"
"go.wpm.so/cli/cli/command"
"go.wpm.so/cli/cli/command/completion"
"go.wpm.so/cli/cli/command/install"
"go.wpm.so/cli/cli/version"
"go.wpm.so/cli/pkg/output"
Expand All @@ -29,6 +30,7 @@ func NewUninstallCommand(wpmCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runUninstall(cmd.Context(), wpmCli, args)
},
ValidArgsFunction: completion.PackagesFromWpmJson(),
}

return cmd
Expand Down
2 changes: 2 additions & 0 deletions cli/command/why/why.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.wpm.so/cli/cli"
"go.wpm.so/cli/cli/command"
"go.wpm.so/cli/cli/command/completion"
"go.wpm.so/cli/pkg/output"
"go.wpm.so/cli/pkg/pm/wpmjson"
"go.wpm.so/cli/pkg/pm/wpmlock"
Expand All @@ -32,6 +33,7 @@ func NewWhyCommand(wpmCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runWhy(wpmCli, args[0])
},
ValidArgsFunction: completion.PackagesFromLockfile(),
}
return cmd
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/wpm/wpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@ func newWpmCommand(wpmCli *command.WpmCli) *cli.TopLevelCommand {
Version: ver,
DisableFlagsInUseLine: true,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: false,
HiddenDefaultCmd: true,
DisableDescriptions: true,
DisableDefaultCmd: false,
DisableDescriptions: os.Getenv("WPM_CLI_DISABLE_COMPLETION_DESCRIPTION") != "",
},
}

// Disable file-completion by default. Most commands and flags should not
// complete with filenames.
cmd.CompletionOptions.SetDefaultShellCompDirective(cobra.ShellCompDirectiveNoFileComp)

cmd.SetIn(wpmCli.In())
cmd.SetOut(wpmCli.Out())
cmd.SetErr(wpmCli.Err())
Expand Down
Loading