Skip to content

Commit

Permalink
Templates sorting and normalize template source keys
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Aug 9, 2023
1 parent 9f77db9 commit 3687579
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cli/azd/cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (a *templateSourceAddAction) Run(ctx context.Context) (*actions.ActionResul
var key = a.args[0]
sourceConfig := &templates.SourceConfig{}

spinnerMessage := "Saving template source"
spinnerMessage := "Validating template source"
a.console.ShowSpinner(ctx, spinnerMessage, input.Step)

// Don't allow source type since they can only be added with known key like 'default' or 'awesome-azd'
Expand All @@ -401,8 +401,6 @@ func (a *templateSourceAddAction) Run(ctx context.Context) (*actions.ActionResul
}

// Validate the custom source config
spinnerMessage := "Validating template source"
a.console.ShowSpinner(ctx, spinnerMessage, input.Step)
_, err := a.sourceManager.CreateSource(ctx, sourceConfig)
a.console.StopSpinner(ctx, spinnerMessage, input.GetStepResultFormat(err))
if err != nil {
Expand All @@ -414,6 +412,9 @@ func (a *templateSourceAddAction) Run(ctx context.Context) (*actions.ActionResul
}
}

spinnerMessage = "Saving template source"
a.console.ShowSpinner(ctx, spinnerMessage, input.Step)

err := a.sourceManager.Add(ctx, key, sourceConfig)
a.console.StopSpinner(ctx, spinnerMessage, input.GetStepResultFormat(err))
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions cli/azd/pkg/templates/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
Expand Down Expand Up @@ -123,7 +124,9 @@ func (sm *sourceManager) Get(ctx context.Context, key string) (*SourceConfig, er

// Add adds a new template source at the specified key and configuration
func (sm *sourceManager) Add(ctx context.Context, key string, source *SourceConfig) error {
existing, err := sm.Get(ctx, key)
newKey := normalizeKey(key)

existing, err := sm.Get(ctx, newKey)
if existing != nil && err == nil {
return fmt.Errorf("template source '%s' already exists, %w", key, ErrSourceExists)
}
Expand All @@ -132,12 +135,14 @@ func (sm *sourceManager) Add(ctx context.Context, key string, source *SourceConf
source.Name = key
}

source.Key = newKey

config, err := sm.configManager.Load()
if err != nil {
return fmt.Errorf("unable to load user configuration: %w", err)
}

path := fmt.Sprintf("%s.%s", baseConfigKey, key)
path := fmt.Sprintf("%s.%s", baseConfigKey, source.Key)
err = config.Set(path, source)
if err != nil {
return fmt.Errorf("unable to add template source '%s': %w", source.Key, err)
Expand All @@ -153,6 +158,8 @@ func (sm *sourceManager) Add(ctx context.Context, key string, source *SourceConf

// Remove removes a template source by the specified key.
func (sm *sourceManager) Remove(ctx context.Context, key string) error {
key = normalizeKey(key)

_, err := sm.Get(ctx, key)
if err != nil && errors.Is(err, ErrSourceNotFound) {
return fmt.Errorf("template source '%s' not found, %w", key, err)
Expand Down Expand Up @@ -206,3 +213,10 @@ func (sm *sourceManager) CreateSource(ctx context.Context, config *SourceConfig)

return source, nil
}

func normalizeKey(key string) string {
key = strings.ToLower(key)
key = strings.ReplaceAll(key, " ", "-")

return key
}
11 changes: 10 additions & 1 deletion cli/azd/pkg/templates/template_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ func (tm *TemplateManager) ListTemplates(ctx context.Context, options *ListOptio
return nil, fmt.Errorf("unable to list templates: %w", err)
}

// Sort by source, then repository path and finally name
slices.SortFunc(templates, func(a *Template, b *Template) bool {
return a.RepositoryPath < b.RepositoryPath
if a.Source != b.Source {
return a.Source < b.Source
}

if a.RepositoryPath != b.RepositoryPath {
return a.RepositoryPath < b.RepositoryPath
}

return a.Name < b.Name
})

allTemplates = append(allTemplates, templates...)
Expand Down

0 comments on commit 3687579

Please sign in to comment.