Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add Fig spec generation
  • Loading branch information
JeffreyCA committed Oct 24, 2025
commit c2d27446b5847aa22c6742f21c8b238452785cce
2 changes: 1 addition & 1 deletion cli/azd/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (p *boolPtr) String() string {
}

func (p *boolPtr) Type() string {
return ""
return "boolptr"
}

const (
Expand Down
56 changes: 56 additions & 0 deletions cli/azd/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
shellZsh = "zsh"
shellFish = "fish"
shellPowerShell = "powershell"
shellFig = "fig"
)

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

// Add hidden Fig completion command
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,29 @@ 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")
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 +174,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
generator := NewFigGenerator(a.flags.includeHidden)
spec := generator.GenerateSpec(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
29 changes: 29 additions & 0 deletions cli/azd/cmd/completion_fig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package cmd

import (
"github.com/azure/azure-dev/cli/azd/internal/figspec"
"github.com/spf13/cobra"
)

// FigSpec wraps the figspec.Spec for easier access
type FigSpec = figspec.Spec

// FigGenerator wraps the figspec.Generator
type FigGenerator struct {
*figspec.Generator
}

// NewFigGenerator creates a new Fig spec generator
func NewFigGenerator(includeHidden bool) *FigGenerator {
return &FigGenerator{
Generator: figspec.NewGenerator(includeHidden),
}
}

// GenerateSpec generates a Fig spec from a Cobra root command
func (g *FigGenerator) GenerateSpec(root *cobra.Command) *FigSpec {
return g.Generator.GenerateSpec(root)
}
32 changes: 32 additions & 0 deletions cli/azd/cmd/figspec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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"
)

// 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)

generator := figspec.NewGenerator(false)
spec := generator.GenerateSpec(root)

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

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