Skip to content

Commit 7488ede

Browse files
feat: support --target for template compatibility markers
Use --target on topo templates to generate a target description on the fly and annotate compatibility markers without requiring --target-description. Keep --target-description behavior unchanged, make the flags mutually exclusive. Signed-off-by: Tomás González <[email protected]>
1 parent 853fb1b commit 7488ede

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ topo templates --target-description target-description.yaml
102102
```
103103

104104
This lists available templates and indicates compatibility with your target hardware.
105+
If you don't already have a target description file for your board, you can still use:
106+
107+
```sh
108+
topo templates --target my-board
109+
```
105110

106111
### 4. Clone a template into a new project
107112

cmd/topo/templates.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ var templatesCmd = &cobra.Command{
3434
if err != nil {
3535
return err
3636
}
37+
} else {
38+
resolvedTarget, exists := lookupTarget(cmd)
39+
if exists {
40+
conn := target.NewConnection(resolvedTarget, target.ConnectionOptions{Multiplex: true})
41+
hwProfile, err := describe.GenerateTargetDescription(conn)
42+
if err != nil {
43+
return err
44+
}
45+
profile = &hwProfile
46+
}
3747
}
3848

3949
reposWithCompatibility := catalog.AnnotateCompatibility(profile, repos)
@@ -42,11 +52,13 @@ var templatesCmd = &cobra.Command{
4252
}
4353

4454
func init() {
55+
addTargetFlag(templatesCmd)
4556
templatesCmd.Flags().StringVar(
4657
&targetDescriptionPath,
4758
"target-description",
4859
"",
4960
"Path to the target description file used to show template compatibility",
5061
)
62+
templatesCmd.MarkFlagsMutuallyExclusive("target", "target-description")
5163
rootCmd.AddCommand(templatesCmd)
5264
}

e2e/templates_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package e2e
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestTemplatesTargetDescription(t *testing.T) {
14+
t.Run("matches templates to target description", func(t *testing.T) {
15+
bin := buildBinary(t)
16+
17+
targetDescriptionYAML := `host:
18+
- model: Cortex-A
19+
cores: 4
20+
features:
21+
- asimd
22+
totalmemory_kb: 4194304
23+
`
24+
targetDescriptionPath := writeTargetDescription(t, targetDescriptionYAML)
25+
26+
cmd := exec.Command(bin, "templates", "--target-description", targetDescriptionPath)
27+
out, err := cmd.CombinedOutput()
28+
output := string(out)
29+
30+
require.NoError(t, err, output)
31+
assert.Contains(t, output, "✅ Hello-World")
32+
assert.Contains(t, output, "❌ Lightbulb-moment")
33+
})
34+
t.Run("correctly handles the --target flag when no target description is provided", func(t *testing.T) {
35+
bin := buildBinary(t)
36+
37+
cmd := exec.Command(bin, "templates", "--target", "localhost")
38+
out, err := cmd.CombinedOutput()
39+
output := string(out)
40+
41+
require.NoError(t, err, output)
42+
assert.Contains(t, output, "✅ Hello-World")
43+
})
44+
}
45+
46+
func writeTargetDescription(t *testing.T, content string) string {
47+
t.Helper()
48+
path := filepath.Join(t.TempDir(), "target-description.yaml")
49+
50+
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
51+
return path
52+
}

0 commit comments

Comments
 (0)