Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Reworked sm-json to use binary created with Google go

  • Loading branch information...
commit d4118ce11a2870e339d3e7091b74d925d944e6c8 1 parent f4915d3
Long Nguyen longnguyen11288 authored
67 bin/sm-json
... ... @@ -1,67 +0,0 @@
1   -#!/usr/bin/perl -w
2   -
3   -BEGIN{ push @INC, $ENV{'sm_path'}."/core/internal/perl"; }
4   -
5   -use JSON -support_by_pp; # http://search.cpan.org/~makamaka/JSON-2.53/lib/JSON.pm
6   -
7   -sub fail { print STDERR join(" ", @_)."\n"; exit 1; }
8   -
9   -my (
10   - $path, $json_file, $json_file_handle, $json_object, $json, $key, $namespace,
11   - $hash, $subhash, $reference
12   -);
13   -
14   -$path = shift(@ARGV);
15   -$json_file = shift(@ARGV);
16   -
17   -if( ! $path ) {
18   - fail "ERROR: A json path to extract must be given as the first argument\nexample: 'system/hostname'."
19   -} elsif ( ! $json_file ) {
20   - fail "ERROR: A json file must be given as the second argument."
21   -} elsif( ! -e $json_file ) {
22   - fail "ERROR: The json file $json_file does not exist!"
23   -}
24   -
25   -open($json_file_handle, '<', $json_file);
26   -$json_string = join("", <$json_file_handle>);
27   -close($json_file_handle);
28   -
29   -$json_object = new JSON;
30   -$json = $json_object->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($json_string);
31   -
32   -@_ = split("/", $path);
33   -$key = pop(@_);
34   -$namespace = join("/", @_);
35   -$hash = $json;
36   -$subhash = {};
37   -
38   -if($namespace) {
39   - foreach(split("/", $namespace)) {
40   - if(! $hash->{$_}) {
41   - exit 0 # No value.
42   - } elsif(exists $hash->{$_}) {
43   - # TODO: Question: Proper way to reassign the hash to it's subhash, this
44   - # works however I am not sure if there is an easier one liner that does
45   - # not involve a 2nd variable (subhash).
46   - $subhash = $hash->{$_};
47   - $hash = $subhash; # reassign the hash to the value of the current key (namespace).
48   - } else {
49   - fail "ERROR: Namespace ${namespace} missing under path '".join("/", $path)."'";
50   - }
51   - }
52   -}
53   -
54   -$reference = \$hash->{$key};
55   -
56   -if (ref($reference) eq "SCALAR") {
57   - printf "%s\n", $hash->{$key};
58   -} elsif (ref($$reference) eq "ARRAY") {
59   - print STDOUT join(" ", @{$hash->{$key}}) . "\n"; # TODO: Think this out a bit more.
60   -} elsif (ref($hash->{$key}) eq "HASH") {
61   - # This allows for namespace querying to reflect on available keys.
62   - printf "%s\n", join(" ",keys(%{$hash->{$key}}));
63   -} else {
64   - fail "ERROR: Key '$key' missing under namespace '$namespace' missing for path '$path'.";
65   -}
66   -
67   -exit 0; # Success!!!
1  core/install/shell/functions
@@ -238,7 +238,6 @@ install_sm_mustache()
238 238
239 239 install_sm_json()
240 240 {
241   - return 0 # Remove this line once sm-json is ready.
242 241 typeset _url
243 242 if [[ -s "${install_path}/bin/sm-json" ]]
244 243 then
16 core/internal/shell/json/functions
@@ -30,7 +30,7 @@ __sm.json.cli()
30 30 __sm.json.get()
31 31 {
32 32 trace_filter json || set -o xtrace
33   - typeset _type _path _default _variable _json_uri _json_uri_src _array_name
  33 + typeset _type _path _default _variable _json_uri _json_uri_src _array_name _value
34 34
35 35 _json_uri_src="$1" && shift
36 36 _json_uri="${sm_temp_path}/${_json_uri_src##*/}"
@@ -79,9 +79,10 @@ __sm.json.get()
79 79 fi
80 80
81 81 typeset -g ${_variable} >/dev/null
82   -
83   - if ! eval "${_variable}=$("${sm_path}/bin/sm-json" "${_path}" "${_json_uri}")"
84   - then eval "${_variable}=\"\""
  82 +
  83 + if _value="$( "${sm_path}/bin/sm-json" -path "${_path}" -uri "${_json_uri}" )"
  84 + then eval "${_variable}=\"${_value}\""
  85 + else eval "${_variable}=\"${_default}\""
85 86 fi
86 87
87 88 if eval "[[ -z \"\${${_variable}}\" ]]"
@@ -115,9 +116,10 @@ __sm.json.get()
115 116
116 117 typeset -gxa ${_variable} >/dev/null
117 118
118   - if ! eval "${_variable}=($("${sm_path}/bin/sm-json" "${_path}" "${_json_uri}"))"
119   - then eval "${_variable}=()"
120   - fi
  119 + if _value="$( "${sm_path}/bin/sm-json" -path "${_path}" -uri "${_json_uri}" )"
  120 + then eval "${_variable}=\"${_value}\""
  121 + else eval "${_variable}=\"${_default}\""
  122 + fi
121 123
122 124 if eval "[[ -z \"\${${_variable}}\" ]]"
123 125 then eval "${_variable}=(\"${_default}\")"

0 comments on commit d4118ce

Please sign in to comment.
Something went wrong with that request. Please try again.