Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to filter the get command with the go text/template package #13

Merged
merged 6 commits into from
May 15, 2024

Conversation

graugans
Copy link
Owner

@graugans graugans commented May 14, 2024

This provides an easy way to manipulate the data retrieved via the get command:

Print all ports with some details

ovp8xx get  --format '{{$p := prefix "\n"}}{{ range $port, $details := .ports }}{{call $p}}[{{ $port }}
] state: {{ $details.state }},{{print "\t"}}type: {{ $details.info.features.type }},{{print "\t"}}PCIC Port: {{ $detail
s.data.pcicTCPPort }}{{ end }}'

[port0] state: RUN, type: 3D, PCIC Port: 50010
[port1] state: RUN, type: 3D, PCIC Port: 50011
[port2] state: RUN, type: 2D, PCIC Port: 50012
[port3] state: RUN, type: 2D, PCIC Port: 50013
[port6] state: RUN, type: IMU, PCIC Port: 50016

Modify the state of the ports

Set all ports to CONF

ovp8xx get  --format '{{ $state := "CONF" }}{ "ports": { {{$p := prefix ", "}}{{- range $key, $val := .ports -}}{{ if ne $key "port6" }}{{call $p}}"{{- $key -}}":{"state": "{{ $state }}"}{{end}}{{- end }} } }' 

The result looks like this:

{
  "ports": {
    "port0": {
      "state": "CONF"
    },
    "port1": {
      "state": "CONF"
    },
    "port2": {
      "state": "CONF"
    },
    "port3": {
      "state": "CONF"
    }
  }
}

This can be directly piped into the set command

ovp8xx get  --format '{{ $state := "CONF" }}{ "ports": { {{$p := prefix ", "}}{{- range $key, $val := .ports -}}{{ if ne $key "port6" }}{{call $p}}"{{- $key -}}":{"state": "{{ $state }}"}{{end}}{{- end }} } }'  | ovp8xx set

@codecov-commenter
Copy link

codecov-commenter commented May 14, 2024

Codecov Report

Attention: Patch coverage is 0% with 69 lines in your changes are missing coverage. Please review.

Project coverage is 31.35%. Comparing base (603ed91) to head (3c2d4d8).
Report is 2 commits behind head on v2.

Files Patch % Lines
cmd/ovp8xx/cmd/get.go 0.00% 39 Missing ⚠️
internal/versioninfo/version.go 0.00% 18 Missing ⚠️
cmd/ovp8xx/ovp8xx.go 0.00% 8 Missing ⚠️
cmd/ovp8xx/cmd/helper.go 0.00% 3 Missing ⚠️
cmd/ovp8xx/cmd/root.go 0.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##               v2      #13      +/-   ##
==========================================
- Coverage   33.83%   31.35%   -2.49%     
==========================================
  Files          19       20       +1     
  Lines         857      925      +68     
==========================================
  Hits          290      290              
- Misses        566      634      +68     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

graugans added 3 commits May 14, 2024 15:54
Otherwise the user gets a prompt to answer Yes in the terminal. May block forever
This is done with the help of the text/template package. One example to change the state of all ports except port6 to state CONF:

{{ $state := "CONF" }}{ "ports": { {{$p := prefix ", "}}{{- range $key, $val := .ports -}}{{ if ne $key "port6" }}{{call $p}}"{{- $key -}}":{"state": "{{ $state }}"}{{end}}{{- end }} } }

There are two custom functions toJSON and prefix
@graugans graugans merged commit 9347fc1 into v2 May 15, 2024
8 checks passed
@graugans graugans deleted the feature/templates branch July 11, 2024 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants