Skip to content

Commit

Permalink
feature: add support for source <(aws-sso completions --source)
Browse files Browse the repository at this point in the history
Enables installing the completions at build time (aka during package building) and also simplifies the management of completions in the long run as the only required modification to the shell rc file can be a single line.
  • Loading branch information
ghthor committed Feb 17, 2024
1 parent 1d47cef commit 9889bdb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 4 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,9 @@ 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.SourceHelper(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
33 changes: 33 additions & 0 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package helper
*/

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

Expand Down Expand Up @@ -88,6 +90,16 @@ func getScript(shell string) ([]byte, string, error) {
return bytes, path, nil
}

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

return printConfig(c)
}

// 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 +131,27 @@ func UninstallHelper(shell string, path string) error {
return err
}

func printConfig(contents []byte) error {
var err error
var exec string
var source []byte

if exec, err = os.Executable(); err != nil {
return err
}

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

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

_, err = io.Copy(os.Stdout, 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
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 @@ type FileEdit struct {

var prompt = askUser

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

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

return buf.Bytes(), nil
}

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

Expand Down

0 comments on commit 9889bdb

Please sign in to comment.