fcli
fcli copied to clipboard
Documentation: Add fcli variable example of accessing array index
I just learned a trick with fcli variables that I think is pretty neat and should probably include an example in the documentation for this. Apparently, if you have an array/list of entities saved to an fcli variable, you can access a specific element in the array/list by its index.
Take the following example:
First I get a list of artifacts from an application and save the result to an fcli variable called art1
:
fcli ssc artifact ls --av 10009 --store art1
The command above may produce a list of artifacts that may look something like this:
Id Scan types Last scan date Upload date Status
658 WEBINSPECT 2023-06-16T08:49:01.000+00:00 2023-06-16T08:53:34.283+00:00 PROCESS_COMPLETE
417 WEBINSPECT 2023-04-27T12:04:31.000+00:00 2023-04-27T12:09:11.692+00:00 PROCESS_COMPLETE
348 WEBINSPECT 2023-03-24T23:54:41.000+00:00 2023-03-24T23:59:55.154+00:00 PROCESS_COMPLETE
347 WEBINSPECT 2023-03-24T23:48:40.000+00:00 2023-03-24T23:54:04.530+00:00 PROCESS_COMPLETE
...
Now, if you want to use the art1
variable with an fcli command like fcli ssc artifact get
, you can not directly pass in the art1
variable by itself, other wise you'll get an error like this:
$ fcli ssc artifact get ::art1::
Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'id' cannot be found on object of type 'com.fasterxml.jackson.databind.node.ArrayNode' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:222)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:105)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:93)
This obviously doesn't work because we're passing in an array/list of artifacts to the artifact get
command. So instead we need to simply pass in the id of one of the entries in the array/list. To do that, we can do something like the following:
$ fcli ssc artifact get ::art1::get(0).id
---
id: 658
artifactType: "FPR"
status: "PROCESS_COMPLETE"
allowDelete: true
allowPurge: false
allowApprove: false
inModifyingStatus: false
uploadDate: "2023-06-16T08:53:34.283+00:00"
...
By using the get()
method with the fcli variable, we're able to access a specific index in the array/list.