...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/paths/paths.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/paths

     1  // Copyright 2022 Google LLC
     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 paths
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"k8s.io/klog/v2"
    23  )
    24  
    25  func getGitRoot() (string, error) {
    26  	dir, err := os.Getwd()
    27  	if err != nil {
    28  		return "", fmt.Errorf("error getting working directory: %w", err)
    29  	}
    30  
    31  	for {
    32  		p := filepath.Join(dir, ".git")
    33  		_, err := os.Stat(p)
    34  		if err != nil {
    35  			if !os.IsNotExist(err) {
    36  				return "", fmt.Errorf("error getting stat of %q: %w", p, err)
    37  			}
    38  		}
    39  		if err == nil {
    40  			return dir, nil
    41  		}
    42  		parent := filepath.Dir(dir)
    43  		if parent == dir {
    44  			return "", fmt.Errorf("unable to locate repo root in working directory %q", dir)
    45  		}
    46  		dir = parent
    47  	}
    48  }
    49  
    50  func GetOperatorSrcRoot() (string, error) {
    51  	gitRoot, err := getGitRoot()
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  	return filepath.Join(gitRoot, "operator"), nil
    56  }
    57  
    58  func GetOperatorCRDsPath() string {
    59  	return filepath.Join(GetOperatorSrcRootOrLogFatal(), "config", "crd", "bases")
    60  }
    61  
    62  func GetOperatorSrcRootOrLogFatal() string {
    63  	root, err := GetOperatorSrcRoot()
    64  	if err != nil {
    65  		klog.Fatal(err)
    66  	}
    67  	return root
    68  }
    69  

View as plain text