Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.snap text eol=lf
*.txt text eol=lf
*.sh text eol=lf
*.ts text eol=lf
1 change: 1 addition & 0 deletions cli/azd/.vscode/cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ words:
- tekuri
- jsonschema
- rustc
- figspec
languageSettings:
- languageId: go
ignoreRegExpList:
Expand Down
57 changes: 57 additions & 0 deletions cli/azd/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal/figspec"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/spf13/cobra"
)
Expand All @@ -17,6 +18,7 @@ const (
shellZsh = "zsh"
shellFish = "fish"
shellPowerShell = "powershell"
shellFig = "fig"
)

func completionActions(root *actions.ActionDescriptor) {
Expand Down Expand Up @@ -90,6 +92,18 @@ See each sub-command's help for details on how to use the generated script.`,
Footer: getCmdCompletionPowerShellHelpFooter,
},
})

figCmd := &cobra.Command{
Short: "Generate Fig autocomplete spec.",
DisableFlagsInUseLine: true,
}
completionGroup.Add(shellFig, &actions.ActionDescriptorOptions{
Command: figCmd,
ActionResolver: newCompletionFigAction,
FlagsResolver: newCompletionFigFlags,
OutputFormats: []output.Format{output.NoneFormat},
DefaultFormat: output.NoneFormat,
})
}

type completionAction struct {
Expand All @@ -113,6 +127,30 @@ func newCompletionPowerShellAction(cmd *cobra.Command) actions.Action {
return &completionAction{shell: shellPowerShell, cmd: cmd}
}

// Fig completion action and flags
type completionFigFlags struct {
includeHidden bool
}

func newCompletionFigFlags(cmd *cobra.Command) *completionFigFlags {
flags := &completionFigFlags{}
cmd.Flags().BoolVar(&flags.includeHidden, "include-hidden", false, "Include hidden commands in the Fig spec")
_ = cmd.Flags().MarkHidden("include-hidden")
return flags
}

type completionFigAction struct {
flags *completionFigFlags
cmd *cobra.Command
}

func newCompletionFigAction(cmd *cobra.Command, flags *completionFigFlags) actions.Action {
return &completionFigAction{
flags: flags,
cmd: cmd,
}
}

func (a *completionAction) Run(ctx context.Context) (*actions.ActionResult, error) {
rootCmd := a.cmd.Root()

Expand All @@ -137,6 +175,25 @@ func (a *completionAction) Run(ctx context.Context) (*actions.ActionResult, erro
return &actions.ActionResult{}, nil
}

func (a *completionFigAction) Run(ctx context.Context) (*actions.ActionResult, error) {
rootCmd := a.cmd.Root()

// Generate the Fig spec
builder := figspec.NewSpecBuilder(a.flags.includeHidden)
spec := builder.BuildSpec(rootCmd)

// Convert to TypeScript
tsCode, err := spec.ToTypeScript()
if err != nil {
return nil, fmt.Errorf("failed to generate Fig spec: %w", err)
}

// Write to stdout
fmt.Fprint(a.cmd.OutOrStdout(), tsCode)

return &actions.ActionResult{}, nil
}

// Help functions for completion commands

func getCmdCompletionBashHelpDescription(cmd *cobra.Command) string {
Expand Down
35 changes: 35 additions & 0 deletions cli/azd/cmd/figspec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package cmd

import (
"testing"

"github.com/azure/azure-dev/cli/azd/internal/figspec"
"github.com/azure/azure-dev/cli/azd/test/snapshot"
"github.com/stretchr/testify/require"
)

// TestFigSpec generates a Fig autocomplete spec for azd, powering VS Code terminal IntelliSense.
// The generated TypeScript spec must be committed to the vscode repository to enable completions.
//
// To update snapshots (assuming your current directory is cli/azd):
//
// For Bash,
// UPDATE_SNAPSHOTS=true go test ./cmd -run TestFigSpec
//
// For Pwsh,
// $env:UPDATE_SNAPSHOTS='true'; go test ./cmd -run TestFigSpec; $env:UPDATE_SNAPSHOTS=$null
func TestFigSpec(t *testing.T) {
root := NewRootCmd(false, nil, nil)

builder := figspec.NewSpecBuilder(false)
spec := builder.BuildSpec(root)

typescript, err := spec.ToTypeScript()
require.NoError(t, err)

snapshotter := snapshot.NewConfig(".ts")
snapshotter.SnapshotT(t, typescript)
}
Loading
Loading