/
update.go
146 lines (124 loc) · 3.72 KB
/
update.go
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
package workflow
import (
"context"
"time"
"github.com/spf13/cobra"
cliworkspace "github.com/Bio-OS/bioos/internal/bioctl/cmd/workspace"
"github.com/Bio-OS/bioos/internal/bioctl/factory"
"github.com/Bio-OS/bioos/internal/bioctl/factory/convert"
clioptions "github.com/Bio-OS/bioos/internal/bioctl/options"
"github.com/Bio-OS/bioos/internal/bioctl/utils/formatter"
"github.com/Bio-OS/bioos/internal/bioctl/utils/prompt"
)
// UpdateOptions is an options to update workflow.
type UpdateOptions struct {
WorkspaceName string
Name string
Description string
workflowClient factory.WorkflowClient
workspaceClient factory.WorkspaceClient
formatter formatter.Formatter
options *clioptions.GlobalOptions
}
func (o *UpdateOptions) GetPromptArgs() ([]string, error) {
workflowName, err := prompt.PromptRequiredString("Workflow", prompt.WithInputMessage("Name"))
if err != nil {
return nil, err
}
return []string{workflowName}, nil
}
func (o *UpdateOptions) GetPromptOptions() error {
var err error
o.WorkspaceName, err = prompt.PromptRequiredString("Workspace", prompt.WithInputMessage("Name"))
if err != nil {
return err
}
o.Name, err = prompt.PromptRequiredString("Name")
if err != nil {
return err
}
o.Description, err = prompt.PromptOptionalString("Description")
if err != nil {
return err
}
return nil
}
func (o *UpdateOptions) GetDefaultFormat() formatter.Format {
return formatter.TextFormat
}
// NewUpdateOptions returns a reference to a UpdateOptions.
func NewUpdateOptions(opt *clioptions.GlobalOptions) *UpdateOptions {
return &UpdateOptions{
options: opt,
}
}
// NewCmdUpdate new a update workflow cmd.
func NewCmdUpdate(opt *clioptions.GlobalOptions) *cobra.Command {
o := NewUpdateOptions(opt)
cmd := &cobra.Command{
Use: "update <workflow_name>",
Short: "update a workflow",
Long: "update a workflow",
Args: cobra.ExactArgs(1),
Run: clioptions.GetCommonRunFunc(o),
}
cmd.Flags().StringVarP(&o.WorkspaceName, "workspace", "w", o.WorkspaceName, "The name of the workspace.")
cmd.Flags().StringVarP(&o.Name, "name", "n", o.Name, "The name of the workflow.")
cmd.Flags().StringVarP(&o.Description, "description", "d", o.Description, "The description of the workflow.")
return cmd
}
// Complete completes all the required options.
func (o *UpdateOptions) Complete() error {
var err error
f := factory.NewFactory(&o.options.Client)
o.workflowClient, err = f.WorkflowClient()
if err != nil {
return err
}
o.workspaceClient, err = f.WorkspaceClient()
if err != nil {
return err
}
if o.options.Stream.OutputFormat == "" {
o.options.Stream.OutputFormat = o.GetDefaultFormat()
}
o.formatter = formatter.NewFormatter(o.options.Stream.OutputFormat, o.options.Stream.Output)
return nil
}
// Validate validate the list options
func (o *UpdateOptions) Validate() error {
if err := o.options.Validate(); err != nil {
return err
}
return nil
}
// Run run the update workflow command
func (o *UpdateOptions) Run(args []string) error {
workflowName := args[0]
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(o.options.Client.Timeout))
defer cancel()
workspaceID, err := cliworkspace.ConvertWorkspaceNameIntoID(ctx, o.workspaceClient, o.WorkspaceName)
if err != nil {
return err
}
workflowID, err := ConvertWorkflowNameIntoID(ctx, o.workflowClient, workspaceID, workflowName)
if err != nil {
return err
}
req := &convert.UpdateWorkflowRequest{
ID: workflowID,
WorkspaceID: workspaceID,
}
if o.Name != "" {
req.Name = o.Name
}
if o.Description != "" {
req.Description = o.Description
}
// todo update api needs to add more fields
_, err = o.workflowClient.UpdateWorkflow(ctx, req)
if err != nil {
return err
}
return nil
}