-
Notifications
You must be signed in to change notification settings - Fork 119
/
translate.go
160 lines (142 loc) · 5.68 KB
/
translate.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright IBM Corporation 2020
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/konveyor/move2kube/internal/common"
"github.com/konveyor/move2kube/internal/move2kube"
"github.com/konveyor/move2kube/internal/qaengine"
"github.com/konveyor/move2kube/types/plan"
)
const (
outpathFlag = "outpath"
curateFlag = "curate"
qadisablecliFlag = "qadisablecli"
qaskipFlag = "qaskip"
qaportFlag = "qaport"
qacacheFlag = "qacache"
)
var (
curate bool
qadisablecli bool
qaskip bool
qaport int
qacaches []string
)
var translateCmd = &cobra.Command{
Use: "translate",
Short: "Translate using move2kube plan",
Long: "Translate artifacts using move2kube plan",
Run: func(cmd *cobra.Command, args []string) {
// Global settings
common.IgnoreEnvironment = ignoreEnv
qaengine.StartEngine(qaskip, qaport, qadisablecli)
cachepaths := []string{}
for i := len(qacaches) - 1; i >= 0; i-- {
cachepaths = append(cachepaths, qacaches[i])
}
qaengine.AddCaches(cachepaths)
// Parameter cleaning and curate plan
var p plan.Plan
fi, err := os.Stat(planfile)
if err == nil && fi.IsDir() {
planfile = filepath.Join(planfile, common.DefaultPlanFile)
_, err = os.Stat(planfile)
}
if err != nil {
if !cmd.Flags().Changed(planFlag) && cmd.Flags().Changed(sourceFlag) {
p = move2kube.CreatePlan(srcpath, name)
err := qaengine.SetWriteCache(filepath.Join(outpath, p.Name, common.QACacheFile))
if err != nil {
log.Warnf("Unable to write cache : %s", err)
}
p = move2kube.CuratePlan(p)
} else {
log.Fatalf("Error while accessing plan file path %s : %s ", planfile, err)
}
} else {
p, err = move2kube.ReadPlan(planfile)
if err != nil {
log.Fatalf("Unable to read plan : %s", err)
}
p.Spec.Inputs.SetRootDir(p.Spec.Inputs.RootDir) // Since AbsRootDir and RelRootDir are not serialized. Also cleans up the user input path.
if cmd.Flags().Changed(nameFlag) {
p.Name = name
}
if curate {
err = qaengine.SetWriteCache(filepath.Join(outpath, p.Name, common.QACacheFile))
if err != nil {
log.Warnf("Unable to write cache : %s", err)
}
p = move2kube.CuratePlan(p)
}
}
if srcpath != "" {
p.Spec.Inputs.SetRootDir(srcpath)
}
fi, err = os.Stat(p.Spec.Inputs.RootDir)
if os.IsNotExist(err) {
log.Fatalf("Source directory does not exist: %s.", err)
} else if err != nil {
log.Fatalf("Error while accessing source directory: %s. ", p.Spec.Inputs.RootDir)
} else if !fi.IsDir() {
log.Fatalf("Source path is a file, expected directory: %s.", p.Spec.Inputs.RootDir)
}
outpath = filepath.Clean(outpath)
if outpath == "." {
outpath = ""
}
outpath = filepath.Join(outpath, p.Name)
fi, err = os.Stat(outpath)
if os.IsNotExist(err) {
log.Debugf("Translated artifacts will be written to %s.", outpath)
} else if err != nil {
log.Fatalf("Error while accessing output directory: %s (%s). Exiting", outpath, err)
} else if !fi.IsDir() {
log.Fatalf("Output path is a file, expected directory: %s. Exiting", outpath)
} else {
log.Infof("Output directory exists: %s. The contents might get overwritten.", outpath)
}
err = qaengine.SetWriteCache(filepath.Join(outpath, common.QACacheFile))
if err != nil {
log.Warnf("Unable to write cache : %s", err)
}
// Translate
move2kube.Translate(p, outpath)
log.Infof("Translated target artifacts can be found at [%s].", outpath)
},
}
func init() {
viper.AutomaticEnv()
// Basic options
translateCmd.Flags().StringVarP(&planfile, planFlag, "p", common.DefaultPlanFile, "Specify a plan file to execute.")
translateCmd.Flags().BoolVarP(&curate, curateFlag, "c", false, "Specify whether to curate the plan with a q/a.")
translateCmd.Flags().StringVarP(&srcpath, sourceFlag, "s", "", "Specify source directory to translate. If you already have a m2k.plan then this will override the rootdir value specified in that plan.")
translateCmd.Flags().StringVarP(&outpath, outpathFlag, "o", "", "Path for output. Default will be directory with the project name.")
translateCmd.Flags().StringVarP(&name, nameFlag, "n", common.DefaultProjectName, "Specify the project name.")
translateCmd.Flags().StringSliceVarP(&qacaches, qacacheFlag, "q", []string{}, "Specify qa cache file locations")
// Advanced options
translateCmd.Flags().BoolVar(&ignoreEnv, ignoreEnvFlag, false, "Ignore data from local machine.")
// Hidden options
translateCmd.Flags().BoolVar(&qadisablecli, qadisablecliFlag, false, "Enable/disable the QA Cli sub-system. Without this system, you will have to use the REST API to interact.")
translateCmd.Flags().BoolVar(&qaskip, qaskipFlag, false, "Enable/disable the default answers to questions posed in QA Cli sub-system. If disabled, you will have to answer the questions posed by QA during interaction.")
translateCmd.Flags().IntVar(&qaport, qaportFlag, 0, "Port for the QA service. By default it chooses a random free port.")
_ = translateCmd.Flags().MarkHidden(qadisablecliFlag)
_ = translateCmd.Flags().MarkHidden(qaskipFlag)
_ = translateCmd.Flags().MarkHidden(qaportFlag)
rootCmd.AddCommand(translateCmd)
}