-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.go
100 lines (85 loc) · 2.42 KB
/
diff.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
package pipelines
import (
"fmt"
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/internal"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/middleware"
"github.com/cycloidio/cycloid-cli/printer"
"github.com/cycloidio/cycloid-cli/printer/factory"
"github.com/spf13/cobra"
)
func NewDiffCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "diff",
Hidden: true,
Short: "not implemented yet",
Long: `not implemented yet`,
RunE: diff,
PreRunE: internal.CheckAPIAndCLIVersion,
}
common.RequiredPersistentFlag(common.WithFlagProject, cmd)
common.RequiredPersistentFlag(common.WithFlagEnv, cmd)
common.RequiredFlag(WithFlagPipeline, cmd)
common.RequiredFlag(WithFlagVars, cmd)
return cmd
}
func diff(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)
var err error
org, err := cmd.Flags().GetString("org")
if err != nil {
return err
}
project, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
env, err := cmd.Flags().GetString("env")
if err != nil {
return err
}
varsPath, err := cmd.Flags().GetString("vars")
if err != nil {
return err
}
pipelinePath, err := cmd.Flags().GetString("pipeline")
if err != nil {
return err
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return errors.Wrap(err, "unable to get output flag")
}
// fetch the printer from the factory
p, err := factory.GetPrinter(output)
if err != nil {
return errors.Wrap(err, "unable to get printer")
}
rawPipeline, err := ioutil.ReadFile(pipelinePath)
if err != nil {
return fmt.Errorf("Pipeline file reading error : %s", err.Error())
}
pipelineTemplate := string(rawPipeline)
rawVars, err := ioutil.ReadFile(varsPath)
if err != nil {
return fmt.Errorf("Pipeline variables file reading error : %s", err.Error())
}
vars := string(rawVars)
pd, err := m.DiffPipeline(org, project, env, pipelineTemplate, vars)
if err != nil {
// print the result on the standard output
if err := p.Print(err, printer.Options{}, os.Stdout); err != nil {
return errors.Wrap(err, "unable to print result")
}
return errors.Wrap(err, "unable obtain a diff")
}
// print the result on the standard output
if err := p.Print(pd, printer.Options{}, os.Stdout); err != nil {
return errors.Wrap(err, "unable to print result")
}
return nil
}