Skip to content
Draft
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions cmd/topo/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"os"
"strings"

"github.com/arm/topo/internal/describe"
"github.com/arm/topo/internal/output/term"
"github.com/arm/topo/internal/target"
"github.com/arm/topo/internal/version"
"github.com/spf13/cobra"
)
Expand All @@ -31,6 +33,20 @@ func addTargetFlag(cmd *cobra.Command) {
cmd.Flags().StringP("target", "t", "", "The SSH destination.")
}

func addTargetDescriptionFlag(cmd *cobra.Command) {
cmd.Flags().String(
"target-description",
"",
"Path to the target description file used to show template compatibility",
)
}

func addTargetDescriptionFlags(cmd *cobra.Command) {
addTargetFlag(cmd)
addTargetDescriptionFlag(cmd)
cmd.MarkFlagsMutuallyExclusive("target", "target-description")
}

func addDryRunFlag(cmd *cobra.Command) {
cmd.Flags().Bool("dry-run", false, "Show what commands would be executed without actually running them")
}
Expand Down Expand Up @@ -63,6 +79,29 @@ func requireTarget(cmd *cobra.Command) (string, error) {
return t, nil
}

func retrieveTargetDescription(cmd *cobra.Command) (*target.HardwareProfile, error) {
targetDescriptionPath, err := cmd.Flags().GetString("target-description")
if err != nil {
panic(fmt.Sprintf("internal error: target-description flag not registered: %v", err))
}

if strings.TrimSpace(targetDescriptionPath) != "" {
return describe.ReadTargetDescriptionFromFile(targetDescriptionPath)
}

resolvedTarget, exists := lookupTarget(cmd)
if !exists {
return nil, nil
}

conn := target.NewConnection(resolvedTarget, target.ConnectionOptions{Multiplex: true})
hwProfile, err := describe.GenerateTargetDescription(conn)
if err != nil {
return nil, err
}
return &hwProfile, nil
}

func resolveOutput(cmd *cobra.Command) (term.Format, error) {
flagValue, err := cmd.Flags().GetString("output")
if err != nil {
Expand Down
32 changes: 4 additions & 28 deletions cmd/topo/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import (
"os"

"github.com/arm/topo/internal/catalog"
"github.com/arm/topo/internal/describe"
"github.com/arm/topo/internal/output/printable"
"github.com/arm/topo/internal/output/templates"
"github.com/arm/topo/internal/target"
"github.com/spf13/cobra"
)

var targetDescriptionPath string

var templatesCmd = &cobra.Command{
Use: "templates",
Short: "List available Service Templates",
Expand All @@ -28,22 +24,9 @@ var templatesCmd = &cobra.Command{
return err
}

var profile *target.HardwareProfile
if targetDescriptionPath != "" {
profile, err = describe.ReadTargetDescriptionFromFile(targetDescriptionPath)
if err != nil {
return err
}
} else {
resolvedTarget, exists := lookupTarget(cmd)
if exists {
conn := target.NewConnection(resolvedTarget, target.ConnectionOptions{Multiplex: true})
hwProfile, err := describe.GenerateTargetDescription(conn)
if err != nil {
return err
}
profile = &hwProfile
}
profile, err := retrieveTargetDescription(cmd)
if err != nil {
return err
}

reposWithCompatibility := catalog.AnnotateCompatibility(profile, repos)
Expand All @@ -52,13 +35,6 @@ var templatesCmd = &cobra.Command{
}

func init() {
addTargetFlag(templatesCmd)
templatesCmd.Flags().StringVar(
&targetDescriptionPath,
"target-description",
"",
"Path to the target description file used to show template compatibility",
)
templatesCmd.MarkFlagsMutuallyExclusive("target", "target-description")
addTargetDescriptionFlags(templatesCmd)
rootCmd.AddCommand(templatesCmd)
}
Loading