Skip to content

Commit 604b9ef

Browse files
authored
feat(spec): Add source, destination String methods (#609)
A user recently ran into some issues since they thought they used a newer version of a plugin, but actually used an old one. This PR adds `String()` methods to specs so we can use them in logs/errors. CLI PR cloudquery/cloudquery#6842 ---
1 parent 3d3f20f commit 604b9ef

4 files changed

Lines changed: 164 additions & 0 deletions

File tree

specs/destination.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,17 @@ func (d *Destination) Validate() error {
7171
}
7272
return nil
7373
}
74+
75+
func (d Destination) VersionString() string {
76+
if d.Registry != RegistryGithub {
77+
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Registry, d.Path)
78+
}
79+
pathParts := strings.Split(d.Path, "/")
80+
if len(pathParts) != 2 {
81+
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Path, d.Version)
82+
}
83+
if d.Name == pathParts[1] {
84+
return fmt.Sprintf("%s (%s)", d.Name, d.Version)
85+
}
86+
return fmt.Sprintf("%s (%s@%s)", d.Name, pathParts[1], d.Version)
87+
}

specs/destination_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,71 @@ func TestDestinationUnmarshalSpecValidate(t *testing.T) {
197197
})
198198
}
199199
}
200+
201+
func TestDestination_VersionString(t *testing.T) {
202+
type fields struct {
203+
Name string
204+
Version string
205+
Path string
206+
Registry Registry
207+
}
208+
tests := []struct {
209+
name string
210+
fields fields
211+
want string
212+
}{
213+
{
214+
name: "should use short version without name part in path when those are the same",
215+
fields: fields{
216+
Name: "aws",
217+
Version: "v10.0.0",
218+
Path: "cloudquery/aws",
219+
Registry: RegistryGithub,
220+
},
221+
want: "aws (v10.0.0)",
222+
},
223+
{
224+
name: "should use long version with path when name doesn't match path",
225+
fields: fields{
226+
Name: "my-aws-spec",
227+
Version: "v10.0.0",
228+
Path: "cloudquery/aws",
229+
Registry: RegistryGithub,
230+
},
231+
want: "my-aws-spec (aws@v10.0.0)",
232+
},
233+
{
234+
name: "should handle non GitHub registry",
235+
fields: fields{
236+
Name: "my-aws-spec",
237+
Version: "v10.0.0",
238+
Path: "localhost:7777",
239+
Registry: RegistryGrpc,
240+
},
241+
want: "my-aws-spec (grpc@localhost:7777)",
242+
},
243+
{
244+
name: "should handle malformed path",
245+
fields: fields{
246+
Name: "my-aws-spec",
247+
Version: "v10.0.0",
248+
Path: "aws",
249+
Registry: RegistryGithub,
250+
},
251+
want: "my-aws-spec (aws@v10.0.0)",
252+
},
253+
}
254+
for _, tt := range tests {
255+
t.Run(tt.name, func(t *testing.T) {
256+
d := Destination{
257+
Name: tt.fields.Name,
258+
Version: tt.fields.Version,
259+
Path: tt.fields.Path,
260+
Registry: tt.fields.Registry,
261+
}
262+
if got := d.VersionString(); got != tt.want {
263+
t.Errorf("Destination.String() = %v, want %v", got, tt.want)
264+
}
265+
})
266+
}
267+
}

specs/source.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@ func (s *Source) Validate() error {
128128
}
129129
return nil
130130
}
131+
132+
func (s Source) VersionString() string {
133+
if s.Registry != RegistryGithub {
134+
return fmt.Sprintf("%s (%s@%s)", s.Name, s.Registry, s.Path)
135+
}
136+
pathParts := strings.Split(s.Path, "/")
137+
if len(pathParts) != 2 {
138+
return fmt.Sprintf("%s (%s@%s)", s.Name, s.Path, s.Version)
139+
}
140+
if s.Name == pathParts[1] {
141+
return fmt.Sprintf("%s (%s)", s.Name, s.Version)
142+
}
143+
return fmt.Sprintf("%s (%s@%s)", s.Name, pathParts[1], s.Version)
144+
}

specs/source_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,71 @@ func TestSourceUnmarshalSpecValidate(t *testing.T) {
189189
})
190190
}
191191
}
192+
193+
func TestSpec_VersionString(t *testing.T) {
194+
type fields struct {
195+
Name string
196+
Version string
197+
Path string
198+
Registry Registry
199+
}
200+
tests := []struct {
201+
name string
202+
fields fields
203+
want string
204+
}{
205+
{
206+
name: "should use short version without name part in path when those are the same",
207+
fields: fields{
208+
Name: "aws",
209+
Version: "v10.0.0",
210+
Path: "cloudquery/aws",
211+
Registry: RegistryGithub,
212+
},
213+
want: "aws (v10.0.0)",
214+
},
215+
{
216+
name: "should use long version with path when name doesn't match path",
217+
fields: fields{
218+
Name: "my-aws-spec",
219+
Version: "v10.0.0",
220+
Path: "cloudquery/aws",
221+
Registry: RegistryGithub,
222+
},
223+
want: "my-aws-spec (aws@v10.0.0)",
224+
},
225+
{
226+
name: "should handle non GitHub registry",
227+
fields: fields{
228+
Name: "my-aws-spec",
229+
Version: "v10.0.0",
230+
Path: "localhost:7777",
231+
Registry: RegistryGrpc,
232+
},
233+
want: "my-aws-spec (grpc@localhost:7777)",
234+
},
235+
{
236+
name: "should handle malformed path",
237+
fields: fields{
238+
Name: "my-aws-spec",
239+
Version: "v10.0.0",
240+
Path: "aws",
241+
Registry: RegistryGithub,
242+
},
243+
want: "my-aws-spec (aws@v10.0.0)",
244+
},
245+
}
246+
for _, tt := range tests {
247+
t.Run(tt.name, func(t *testing.T) {
248+
s := Source{
249+
Name: tt.fields.Name,
250+
Version: tt.fields.Version,
251+
Path: tt.fields.Path,
252+
Registry: tt.fields.Registry,
253+
}
254+
if got := s.VersionString(); got != tt.want {
255+
t.Errorf("Source.String() = %v, want %v", got, tt.want)
256+
}
257+
})
258+
}
259+
}

0 commit comments

Comments
 (0)