Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support for installing completions at package build time #779

Merged
merged 4 commits into from
Mar 8, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

* Add `aws-sso-cli completion --source` flag to generate completion script and
print to stdout. #779

## [v1.14.3] - 2024-01-15

### Bugs
Expand Down
6 changes: 5 additions & 1 deletion cmd/aws-sso/completions_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

type CompleteCmd struct {
Source bool `kong:"help='Print out completions for sourcing in the active shell',xor='action'"`
Install bool `kong:"short='I',help='Install shell completions',xor='action'"`
Uninstall bool `kong:"short='U',help='Uninstall shell completions',xor='action'"`
UninstallPre19 bool `kong:"help='Uninstall pre-v1.9 shell completion integration',xor='action',xor='shell,script'"`
Expand All @@ -37,7 +38,10 @@ type CompleteCmd struct {
func (cc *CompleteCmd) Run(ctx *RunContext) error {
var err error

if ctx.Cli.Completions.Install {
if ctx.Cli.Completions.Source {
err = helper.NewSourceHelper(os.Executable, os.Stdout).
Generate(ctx.Cli.Completions.Shell)
} else if ctx.Cli.Completions.Install {
// install the current auto-complete helper
err = helper.InstallHelper(ctx.Cli.Completions.Shell, ctx.Cli.Completions.ShellScript)
} else if ctx.Cli.Completions.Uninstall {
Expand Down
45 changes: 45 additions & 0 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/

import (
"bytes"
"embed"
"fmt"
"io"
"os"
"path"

Expand Down Expand Up @@ -88,6 +90,33 @@
return bytes, path, nil
}

type SourceHelper struct {
getExe func() (string, error)
output io.Writer
}

func NewSourceHelper(getExe func() (string, error), output io.Writer) *SourceHelper {
return &SourceHelper{
getExe: os.Executable,
output: os.Stdout,
}

Check warning on line 102 in internal/helper/helper.go

View check run for this annotation

Codecov / codecov/patch

internal/helper/helper.go#L98-L102

Added lines #L98 - L102 were not covered by tests
}

// SourceHelper can be used to generate the completions script for immediate sourcing in the active shell
func (h SourceHelper) Generate(shell string) error {
c, _, err := getScript(shell)
if err != nil {
return err
}

execPath, err := h.getExe()
if err != nil {
return err
}

Check warning on line 115 in internal/helper/helper.go

View check run for this annotation

Codecov / codecov/patch

internal/helper/helper.go#L114-L115

Added lines #L114 - L115 were not covered by tests

return printConfig(c, execPath, h.output)
}

// InstallHelper installs any helper code into our shell startup script(s)
func InstallHelper(shell string, path string) error {
c, defaultPath, err := getScript(shell)
Expand Down Expand Up @@ -119,6 +148,22 @@
return err
}

func printConfig(contents []byte, execPath string, output io.Writer) error {
var err error
var source []byte

args := map[string]string{
"Executable": execPath,
}

if source, err = utils.GenerateSource(string(contents), args); err != nil {
return err
}

Check warning on line 161 in internal/helper/helper.go

View check run for this annotation

Codecov / codecov/patch

internal/helper/helper.go#L160-L161

Added lines #L160 - L161 were not covered by tests

_, err = io.Copy(output, bytes.NewReader(source))
return err
}

// installConfigFile adds our blob to the given file
func installConfigFile(path string, contents []byte) error {
var err error
Expand Down
70 changes: 70 additions & 0 deletions internal/helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package helper

import (
"bytes"
"errors"
"testing"
"unicode"

"github.com/stretchr/testify/require"
)

func TestSourceHelper(t *testing.T) {
testcases := []struct {
shell string
expectedError error
expectedOutput []byte
}{
{
shell: "bash",
expectedOutput: []byte(`
complete -F __aws_sso_profile_complete aws-sso-profile
complete -C /bin/aws-sso-cli aws-sso
`),
},
{
shell: "zsh",
expectedOutput: []byte(`
compdef __aws_sso_profile_complete aws-sso-profile
complete -C /bin/aws-sso-cli aws-sso
`),
},
{
shell: "fish",
expectedOutput: []byte(`
function __complete_aws-sso
set -lx COMP_LINE (commandline -cp)
test -z (commandline -ct)
and set COMP_LINE "$COMP_LINE "
/bin/aws-sso-cli
end
complete -f -c aws-sso -a "(__complete_aws-sso)"
`),
},
{
shell: "nushell",
expectedError: errors.New("unsupported shell: nushell"),
},
}

for _, tc := range testcases {
tc := tc

t.Run(tc.shell, func(t *testing.T) {
t.Parallel()
var (
output = bytes.NewBuffer(nil)
h = &SourceHelper{
getExe: func() (string, error) {
return "/bin/aws-sso-cli", nil
},
output: output,
}
)

err := h.Generate(tc.shell)
require.Equal(t, tc.expectedError, err)
require.Contains(t, output.String(), string(bytes.TrimLeftFunc(tc.expectedOutput, unicode.IsSpace)))
})
}
}
15 changes: 15 additions & 0 deletions internal/utils/fileedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@

var prompt = askUser

func GenerateSource(fileTemplate string, vars interface{}) ([]byte, error) {
templ, err := template.New("template").Parse(fileTemplate)
if err != nil {
return nil, err
}

Check warning on line 54 in internal/utils/fileedit.go

View check run for this annotation

Codecov / codecov/patch

internal/utils/fileedit.go#L53-L54

Added lines #L53 - L54 were not covered by tests

var buf bytes.Buffer
err = templ.Execute(&buf, vars)
if err != nil {
return nil, err
}

Check warning on line 60 in internal/utils/fileedit.go

View check run for this annotation

Codecov / codecov/patch

internal/utils/fileedit.go#L59-L60

Added lines #L59 - L60 were not covered by tests

return buf.Bytes(), nil
}

func NewFileEdit(fileTemplate string, vars interface{}) (*FileEdit, error) {
var t string

Expand Down
41 changes: 41 additions & 0 deletions internal/utils/fileedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFileEdit(t *testing.T) {
Expand Down Expand Up @@ -187,3 +188,43 @@ func TestPrompter(t *testing.T) {
assert.Equal(t, []byte(
fmt.Sprintf(FILE_TEMPLATE, CONFIG_PREFIX, "foo", CONFIG_SUFFIX)), fBytes)
}

func TestGenerateSource(t *testing.T) {
testcases := []struct {
name string
tpl string
expectedErr error
expected string
}{
{
name: "template with no variables",
tpl: `
I'm a text template if you can believe that
`,
expected: `
I'm a text template if you can believe that
`,
},
{
name: "template",
tpl: `
{{.Name}}
`,
expected: `
template
`,
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
output, err := GenerateSource(tc.tpl, map[string]interface{}{
"Name": tc.name,
})
require.Equal(t, tc.expectedErr, err)
require.Equal(t, tc.expected, string(output))
})
}
}
Loading