...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo/repo.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo

     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 repo
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"os"
    21  	"path/filepath"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  func getGitRoot() (string, error) {
    29  	dir, err := os.Getwd()
    30  	if err != nil {
    31  		return "", fmt.Errorf("error getting working directory: %w", err)
    32  	}
    33  
    34  	for {
    35  		p := filepath.Join(dir, ".git")
    36  		_, err := os.Stat(p)
    37  		if err != nil {
    38  			if !os.IsNotExist(err) {
    39  				return "", fmt.Errorf("error getting stat of %q: %w", p, err)
    40  			}
    41  		}
    42  		if err == nil {
    43  			return dir, nil
    44  		}
    45  		parent := filepath.Dir(dir)
    46  		if parent == dir {
    47  			return "", fmt.Errorf("unable to locate repo root in working directory %q", dir)
    48  		}
    49  		dir = parent
    50  	}
    51  }
    52  
    53  func GetRoot() (string, error) {
    54  	return getGitRoot()
    55  }
    56  
    57  func GetRootOrLogFatal() string {
    58  	root, err := GetRoot()
    59  	if err != nil {
    60  		klog.Fatal(err)
    61  	}
    62  	return root
    63  }
    64  
    65  func GetRootOrTestFatal(t *testing.T) string {
    66  	t.Helper()
    67  	root, err := GetRoot()
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	return root
    72  }
    73  
    74  func GetCallerPackagePath() (string, error) {
    75  	return getCallerPackagePath()
    76  }
    77  
    78  func GetCallerPackagePathOrLogFatal() string {
    79  	path, err := getCallerPackagePath()
    80  	if err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	return path
    84  }
    85  
    86  func GetCallerPackagePathOrTestFatal(t *testing.T) string {
    87  	t.Helper()
    88  	path, err := getCallerPackagePath()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	return path
    93  }
    94  
    95  func getCallerPackagePath() (string, error) {
    96  	_, filePath, _, ok := runtime.Caller(2)
    97  	if !ok {
    98  		return "", fmt.Errorf("unable to get runtimer caller")
    99  	}
   100  	return filepath.Dir(filePath), nil
   101  }
   102  

View as plain text