github.com/jmrodri/operator-sdk@v0.5.0/commands/operator-sdk/cmd/generate/internal/genutil.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package genutil
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"os/exec"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/operator-framework/operator-sdk/internal/util/projutil"
    26  	"github.com/operator-framework/operator-sdk/pkg/scaffold"
    27  )
    28  
    29  func BuildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
    30  	for _, gd := range genDirs {
    31  		err := runGoBuildCodegen(binDir, codegenSrcDir, gd)
    32  		if err != nil {
    33  			return err
    34  		}
    35  	}
    36  	return nil
    37  }
    38  
    39  func runGoBuildCodegen(binDir, repoDir, genDir string) error {
    40  	binPath := filepath.Join(binDir, filepath.Base(genDir))
    41  	cmd := exec.Command("go", "build", "-o", binPath, genDir)
    42  	cmd.Dir = repoDir
    43  	if gf, ok := os.LookupEnv(projutil.GoFlagsEnv); ok && len(gf) != 0 {
    44  		cmd.Env = append(os.Environ(), projutil.GoFlagsEnv+"="+gf)
    45  	}
    46  
    47  	if projutil.IsGoVerbose() {
    48  		return projutil.ExecCmd(cmd)
    49  	}
    50  	cmd.Stdout = ioutil.Discard
    51  	cmd.Stderr = ioutil.Discard
    52  	return cmd.Run()
    53  }
    54  
    55  // ParseGroupVersions parses the layout of pkg/apis to return a map of
    56  // API groups to versions.
    57  func ParseGroupVersions() (map[string][]string, error) {
    58  	gvs := make(map[string][]string)
    59  	groups, err := ioutil.ReadDir(scaffold.ApisDir)
    60  	if err != nil {
    61  		return nil, fmt.Errorf("could not read pkg/apis directory to find api Versions: %v", err)
    62  	}
    63  
    64  	for _, g := range groups {
    65  		if g.IsDir() {
    66  			groupDir := filepath.Join(scaffold.ApisDir, g.Name())
    67  			versions, err := ioutil.ReadDir(groupDir)
    68  			if err != nil {
    69  				return nil, fmt.Errorf("could not read %s directory to find api Versions: %v", groupDir, err)
    70  			}
    71  
    72  			gvs[g.Name()] = make([]string, 0)
    73  			for _, v := range versions {
    74  				if v.IsDir() && scaffold.ResourceVersionRegexp.MatchString(v.Name()) {
    75  					gvs[g.Name()] = append(gvs[g.Name()], v.Name())
    76  				}
    77  			}
    78  		}
    79  	}
    80  
    81  	if len(gvs) == 0 {
    82  		return nil, fmt.Errorf("no groups or versions found in %s", scaffold.ApisDir)
    83  	}
    84  	return gvs, nil
    85  }
    86  
    87  // CreateFQApis return a string of all fully qualified pkg + groups + versions
    88  // of pkg and gvs in the format:
    89  // "pkg/groupA/v1,pkg/groupA/v2,pkg/groupB/v1"
    90  func CreateFQApis(pkg string, gvs map[string][]string) string {
    91  	gn := 0
    92  	fqb := &strings.Builder{}
    93  	for g, vs := range gvs {
    94  		for vn, v := range vs {
    95  			fqb.WriteString(filepath.Join(pkg, g, v))
    96  			if vn < len(vs)-1 {
    97  				fqb.WriteString(",")
    98  			}
    99  		}
   100  		if gn < len(gvs)-1 {
   101  			fqb.WriteString(",")
   102  		}
   103  		gn++
   104  	}
   105  	return fqb.String()
   106  }