You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/02/25 05:38:15 UTC

incubator-mynewt-newt git commit: (MYNEWT-125) Display valid values for target vars

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 6d1c78a93 -> f58a0d08a


(MYNEWT-125) Display valid values for target vars

Usage:
  newt target set my_target1 var_name   (display valid values for <var_name>)


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/f58a0d08
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/f58a0d08
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/f58a0d08

Branch: refs/heads/develop
Commit: f58a0d08afdd65b82d86c1d53a1440ff0adbae43
Parents: 6d1c78a
Author: Christopher Collins <cc...@gmail.com>
Authored: Wed Feb 24 20:33:35 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Wed Feb 24 20:33:35 2016 -0800

----------------------------------------------------------------------
 newt/cli/pkglist.go | 24 +------------
 newt/cli/repo.go    | 26 ++++++++++++++
 newt/cli/util.go    | 49 ++++++++++++++++++++++++++
 newt/cli/vars.go    | 89 ++++++++++++++++++++++++++++++++++++++++++++++++
 newt/newt.go        | 34 ++++++++++++++++--
 5 files changed, 197 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f58a0d08/newt/cli/pkglist.go
----------------------------------------------------------------------
diff --git a/newt/cli/pkglist.go b/newt/cli/pkglist.go
index d0f84a2..9bd9c9b 100644
--- a/newt/cli/pkglist.go
+++ b/newt/cli/pkglist.go
@@ -235,29 +235,7 @@ func (pkgList *PkgList) loadPkgDir(baseDir string, pkgPrefix string,
 func (pkgList *PkgList) loadPkgs() error {
 	repo := pkgList.Repo
 
-	// XXX: Get additional package search directories from the repo
-	// configuration
-	// If empty, use the default set.
-
-	// Multiple package directories to be searched
-	searchDirs := []string{
-		"app/",
-		"compiler/",
-		"fs/",
-		"libs/",
-		"net/",
-		"hw/bsp/",
-		"hw/mcu/",
-		"hw/mcu/stm",
-		"hw/drivers/",
-		"hw/",
-		"project/",
-		"sys/",
-	}
-
-	if len(repo.AddlPackagePaths) > 0 {
-		searchDirs = append(searchDirs, repo.AddlPackagePaths...)
-	}
+	searchDirs := repo.PkgPaths()
 
 	for _, pkgDir := range searchDirs {
 		pkgBaseDir := repo.BasePath + "/" + pkgDir

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f58a0d08/newt/cli/repo.go
----------------------------------------------------------------------
diff --git a/newt/cli/repo.go b/newt/cli/repo.go
index 39f3818..a5066b4 100644
--- a/newt/cli/repo.go
+++ b/newt/cli/repo.go
@@ -351,6 +351,32 @@ func (repo *Repo) SetConfig(sect string, key string, val string) error {
 	return nil
 }
 
+func (repo *Repo) PkgPaths() []string {
+	// Get additional package search directories from the repo
+	// configuration. If empty, use the default set.
+
+	searchDirs := []string{
+		"app/",
+		"compiler/",
+		"fs/",
+		"libs/",
+		"net/",
+		"hw/bsp/",
+		"hw/mcu/",
+		"hw/mcu/stm",
+		"hw/drivers/",
+		"hw/",
+		"project/",
+		"sys/",
+	}
+
+	if len(repo.AddlPackagePaths) > 0 {
+		searchDirs = append(searchDirs, repo.AddlPackagePaths...)
+	}
+
+	return searchDirs
+}
+
 // Load the repo configuration file
 func (repo *Repo) loadConfig() error {
 	v, err := ReadConfig(repo.BasePath, "app")

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f58a0d08/newt/cli/util.go
----------------------------------------------------------------------
diff --git a/newt/cli/util.go b/newt/cli/util.go
index c49fc39..47549f2 100644
--- a/newt/cli/util.go
+++ b/newt/cli/util.go
@@ -30,6 +30,7 @@ import (
 	"os"
 	"os/exec"
 	"os/signal"
+	"path"
 	"path/filepath"
 	"runtime"
 	"sort"
@@ -201,6 +202,54 @@ func FileModificationTime(path string) (time.Time, error) {
 	return fileInfo.ModTime(), nil
 }
 
+func ChildDirs(path string) ([]string, error) {
+	children, err := ioutil.ReadDir(path)
+	if err != nil {
+		return nil, NewNewtError(err.Error())
+	}
+
+	childDirs := []string{}
+	for _, child := range children {
+		name := child.Name()
+		if !filepath.HasPrefix(name, ".") &&
+			!filepath.HasPrefix(name, "..") &&
+			child.IsDir() {
+
+			childDirs = append(childDirs, name)
+		}
+	}
+
+	return childDirs, nil
+}
+
+func DescendantDirsOfParent(rootPath string, parentName string) ([]string, error) {
+	if NodeNotExist(rootPath) {
+		return []string{}, nil
+	}
+
+	children, err := ChildDirs(rootPath)
+	if err != nil {
+		return nil, err
+	}
+
+	dirs := []string{}
+	if path.Base(rootPath) == parentName {
+		dirs = append(dirs, children...)
+	} else {
+		for _, child := range children {
+			childPath := rootPath + "/" + child
+			subDirs, err := DescendantDirsOfParent(childPath, parentName)
+			if err != nil {
+				return nil, err
+			}
+
+			dirs = append(dirs, subDirs...)
+		}
+	}
+
+	return dirs, nil
+}
+
 // Execute the command specified by cmdStr on the shell and return results
 func ShellCommand(cmdStr string) ([]byte, error) {
 	log.Print("[VERBOSE] " + cmdStr)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f58a0d08/newt/cli/vars.go
----------------------------------------------------------------------
diff --git a/newt/cli/vars.go b/newt/cli/vars.go
new file mode 100644
index 0000000..d6b682d
--- /dev/null
+++ b/newt/cli/vars.go
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package cli
+
+import (
+	"fmt"
+	"sort"
+)
+
+func varsFromChildDirs(key string) ([]string, error) {
+	repo, err := NewRepo()
+	if err != nil {
+		return nil, err
+	}
+
+	valueMap := make(map[string]struct{})
+	searchDirs := repo.PkgPaths()
+	for _, pkgDir := range searchDirs {
+		pkgBaseDir := repo.BasePath + "/" + pkgDir
+		values, err := DescendantDirsOfParent(pkgBaseDir, key)
+		if err != nil {
+			return nil, NewNewtError(err.Error())
+		}
+
+		for _, value := range values {
+			valueMap[value] = struct{}{}
+		}
+	}
+
+	valueSlice := make([]string, 0, len(valueMap))
+	for value, _ := range valueMap {
+		valueSlice = append(valueSlice, value)
+	}
+
+	return valueSlice, nil
+}
+
+var varsMap = map[string]func() ([]string, error){
+	"arch": func() ([]string, error) {
+		return varsFromChildDirs("arch")
+	},
+
+	"bsp": func() ([]string, error) {
+		return varsFromChildDirs("bsp")
+	},
+
+	"compiler": func() ([]string, error) {
+		return varsFromChildDirs("compiler")
+	},
+
+	"project": func() ([]string, error) {
+		return varsFromChildDirs("project")
+	},
+}
+
+// Returns a slice of valid values for the target variable with the specified
+// name.  If an invalid target variable is specified, an error is returned.
+func VarValues(varName string) ([]string, error) {
+	fn := varsMap[varName]
+	if fn == nil {
+		err := NewNewtError(fmt.Sprintf("Unknown target variable: \"%s\"", varName))
+		return nil, err
+	}
+
+	values, err := fn()
+	if err != nil {
+		return nil, err
+	}
+
+	sort.Strings(values)
+	return values, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f58a0d08/newt/newt.go
----------------------------------------------------------------------
diff --git a/newt/newt.go b/newt/newt.go
index da715cd..f223d57 100644
--- a/newt/newt.go
+++ b/newt/newt.go
@@ -92,6 +92,24 @@ func extractTargetVars(args []string, t *cli.Target) error {
 	return nil
 }
 
+func showValidSettings(varName string) error {
+	var err error = nil
+	var values []string
+
+	fmt.Printf("Valid values for target variable \"%s\":\n", varName)
+
+	values, err = cli.VarValues(varName)
+	if err != nil {
+		return err
+	}
+
+	for _, value := range values {
+		fmt.Printf("    %s\n", value)
+	}
+
+	return nil
+}
+
 func targetSetCmd(cmd *cobra.Command, args []string) {
 	if len(args) != 2 {
 		NewtUsage(cmd,
@@ -104,7 +122,18 @@ func targetSetCmd(cmd *cobra.Command, args []string) {
 	}
 	ar := strings.SplitN(args[1], "=", 2)
 
-	t.Vars[ar[0]] = ar[1]
+	if len(ar) == 1 {
+		// User entered a variable name without a value.  Display valid values
+		// for the specified variable.
+		err = showValidSettings(ar[0])
+		if err != nil {
+			NewtUsage(cmd, err)
+		}
+		return
+	} else {
+		// Assign value to specified variable.
+		t.Vars[ar[0]] = ar[1]
+	}
 
 	if err := t.Save(); err != nil {
 		NewtUsage(cmd, err)
@@ -453,7 +482,8 @@ func targetAddCmds(base *cobra.Command) {
 		<target-name> to value <value>.`)
 	setHelpEx := "  newt target set <target-name> <var-name>=<value>\n"
 	setHelpEx += "  newt target set my_target1 var_name=value\n"
-	setHelpEx += "  newt target set my_target1 arch=cortex_m4"
+	setHelpEx += "  newt target set my_target1 arch=cortex_m4\n"
+	setHelpEx += "  newt target set my_target1 var_name   (display valid values for <var_name>)"
 
 	setCmd := &cobra.Command{
 		Use:     "set",