From 75f90410a738a9822534feade86bb9a79ba417aa Mon Sep 17 00:00:00 2001 From: Graza Date: Mon, 14 Apr 2025 11:20:47 +0100 Subject: [PATCH 1/3] refactor: added location to format list/show --- internal/display/format.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/internal/display/format.go b/internal/display/format.go index 9c3be5c2..315a488d 100644 --- a/internal/display/format.go +++ b/internal/display/format.go @@ -3,6 +3,7 @@ package display import ( "context" "fmt" + "slices" "strings" "github.com/turbot/go-kit/types" @@ -11,15 +12,18 @@ 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, } } @@ -29,6 +33,7 @@ func (r *FormatResource) GetListData() *printers.RowData { res := printers.NewRowData( printers.NewFieldValue("TYPE", r.Type), printers.NewFieldValue("NAME", r.Name), + printers.NewFieldValue("LOCATION", r.Location), printers.NewFieldValue("DESCRIPTION", r.Description), ) return res @@ -41,6 +46,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), ) @@ -70,19 +76,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("config", &sdktypes.FormatDescription{ Name: f.ShortName, Type: f.Type, Description: types.SafeString(f.Description), @@ -123,15 +132,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) { @@ -160,12 +170,12 @@ 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 + return NewFormatResource("config", 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) From e3f5bee4ee86c3b1fb26a2b81878498ae88eb1f2 Mon Sep 17 00:00:00 2001 From: Graza Date: Mon, 14 Apr 2025 11:27:26 +0100 Subject: [PATCH 2/3] refactor: added '-' as name during a table list output for format types --- internal/display/format.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/display/format.go b/internal/display/format.go index 315a488d..7ed17020 100644 --- a/internal/display/format.go +++ b/internal/display/format.go @@ -30,9 +30,13 @@ func NewFormatResource(location string, format *sdktypes.FormatDescription) *For // 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), ) From a56169522feb530d99445892813b07926ef0b3be Mon Sep 17 00:00:00 2001 From: Graza Date: Mon, 14 Apr 2025 11:50:23 +0100 Subject: [PATCH 3/3] refactor: use config path of file for config based formats --- internal/display/format.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/display/format.go b/internal/display/format.go index 7ed17020..21dbdffe 100644 --- a/internal/display/format.go +++ b/internal/display/format.go @@ -95,7 +95,7 @@ func ListFormatResources(ctx context.Context) ([]*FormatResource, error) { // add custom formats for _, f := range config.GlobalConfig.Formats { - formatMap[f.GetUnqualifiedName()] = NewFormatResource("config", &sdktypes.FormatDescription{ + formatMap[f.GetUnqualifiedName()] = NewFormatResource(f.Config.Range.Filename, &sdktypes.FormatDescription{ Name: f.ShortName, Type: f.Type, Description: types.SafeString(f.Description), @@ -163,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()) } @@ -174,7 +176,12 @@ func getFormatInstance(ctx context.Context, name string) (*FormatResource, error // prefer custom format (order of precedence) if format, isCustom := desc.CustomFormats[name]; isCustom { - return NewFormatResource("config", format), nil + loc := "config" + if filePath != "" { + loc = filePath + + } + return NewFormatResource(loc, format), nil } // if format is a preset