PSA: couple go files for transform

Not a question but wanted to share 2 very simple GO templates I did while learning the syntax and using CLI tool for customer org builds and integration work I do.

If not appropriate for forum, please delete.

I found using GC to get Id's to Names a constant need and using the API explorer to "find" my Name or ID slow or required multiple steps to find info (copy/paste into N++ etc)

I see CLI APIs return simple json or entities array so I created template for each. Extremely simple and my first dabble with them!

Both these extract top level Id and Name into a valid json format, to aid finding pairs quickly.

Go template where no entities on response;

[
	{{ $numRows := len . }}
	{{ range $index, $element := . }}{{if $index}},{{end}}
	{
	"id": "{{.id}}",
	"name":"{{.name}}"
	}
{{- end }}
]

And one where entities is returned from the CLI command;

{{/* .entities top level access from json. Many API calls return an array of entities */}}
{{/* the if index bit on end of range makes the output add comma to rows, and not the last row to generate valid json response */}}
[
	{{ $numRows := len . }}
	{{ range $index, $element := .entities }}{{if $index}},{{end}}
	{
	"id": "{{.id}}",
	"name":"{{.name}}"
	}
{{- end }}
]

Example of use, to get the workflows and grab the id and name to quickly find the Id to add to a trigger creation!

gc flows list --varType workflow --transform GO_entities.txt -p sg

Again, if not appropriate just delete post

Regards
Simon

Simon, thanks for the Go examples. I've been wanting to give Go a try and this will help.

If you're interested, the same can be accomplished with gc and jq:

gc users list | jq ".entities[] | {id: .id, name: .name}"
gc flows list --varType workflow | jq ".entities[] | {id: .id, name: .name}"

Hi Anthony

I had seen examples and also tried jq, it's a 3rd party extra download so I wanted to see what GO could do, as it's built in to GC with the transform. Reducing dependencies :slight_smile: jq is elegant and powerful though.

I also did some work with powershell as well which was more about pulling multiple data sets manipulating outputs and such.

[PSObject] $agentsNames = Get-Content $AgentjsonFilePath | ConvertFrom-Json | Select-Object @{N="id";E={$_.entities.id}}, @{N="name";E={$_.entities.name}}

But I am looking at GO as its built in and just for getting info out I need in a quick and easy way :slight_smile:

We’re mostly a Windows shop, so I just downloaded the .exe. It definitely has some nuances and I’m certainly no expert at it.

I’ve been playing a bit with Powershell, mixing it with jq but I ran into some issues with escaping the quotes. I can’t seem to get it to work. I haven’t tried straight Powershell.

The transform option is definitely cleaner. You can wind up with a pretty long and complex command line piping filters.

I definitely plan on giving Go and transforms a try.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.