This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
main.go
100 lines (85 loc) · 2.86 KB
/
main.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
// Copyright 2017 Istio Authors
//
// 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 (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
"istio.io/istio/mixer/tools/codegen/pkg/inventory"
)
func withArgs(args []string, errorf func(format string, a ...interface{})) {
var mappings []string
var output string
var mappingFile string
rootCmd := cobra.Command{
Use: "mixgeninventory",
Short: "Generates mixer adapter inventory source code",
Long: "Generates mixer adapter inventory source code from an input map of adapter packages to include in the inventory.\n" +
"Example: mixgeninventory -p prometheus:istio.io/istio/mixer/adapter/prometheus -p stdio:istio.io/istio/mixer/adapter/stdio",
Run: func(cmd *cobra.Command, args []string) {
packageMap := make(map[string]string)
if mappingFile != "" {
yamlFile, err := ioutil.ReadFile(mappingFile)
if err != nil {
errorf("could not read mapping file: %v", err)
return
}
err = yaml.Unmarshal(yamlFile, &packageMap)
if err != nil {
errorf("could not unmarshal mapping file: %v", err)
return
}
} else {
for _, maps := range mappings {
m := strings.Split(maps, ":")
if len(m) != 2 {
errorf("Invalid flag -p %v", mappings)
}
packageMap[strings.TrimSpace(m[0])] = strings.TrimSpace(m[1])
}
}
out := os.Stdout
if len(output) > 0 {
file, err := os.Create(output)
if err != nil {
errorf("could not create output file '%s': %v", output, err)
return
}
out = file
}
if err := inventory.Generate(packageMap, out); err != nil {
errorf("%v", err)
}
},
}
rootCmd.SetArgs(args)
rootCmd.PersistentFlags().StringArrayVarP(&mappings, "packages", "p", []string{},
"colon-separated mapping of Go packages to their full import paths. Example: -p prometheus:istio.io/istio/mixer/adapter/prometheus")
rootCmd.PersistentFlags().StringVarP(&mappingFile, "file", "f", "",
"Path to a YAML file that maps Go package names to their full import paths.")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "name of file to generate")
if err := rootCmd.Execute(); err != nil {
errorf("%v", err)
}
}
func main() {
withArgs(os.Args[1:],
func(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", a...) // nolint: gas
os.Exit(1)
})
}