From 92bffd5bad15539dd8df92ac3094393961e929c7 Mon Sep 17 00:00:00 2001 From: Michal Gorecki Date: Thu, 1 Feb 2024 10:59:39 +0100 Subject: [PATCH] newt/cli: Add target info command This command prints all target's packages that contain app.flags field and it's values. In the future this command might be extended to show other miscellaneous info about specified target. --- newt/cli/target_cmds.go | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go index a4fa89f84..b3a13e543 100644 --- a/newt/cli/target_cmds.go +++ b/newt/cli/target_cmds.go @@ -23,6 +23,7 @@ import ( "bytes" "fmt" "io/ioutil" + "mynewt.apache.org/newt/newt/ycfg" "os" "sort" "strings" @@ -249,6 +250,50 @@ func targetShowCmd(cmd *cobra.Command, args []string) { } } +func printCflags(appCflags []ycfg.YCfgEntry) { + for _, f := range appCflags { + if itfVals, ok := f.Value.([]interface{}); ok { + for _, itfVal := range itfVals { + if strVal, ok := itfVal.(string); ok { + fmt.Println(strVal) + } + } + } + } +} + +func targetInfoCmd(cmd *cobra.Command, args []string) { + if len(args) < 1 { + NewtUsage(cmd, + util.NewNewtError("Must specify target name")) + } + + TryGetProject() + + b, err := TargetBuilderForTargetOrUnittest(args[0]) + if err != nil { + NewtUsage(cmd, err) + } + + if err := b.PrepBuild(); err != nil { + NewtUsage(nil, err) + } + + fmt.Println("Packages containing app.cflags:") + + for _, rpkg := range b.AppBuilder.SortedRpkgs() { + appCflags, err := rpkg.Lpkg.PkgY.Get("app.cflags", nil) + if err != nil { + NewtUsage(nil, err) + } + if appCflags != nil { + fmt.Println(rpkg.Lpkg.Name()) + printCflags(appCflags) + fmt.Println("") + } + } +} + func targetListCmd(cmd *cobra.Command, args []string) { TryGetProject() targetNames := []string{} @@ -937,6 +982,21 @@ func AddTargetCommands(cmd *cobra.Command) { return append(targetList(), unittestList()...) }) + infoHelpText := "Shows which packages contain app cflags in the target specified " + + "by ." + infoHelpEx := " newt target info \n" + infoHelpEx += " newt target info my_target1" + + infoCmd := &cobra.Command{ + Use: "info", + Short: "Show packages with global cflags", + Long: infoHelpText, + Example: infoHelpEx, + Run: targetInfoCmd, + } + targetCmd.AddCommand(infoCmd) + AddTabCompleteFn(infoCmd, targetList) + for _, cmd := range targetCfgCmdAll() { targetCmd.AddCommand(cmd) }