这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
45 changes: 33 additions & 12 deletions internal/display/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package display
import (
"context"
"fmt"
"slices"
"strings"

"github.com/turbot/go-kit/types"
Expand All @@ -11,24 +12,32 @@ import (
sdktypes "github.com/turbot/tailpipe-plugin-sdk/types"
"github.com/turbot/tailpipe/internal/config"
"github.com/turbot/tailpipe/internal/plugin"
"golang.org/x/exp/slices"
)

const formatTypeDescription = "This is a format type, it can be used for defining instances of formats."

type FormatResource struct {
sdktypes.FormatDescription
Location string `json:"location,omitempty"`
}

func NewFormatResource(format *sdktypes.FormatDescription) *FormatResource {
func NewFormatResource(location string, format *sdktypes.FormatDescription) *FormatResource {
return &FormatResource{
Location: location,
FormatDescription: *format,
}
}

// GetListData implements the printers.Listable interface
func (r *FormatResource) GetListData() *printers.RowData {
name := r.Name
if name == "" {
name = "-"
}
res := printers.NewRowData(
printers.NewFieldValue("TYPE", r.Type),
printers.NewFieldValue("NAME", r.Name),
printers.NewFieldValue("NAME", name),
printers.NewFieldValue("LOCATION", r.Location),
printers.NewFieldValue("DESCRIPTION", r.Description),
)
return res
Expand All @@ -41,6 +50,7 @@ func (r *FormatResource) GetShowData() *printers.RowData {
fields = append(fields,
printers.NewFieldValue("Type", r.Type),
printers.NewFieldValue("Name", r.Name),
printers.NewFieldValue("Location", r.Location),
printers.NewFieldValue("Description", r.Description),
printers.NewFieldValue("Regex", r.Regex),
)
Expand Down Expand Up @@ -70,19 +80,22 @@ func ListFormatResources(ctx context.Context) ([]*FormatResource, error) {
// populate types
for _, t := range desc.FormatTypes {
// types do not have an instance name or description
formatMap[t] = NewFormatResource(&sdktypes.FormatDescription{Type: t})
formatMap[t] = NewFormatResource(pluginVersion.Name, &sdktypes.FormatDescription{
Type: t,
Description: formatTypeDescription,
})
}

// populate presets
for k, f := range desc.FormatPresets {
formatMap[k] = NewFormatResource(f)
formatMap[k] = NewFormatResource(pluginVersion.Name, f)
}
}
}

// add custom formats
for _, f := range config.GlobalConfig.Formats {
formatMap[f.GetUnqualifiedName()] = NewFormatResource(&sdktypes.FormatDescription{
formatMap[f.GetUnqualifiedName()] = NewFormatResource(f.Config.Range.Filename, &sdktypes.FormatDescription{
Name: f.ShortName,
Type: f.Type,
Description: types.SafeString(f.Description),
Expand Down Expand Up @@ -123,15 +136,16 @@ func GetFormatResource(ctx context.Context, name string) (*FormatResource, error

func getFormatType(_ context.Context, formatType string) (*FormatResource, error) {
// Check this is a valid type exported by a plugin
_, ok := config.GetPluginForFormatType(formatType, config.GlobalConfig.PluginVersions)
pluginName, ok := config.GetPluginForFormatType(formatType, config.GlobalConfig.PluginVersions)
if !ok {
// if not found, return error
return nil, fmt.Errorf("no plugin found for type '%s'", formatType)
}

// TODO: #graza once we add source of format add plugin name here https://github.com/turbot/tailpipe/issues/283
return NewFormatResource(&sdktypes.FormatDescription{Type: formatType}), nil

return NewFormatResource(pluginName, &sdktypes.FormatDescription{
Type: formatType,
Description: formatTypeDescription,
}), nil
}

func getFormatInstance(ctx context.Context, name string) (*FormatResource, error) {
Expand All @@ -149,7 +163,9 @@ func getFormatInstance(ctx context.Context, name string) (*FormatResource, error

// if this is a custom format, pass the custom formats to the describe call
var opts []plugin.DescribeOpts
var filePath string
if customFormat, ok := config.GlobalConfig.Formats[name]; ok {
filePath = customFormat.Config.Range.Filename
opts = append(opts, plugin.WithCustomFormats(customFormat), plugin.WithCustomFormatsOnly())
}

Expand All @@ -160,12 +176,17 @@ func getFormatInstance(ctx context.Context, name string) (*FormatResource, error

// prefer custom format (order of precedence)
if format, isCustom := desc.CustomFormats[name]; isCustom {
return NewFormatResource(format), nil
loc := "config"
if filePath != "" {
loc = filePath

}
return NewFormatResource(loc, format), nil
}

// if format is a preset
if format, isPreset := desc.FormatPresets[name]; isPreset {
return NewFormatResource(format), nil
return NewFormatResource(pluginName, format), nil
}

return nil, fmt.Errorf("format '%s' not found", name)
Expand Down