You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/02/21 15:14:52 UTC

[mynewt-newt] branch master updated: Allow to inject syscfg from command line

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git


The following commit(s) were added to refs/heads/master by this push:
     new fbc127a  Allow to inject syscfg from command line
fbc127a is described below

commit fbc127ac591c8d4261499597cd067246eaefe265
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Thu Feb 17 10:05:11 2022 +0100

    Allow to inject syscfg from command line
    
    This adds new -S/--syscfg command line switch to build and config
    commands which allows to inject syscfg settings from command line.
    Injected settings have highest priority and thus will override any
    other value set by any other package.
    
    The purpose of this setting is to easily allow to use single targets
    to build multiple similar images, e.g. that have only different BLE
    public address different. It is not intended to be used instead of
    having multiple targets to build different images (which is still
    preferred setup), but to allow quickly build variants of the same
    target wihout need to edit files every time.
---
 newt/cli/build_cmds.go      |  3 +++
 newt/cli/image_cmds.go      |  3 +++
 newt/cli/target_cfg_cmds.go | 33 +++++++++++++++++++++++++++++----
 newt/resolve/resolve.go     |  1 +
 newt/syscfg/syscfg.go       | 23 +++++++++++++++++++++++
 util/util.go                |  1 +
 6 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index 63d08bd..6902e09 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -440,6 +440,9 @@ func AddBuildCommands(cmd *cobra.Command) {
 	buildCmd.Flags().BoolVarP(&printShellCmds, "printCmds", "p", false,
 		"Print executed build commands")
 
+	buildCmd.Flags().StringVarP(&util.InjectSyscfg, "syscfg", "S", "",
+		"Injected syscfg settings, key=value pairs separated by colon")
+
 	buildCmd.Flags().BoolVar(&executeShell, "executeShell", false,
 		"Execute build command using /bin/sh (Linux and MacOS only)")
 
diff --git a/newt/cli/image_cmds.go b/newt/cli/image_cmds.go
index 6da30a7..0d30926 100644
--- a/newt/cli/image_cmds.go
+++ b/newt/cli/image_cmds.go
@@ -209,6 +209,9 @@ func AddImageCommands(cmd *cobra.Command) {
 	createImageCmd.PersistentFlags().BoolVarP(&useLegacyTLV,
 		"legacy-tlvs", "L", false, "Use legacy TLV values for NONCE and SECRET_ID")
 
+	createImageCmd.Flags().StringVarP(&util.InjectSyscfg, "syscfg", "", "",
+		"Injected syscfg settings, key=value pairs separated by colon")
+
 	cmd.AddCommand(createImageCmd)
 	AddTabCompleteFn(createImageCmd, targetList)
 
diff --git a/newt/cli/target_cfg_cmds.go b/newt/cli/target_cfg_cmds.go
index f5af21b..8ea1ba0 100644
--- a/newt/cli/target_cfg_cmds.go
+++ b/newt/cli/target_cfg_cmds.go
@@ -62,8 +62,16 @@ func printSetting(entry syscfg.CfgEntry) {
 		util.StatusMessage(util.VERBOSITY_DEFAULT,
 			"    * Overridden: ")
 		for i := 1; i < len(entry.History); i++ {
-			util.StatusMessage(util.VERBOSITY_DEFAULT, "%s, ",
-				entry.History[i].Source.FullName())
+			var fullName string
+
+			lpkg := entry.History[i].Source
+			if lpkg == nil {
+				fullName = "<command line>"
+			} else {
+				fullName = lpkg.FullName()
+			}
+
+			util.StatusMessage(util.VERBOSITY_DEFAULT, "%s, ", fullName)
 		}
 		util.StatusMessage(util.VERBOSITY_DEFAULT,
 			"default=%s\n", entry.History[0].Value)
@@ -82,8 +90,16 @@ func printBriefSetting(entry syscfg.CfgEntry) {
 	var extras []string
 
 	if len(entry.History) > 1 {
-		s := fmt.Sprintf("overridden by %s",
-			entry.History[len(entry.History)-1].Source.FullName())
+		var fullName string
+
+		lpkg := entry.History[len(entry.History)-1].Source
+		if lpkg == nil {
+			fullName = "<command line>"
+		} else {
+			fullName = lpkg.FullName()
+		}
+
+		s := fmt.Sprintf("overridden by %s", fullName)
 		extras = append(extras, s)
 	}
 	if len(entry.ValueRefName) > 0 {
@@ -786,6 +802,9 @@ func targetCfgCmdAll() []*cobra.Command {
 		Run:   targetConfigShowCmd,
 	}
 
+	configShowCmd.Flags().StringVarP(&util.InjectSyscfg, "syscfg", "S", "",
+		"Injected syscfg settings, key=value pairs separated by colon")
+
 	configCmd.AddCommand(configShowCmd)
 	AddTabCompleteFn(configShowCmd, func() []string {
 		return append(targetList(), unittestList()...)
@@ -798,6 +817,9 @@ func targetCfgCmdAll() []*cobra.Command {
 		Run:   targetConfigBriefCmd,
 	}
 
+	configBriefCmd.Flags().StringVarP(&util.InjectSyscfg, "syscfg", "S", "",
+		"Injected syscfg settings, key=value pairs separated by colon")
+
 	configCmd.AddCommand(configBriefCmd)
 	AddTabCompleteFn(configBriefCmd, func() []string {
 		return append(targetList(), unittestList()...)
@@ -810,6 +832,9 @@ func targetCfgCmdAll() []*cobra.Command {
 		Run:   targetConfigFlatCmd,
 	}
 
+	configFlatCmd.Flags().StringVarP(&util.InjectSyscfg, "syscfg", "S", "",
+		"Injected syscfg settings, key=value pairs separated by colon")
+
 	configCmd.AddCommand(configFlatCmd)
 	AddTabCompleteFn(configFlatCmd, func() []string {
 		return append(targetList(), unittestList()...)
diff --git a/newt/resolve/resolve.go b/newt/resolve/resolve.go
index 0f110aa..6e04cad 100644
--- a/newt/resolve/resolve.go
+++ b/newt/resolve/resolve.go
@@ -598,6 +598,7 @@ func (r *Resolver) reloadCfg() (bool, error) {
 		return false, err
 	}
 
+	cfg.AddInjectedSettings()
 	cfg.ResolveValueRefs()
 
 	// Determine if any new settings have been added or if any existing
diff --git a/newt/syscfg/syscfg.go b/newt/syscfg/syscfg.go
index 360f259..cb76ae5 100644
--- a/newt/syscfg/syscfg.go
+++ b/newt/syscfg/syscfg.go
@@ -212,6 +212,23 @@ func (cfg *Cfg) ExpandRef(val string) (string, string, error) {
 
 }
 
+func (cfg *Cfg) AddInjectedSettings() {
+	for _, setting := range strings.Split(util.InjectSyscfg, ":") {
+		kv := strings.SplitN(setting, "=", 2)
+		if len(kv) < 2 {
+			continue
+		}
+
+		k := kv[0]
+		v := kv[1]
+
+		if entry, ok := cfg.Settings[k]; ok {
+			entry.appendValue(nil, v)
+			cfg.Settings[k] = entry
+		}
+	}
+}
+
 func (cfg *Cfg) ResolveValueRefs() {
 	for k, entry := range cfg.Settings {
 		refName, val, err := cfg.ExpandRef(strings.TrimSpace(entry.Value))
@@ -1053,6 +1070,12 @@ func settingName(setting string) string {
 }
 
 func findPkgPriority(lpkg *pkg.LocalPackage) int {
+	if lpkg == nil {
+		// this is correct when doing lookup for a setting that was
+		// injected from command line, so let's assume this is always
+		// the case here
+		return int(pkg.PACKAGE_TYPE_TARGET)*pkg.PACKAGE_SUBPRIO_NUM + 1
+	}
 	return int(lpkg.Type())*pkg.PACKAGE_SUBPRIO_NUM + lpkg.SubPriority()
 }
 
diff --git a/util/util.go b/util/util.go
index 3ee46b3..6d8abbb 100644
--- a/util/util.go
+++ b/util/util.go
@@ -44,6 +44,7 @@ import (
 
 var Verbosity int
 var PrintShellCmds bool
+var InjectSyscfg string
 var ExecuteShell bool
 var EscapeShellCmds bool
 var logFile *os.File