forked from konveyor/move2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.go
88 lines (78 loc) · 2.91 KB
/
collect.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
/*
* Copyright IBM Corporation 2021
*
* 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 cmd
import (
"os"
"path/filepath"
"strings"
"github.com/konveyor/move2kube/lib"
"github.com/konveyor/move2kube/types"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type collectFlags struct {
annotations string
outpath string
srcpath string
}
func collectHandler(flags collectFlags) {
var err error
annotations := flags.annotations
outpath := flags.outpath
srcpath := flags.srcpath
if outpath != "" {
if outpath, err = filepath.Abs(outpath); err != nil {
logrus.Fatalf("Failed to make the output directory path %q absolute. Error: %q", outpath, err)
}
}
if srcpath != "" {
srcpath, err = filepath.Abs(srcpath)
if err != nil {
logrus.Fatalf("Failed to make the source directory path %q absolute. Error: %q", srcpath, err)
}
fi, err := os.Stat(srcpath)
if os.IsNotExist(err) {
logrus.Fatalf("Source directory does not exist: %s.", err)
} else if err != nil {
logrus.Fatalf("Error while accessing directory: %s. ", srcpath)
} else if !fi.IsDir() {
logrus.Fatalf("Source path is a file, expected directory: %s.", srcpath)
}
}
outpath = filepath.Join(filepath.Clean(outpath), types.AppNameShort+"_collect")
if annotations == "" {
lib.Collect(srcpath, outpath, []string{})
} else {
lib.Collect(srcpath, outpath, strings.Split(annotations, ","))
}
logrus.Infof("Collect Output in [%s]. Copy this directory into the source directory to be used for planning.", outpath)
}
// GetCollectCommand returns a command to collect information from running applications
func GetCollectCommand() *cobra.Command {
viper.AutomaticEnv()
flags := collectFlags{}
collectCmd := &cobra.Command{
Use: "collect",
Short: "Collect and process metadata from multiple sources.",
Long: "Collect metadata from multiple sources (cluster, image repo etc.), filter and summarize it into a yaml.",
Run: func(*cobra.Command, []string) { collectHandler(flags) },
}
collectCmd.Flags().StringVarP(&flags.annotations, "annotations", "a", "", "Specify annotations to select collector subset.")
collectCmd.Flags().StringVarP(&flags.outpath, outputFlag, "o", ".", "Specify output directory for collect.")
collectCmd.Flags().StringVarP(&flags.srcpath, sourceFlag, "s", "", "Specify source directory for the artifacts to be considered while collecting.")
return collectCmd
}