@@ -2,6 +2,8 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "strconv"
6
+ "strings"
5
7
"time"
6
8
7
9
"github.com/ovh/cds/cli"
@@ -15,38 +17,53 @@ var workflowTransformAsCodeCmd = cli.Command{
15
17
{Name : _ProjectKey },
16
18
{Name : _WorkflowName },
17
19
},
20
+ Flags : []cli.Flag {
21
+ {Name : "silent" , Type : cli .FlagBool },
22
+ },
18
23
}
19
24
20
- func workflowTransformAsCodeRun (v cli.Values ) error {
25
+ func workflowTransformAsCodeRun (v cli.Values ) ( interface {}, error ) {
21
26
w , err := client .WorkflowGet (v .GetString (_ProjectKey ), v .GetString (_WorkflowName ))
22
27
if err != nil {
23
- return err
28
+ return nil , err
24
29
}
25
30
if w .FromRepository != "" {
26
- fmt .Println ("Workflow is already as code." )
27
- return nil
31
+ return nil , sdk .ErrWorkflowAlreadyAsCode
28
32
}
29
33
30
34
ope , err := client .WorkflowTransformAsCode (v .GetString (_ProjectKey ), v .GetString (_WorkflowName ))
31
35
if err != nil {
32
- return err
36
+ return nil , err
33
37
}
34
38
35
- fmt .Printf ("CDS is pushing files on your repository. A pull request will be created, please wait...\n " )
39
+ if ! v .GetBool ("silent" ) {
40
+ fmt .Println ("CDS is pushing files on your repository. A pull request will be created, please wait..." )
41
+ }
36
42
for {
37
43
if err := client .WorkflowTransformAsCodeFollow (v .GetString (_ProjectKey ), v .GetString (_WorkflowName ), ope ); err != nil {
38
- return err
44
+ return nil , err
39
45
}
40
46
if ope .Status > sdk .OperationStatusProcessing {
41
47
break
42
48
}
43
49
time .Sleep (1 * time .Second )
44
50
}
51
+
52
+ urlSplitted := strings .Split (ope .Setup .Push .PRLink , "/" )
53
+ id , err := strconv .Atoi (urlSplitted [len (urlSplitted )- 1 ])
54
+ if err != nil {
55
+ return nil , fmt .Errorf ("cannot read id from pull request URL %s: %v" , ope .Setup .Push .PRLink , err )
56
+ }
57
+ reponse := struct {
58
+ URL string `cli:"url" json:"url"`
59
+ ID int `cli:"id" json:"id"`
60
+ }{
61
+ URL : ope .Setup .Push .PRLink ,
62
+ ID : id ,
63
+ }
45
64
switch ope .Status {
46
- case sdk .OperationStatusDone :
47
- fmt .Println (cli .Blue (ope .Setup .Push .PRLink ))
48
65
case sdk .OperationStatusError :
49
- return fmt .Errorf ("cannot perform operation: %s" , ope .Error )
66
+ return nil , fmt .Errorf ("cannot perform operation: %s" , ope .Error )
50
67
}
51
- return nil
68
+ return reponse , nil
52
69
}
0 commit comments