forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genutil.go
141 lines (127 loc) · 3.68 KB
/
genutil.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
// Copyright 2018 The Operator-SDK 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 genutil
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
flags "github.com/operator-framework/operator-sdk/internal/pkg/flags"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func buildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
for _, gd := range genDirs {
err := runGoBuildCodegen(binDir, codegenSrcDir, gd)
if err != nil {
return err
}
}
return nil
}
func runGoBuildCodegen(binDir, repoDir, genDir string) error {
binPath := filepath.Join(binDir, filepath.Base(genDir))
cmd := exec.Command("go", "build", "-o", binPath, genDir)
cmd.Dir = repoDir
if gf, ok := os.LookupEnv(projutil.GoFlagsEnv); ok && len(gf) != 0 {
cmd.Env = append(os.Environ(), projutil.GoFlagsEnv+"="+gf)
}
// Only print binary build info if verbosity is explicitly set.
if viper.GetBool(flags.VerboseOpt) {
return projutil.ExecCmd(cmd)
}
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
return cmd.Run()
}
// ParseGroupVersions parses the layout of pkg/apis to return a map of
// API groups to versions.
func parseGroupVersions() (map[string][]string, error) {
gvs := make(map[string][]string)
groups, err := ioutil.ReadDir(scaffold.ApisDir)
if err != nil {
return nil, fmt.Errorf("could not read pkg/apis directory to find api Versions: %v", err)
}
for _, g := range groups {
if g.IsDir() {
groupDir := filepath.Join(scaffold.ApisDir, g.Name())
versions, err := ioutil.ReadDir(groupDir)
if err != nil {
return nil, fmt.Errorf("could not read %s directory to find api Versions: %v", groupDir, err)
}
gvs[g.Name()] = make([]string, 0)
for _, v := range versions {
if v.IsDir() && scaffold.ResourceVersionRegexp.MatchString(v.Name()) {
gvs[g.Name()] = append(gvs[g.Name()], v.Name())
}
}
}
}
if len(gvs) == 0 {
return nil, fmt.Errorf("no groups or versions found in %s", scaffold.ApisDir)
}
return gvs, nil
}
// CreateFQApis return a string of all fully qualified pkg + groups + versions
// of pkg and gvs in the format:
// "pkg/groupA/v1,pkg/groupA/v2,pkg/groupB/v1"
func createFQApis(pkg string, gvs map[string][]string) string {
gn := 0
fqb := &strings.Builder{}
for g, vs := range gvs {
for vn, v := range vs {
fqb.WriteString(filepath.Join(pkg, g, v))
if vn < len(vs)-1 {
fqb.WriteString(",")
}
}
if gn < len(gvs)-1 {
fqb.WriteString(",")
}
gn++
}
return fqb.String()
}
func withHeaderFile(f func(string) error) (err error) {
i, err := (&scaffold.Boilerplate{}).GetInput()
if err != nil {
return err
}
hf := i.Path
if _, err := os.Stat(hf); os.IsNotExist(err) {
if hf, err = createEmptyTmpFile(); err != nil {
return err
}
defer func() {
if err = os.RemoveAll(hf); err != nil {
log.Error(err)
}
}()
}
return f(hf)
}
func createEmptyTmpFile() (string, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
if err = f.Close(); err != nil {
return "", err
}
return f.Name(), nil
}