newt; move targetSyscfgKVxxx() routines to be inside syscfg package.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/af2b279c Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/af2b279c Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/af2b279c Branch: refs/heads/mynewt_1_0_0 Commit: af2b279c1bfb9548ea25e17489464c877aaf55ea Parents: 91f6e0b Author: Marko Kiiskila <[email protected]> Authored: Tue Feb 28 16:58:21 2017 -0800 Committer: Marko Kiiskila <[email protected]> Committed: Mon Mar 6 13:37:01 2017 -0800 ---------------------------------------------------------------------- newt/cli/target_cmds.go | 54 ++------------------------------------------ newt/syscfg/syscfg.go | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/af2b279c/newt/cli/target_cmds.go ---------------------------------------------------------------------- diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go index 8888847..a6ea7df 100644 --- a/newt/cli/target_cmds.go +++ b/newt/cli/target_cmds.go @@ -121,7 +121,7 @@ func targetShowCmd(cmd *cobra.Command, args []string) { } // A few variables come from the base package rather than the target. - kvPairs["syscfg"] = targetSyscfgKVToStr( + kvPairs["syscfg"] = syscfg.KeyValueToStr( target.Package().SyscfgV.GetStringMapString("syscfg.vals")) kvPairs["cflags"] = pkgVarSliceString(target.Package(), "pkg.cflags") kvPairs["lflags"] = pkgVarSliceString(target.Package(), "pkg.lflags") @@ -142,56 +142,6 @@ func targetShowCmd(cmd *cobra.Command, args []string) { } } -func targetSyscfgKVFromStr(str string) (map[string]string, error) { - vals := map[string]string{} - - if strings.TrimSpace(str) == "" { - return vals, nil - } - - // Separate syscfg vals are delimited by ':'. - fields := strings.Split(str, ":") - - // Key-value pairs are delimited by '='. If no '=' is present, assume the - // string is the key name and the value is 1. - for _, f := range fields { - if _, err := util.AtoiNoOct(f); err == nil { - return nil, util.FmtNewtError( - "Invalid setting name \"%s\"; must not be a number", f) - } - - kv := strings.SplitN(f, "=", 2) - switch len(kv) { - case 1: - vals[f] = "1" - case 2: - vals[kv[0]] = kv[1] - } - } - - return vals, nil -} - -func targetSyscfgKVToStr(syscfgKv map[string]string) string { - str := "" - - names := make([]string, 0, len(syscfgKv)) - for k, _ := range syscfgKv { - names = append(names, k) - } - sort.Strings(names) - - for i, name := range names { - if i != 0 { - str += ":" - } - - str += fmt.Sprintf("%s=%s", name, syscfgKv[name]) - } - - return str -} - func targetSetCmd(cmd *cobra.Command, args []string) { if len(args) < 2 { NewtUsage(cmd, @@ -234,7 +184,7 @@ func targetSetCmd(cmd *cobra.Command, args []string) { // instead of the target. if kv[0] == "target.syscfg" { t.Package().SyscfgV = viper.New() - kv, err := targetSyscfgKVFromStr(kv[1]) + kv, err := syscfg.KeyValueFromStr(kv[1]) if err != nil { NewtUsage(cmd, err) } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/af2b279c/newt/syscfg/syscfg.go ---------------------------------------------------------------------- diff --git a/newt/syscfg/syscfg.go b/newt/syscfg/syscfg.go index f5cef0e..cac298e 100644 --- a/newt/syscfg/syscfg.go +++ b/newt/syscfg/syscfg.go @@ -993,3 +993,53 @@ func EnsureWritten(cfg Cfg, includeDir string) error { return nil } + +func KeyValueFromStr(str string) (map[string]string, error) { + vals := map[string]string{} + + if strings.TrimSpace(str) == "" { + return vals, nil + } + + // Separate syscfg vals are delimited by ':'. + fields := strings.Split(str, ":") + + // Key-value pairs are delimited by '='. If no '=' is present, assume the + // string is the key name and the value is 1. + for _, f := range fields { + if _, err := util.AtoiNoOct(f); err == nil { + return nil, util.FmtNewtError( + "Invalid setting name \"%s\"; must not be a number", f) + } + + kv := strings.SplitN(f, "=", 2) + switch len(kv) { + case 1: + vals[f] = "1" + case 2: + vals[kv[0]] = kv[1] + } + } + + return vals, nil +} + +func KeyValueToStr(syscfgKv map[string]string) string { + str := "" + + names := make([]string, 0, len(syscfgKv)) + for k, _ := range syscfgKv { + names = append(names, k) + } + sort.Strings(names) + + for i, name := range names { + if i != 0 { + str += ":" + } + + str += fmt.Sprintf("%s=%s", name, syscfgKv[name]) + } + + return str +}
