Browse code

Merge "Copy policy_add() from Grenade functions"

Jenkins authored on 2013/09/05 01:47:04
Showing 1 changed files
... ...
@@ -1644,6 +1644,37 @@ vercmp_numbers() {
1644 1644
 }
1645 1645
 
1646 1646
 
1647
+# ``policy_add policy_file policy_name policy_permissions``
1648
+#
1649
+# Add a policy to a policy.json file
1650
+# Do nothing if the policy already exists
1651
+
1652
+function policy_add() {
1653
+    local policy_file=$1
1654
+    local policy_name=$2
1655
+    local policy_perm=$3
1656
+
1657
+    if grep -q ${policy_name} ${policy_file}; then
1658
+        echo "Policy ${policy_name} already exists in ${policy_file}"
1659
+        return
1660
+    fi
1661
+
1662
+    # Add a terminating comma to policy lines without one
1663
+    # Remove the closing '}' and all lines following to the end-of-file
1664
+    local tmpfile=$(mktemp)
1665
+    uniq ${policy_file} | sed -e '
1666
+        s/]$/],/
1667
+        /^[}]/,$d
1668
+    ' > ${tmpfile}
1669
+
1670
+    # Append policy and closing brace
1671
+    echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1672
+    echo "}" >>${tmpfile}
1673
+
1674
+    mv ${tmpfile} ${policy_file}
1675
+}
1676
+
1677
+
1647 1678
 # Restore xtrace
1648 1679
 $XTRACE
1649 1680