forked from reanahub/reana-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretention_rules_list.go
More file actions
151 lines (130 loc) · 3.61 KB
/
Copy pathretention_rules_list.go
File metadata and controls
151 lines (130 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
This file is part of REANA.
Copyright (C) 2022, 2025, 2026 CERN.
REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/
package cmd
import (
"reanahub/reana-client-go/client"
"reanahub/reana-client-go/client/operations"
"reanahub/reana-client-go/pkg/displayer"
"reanahub/reana-client-go/pkg/formatter"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
"github.com/spf13/cobra"
)
const retentionRulesListDesc = `
List the retention rules for a workflow.
Example:
$ reana-client retention-rules-list -w myanalysis.42
`
const retentionRulesListFormatFlagDesc = `Format output according to column titles or column
values. Use <columm_name>=<column_value> format.
E.g. display pattern and status of active retention rules
--format workspace_files,status=active.`
type retentionRulesListOptions struct {
token string
workflow string
jsonOutput bool
formatFilters []string
}
// newRetentionRulesListCmd creates a command to list retention rules.
func newRetentionRulesListCmd() *cobra.Command {
o := &retentionRulesListOptions{}
cmd := &cobra.Command{
Use: "retention-rules-list",
Short: "List the retention rules for a workflow.",
Long: retentionRulesListDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
},
}
f := cmd.Flags()
f.StringVarP(
&o.token,
"access-token",
"t",
"",
"Access token of the current user.",
)
f.StringVarP(
&o.workflow,
"workflow",
"w",
"",
"Name or UUID of the workflow. Overrides value of REANA_WORKON environment variable.",
)
f.BoolVar(&o.jsonOutput, "json", false, "Get output in JSON format.")
f.StringSliceVar(
&o.formatFilters,
"format",
[]string{},
retentionRulesListFormatFlagDesc,
)
return cmd
}
func (o *retentionRulesListOptions) run(cmd *cobra.Command) error {
retentionRulesParams := operations.NewGetWorkflowRetentionRulesParams()
retentionRulesParams.SetAccessToken(&o.token)
retentionRulesParams.SetWorkflowIDOrName(o.workflow)
api, err := client.ApiClient()
if err != nil {
return err
}
retentionRulesResp, err := api.Operations.GetWorkflowRetentionRules(
retentionRulesParams,
)
if err != nil {
return err
}
df := buildRetentionRulesDataFrame(retentionRulesResp)
df = df.Arrange(dataframe.Sort("retention_days"))
parsedFormatFilters := formatter.ParseFormatParameters(
o.formatFilters,
true,
)
df, err = formatter.FormatDataFrame(df, parsedFormatFilters)
if err != nil {
return err
}
if o.jsonOutput {
err := displayer.DisplayJsonOutput(df.Maps(), cmd.OutOrStdout())
if err != nil {
return err
}
} else {
data := formatter.DataFrameToStringData(df)
displayer.DisplayTable(df.Names(), data, cmd.OutOrStdout())
}
return nil
}
func buildRetentionRulesDataFrame(
response *operations.GetWorkflowRetentionRulesOK,
) dataframe.DataFrame {
workspaceFilesSeries := series.New(
[]string{},
series.String,
"workspace_files",
)
retentionDaysSeries := series.New([]int{}, series.Int, "retention_days")
applyOnSeries := series.New([]string{}, series.String, "apply_on")
statusSeries := series.New([]string{}, series.String, "status")
for _, rule := range response.Payload.RetentionRules {
workspaceFilesSeries.Append(rule.WorkspaceFiles)
retentionDaysSeries.Append(int(rule.RetentionDays))
if rule.ApplyOn == nil {
applyOnSeries.Append(nil)
} else {
applyOnSeries.Append(*rule.ApplyOn)
}
statusSeries.Append(rule.Status)
}
return dataframe.New(
workspaceFilesSeries,
retentionDaysSeries,
applyOnSeries,
statusSeries,
)
}