Yuta HIGUCHI
Committed by Gerrit Code Review

resources cmd: option to show only available resource

Change-Id: I75fd80c2d3258b76ca1b99581b0b5ada8e203f11
...@@ -56,6 +56,11 @@ import com.google.common.collect.TreeRangeSet; ...@@ -56,6 +56,11 @@ import com.google.common.collect.TreeRangeSet;
56 description = "Lists registered resources") 56 description = "Lists registered resources")
57 public class ResourcesCommand extends AbstractShellCommand { 57 public class ResourcesCommand extends AbstractShellCommand {
58 58
59 + @Option(name = "-a", aliases = "--available",
60 + description = "Output available resources only",
61 + required = false, multiValued = false)
62 + boolean availablesOnly = false;
63 +
59 @Option(name = "-s", aliases = "--sort", description = "Sort output", 64 @Option(name = "-s", aliases = "--sort", description = "Sort output",
60 required = false, multiValued = false) 65 required = false, multiValued = false)
61 boolean sort = false; 66 boolean sort = false;
...@@ -102,7 +107,6 @@ public class ResourcesCommand extends AbstractShellCommand { ...@@ -102,7 +107,6 @@ public class ResourcesCommand extends AbstractShellCommand {
102 } 107 }
103 108
104 private void printResource(Resource resource, int level) { 109 private void printResource(Resource resource, int level) {
105 - // TODO add an option to show only available resource
106 // workaround to preserve the original behavior of ResourceService#getRegisteredResources 110 // workaround to preserve the original behavior of ResourceService#getRegisteredResources
107 Set<Resource> children; 111 Set<Resource> children;
108 if (resource instanceof DiscreteResource) { 112 if (resource instanceof DiscreteResource) {
...@@ -118,21 +122,27 @@ public class ResourcesCommand extends AbstractShellCommand { ...@@ -118,21 +122,27 @@ public class ResourcesCommand extends AbstractShellCommand {
118 if (resource instanceof ContinuousResource) { 122 if (resource instanceof ContinuousResource) {
119 print("%s%s: %f", Strings.repeat(" ", level), 123 print("%s%s: %f", Strings.repeat(" ", level),
120 resourceName, 124 resourceName,
121 - // Note: last() does not return, what we've registered
122 - // following does not work
123 - //((Class<?>) resource.last()).getSimpleName(),
124 ((ContinuousResource) resource).value()); 125 ((ContinuousResource) resource).value());
125 // Continuous resource is terminal node, stop here 126 // Continuous resource is terminal node, stop here
126 return; 127 return;
127 } else { 128 } else {
129 + String availability = "";
130 + if (availablesOnly && !children.isEmpty()) {
131 + // intermediate nodes cannot be omitted, print availability
132 + if (resourceService.isAvailable(resource)) {
133 + availability = " ✔";
134 + } else {
135 + availability = " ✘";
136 + }
137 + }
128 String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); 138 String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
129 if (toString.startsWith(resourceName)) { 139 if (toString.startsWith(resourceName)) {
130 - print("%s%s", Strings.repeat(" ", level), 140 + print("%s%s%s", Strings.repeat(" ", level),
131 - toString); 141 + toString, availability);
132 } else { 142 } else {
133 - print("%s%s: %s", Strings.repeat(" ", level), 143 + print("%s%s: %s%s", Strings.repeat(" ", level),
134 resourceName, 144 resourceName,
135 - toString); 145 + toString, availability);
136 } 146 }
137 } 147 }
138 } 148 }
...@@ -219,6 +229,10 @@ public class ResourcesCommand extends AbstractShellCommand { ...@@ -219,6 +229,10 @@ public class ResourcesCommand extends AbstractShellCommand {
219 // resource which has children should be printed 229 // resource which has children should be printed
220 return true; 230 return true;
221 } 231 }
232 + if (availablesOnly && !resourceService.isAvailable(resource)) {
233 + // don't print unavailable discrete resource
234 + return false;
235 + }
222 } else if (!(resource instanceof ContinuousResource)) { 236 } else if (!(resource instanceof ContinuousResource)) {
223 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName()); 237 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
224 return false; 238 return false;
......