feat: add list-length method to shell functions

This commit is contained in:
Jose Diaz-Gonzalez committed 2019-11-27 17:08:18 -05:00
1 parent e3f0160e5f
commit 79b344bbe4
3 files changed
+35 -2

No files matched your search

+27
View File
@@ -182,6 +182,33 @@ func PropertyListGet(pluginName string, appName string, property string) (lines
return lines, nil return lines, nil
} }
// PropertyListLength returns the length of a property list
func PropertyListLength(pluginName string, appName string, property string) (length int, err error) {
if !PropertyExists(pluginName, appName, property) {
return length, nil
}
propertyPath := getPropertyPath(pluginName, appName, property)
file, err := os.Open(propertyPath)
if err != nil {
return length, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
return length, fmt.Errorf("Unable to read %s config value for %s.%s: %s", pluginName, appName, property, err.Error())
}
length = len(lines)
return length, nil
}
// PropertyListGetByIndex returns an entry within property list by index // PropertyListGetByIndex returns an entry within property list by index
func PropertyListGetByIndex(pluginName string, appName string, property string, index int) (propertyValue string, err error) { func PropertyListGetByIndex(pluginName string, appName string, property string, index int) (propertyValue string, err error) {
lines, err := PropertyListGet(pluginName, appName, property) lines, err := PropertyListGet(pluginName, appName, property)
+6
View File
@@ -63,6 +63,12 @@ fn-plugin-property-list-get-by-value() {
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lismember" "$PLUGIN" "$APP" "$KEY" "$VALUE" "$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lismember" "$PLUGIN" "$APP" "$KEY" "$VALUE"
} }
fn-plugin-property-list-length() {
declare desc="returns a property list"
declare PLUGIN="$1" APP="$2" KEY="$3"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "llen" "$PLUGIN" "$APP" "$KEY"
}
fn-plugin-property-list-remove() { fn-plugin-property-list-remove() {
declare desc="removes a value from a property list" declare desc="removes a value from a property list"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4" declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4"
+2 -2
View File
@@ -46,13 +46,13 @@ func main() {
case "llen": case "llen":
appName := flag.Arg(2) appName := flag.Arg(2)
property := flag.Arg(3) property := flag.Arg(3)
lines, err := common.PropertyListGet(pluginName, appName, property) length, err := common.PropertyListLength(pluginName, appName, property)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err.Error()) fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
println(len(lines)) println(length)
case "lrange": case "lrange":
appName := flag.Arg(2) appName := flag.Arg(2)
property := flag.Arg(3) property := flag.Arg(3)