2
0

file.go 720 B

1234567891011121314151617181920212223242526272829303132
  1. package utils
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. )
  7. func GetFileReferenceFromKubeconfig(
  8. filename string,
  9. kubeconfigPath string,
  10. ) (string, error) {
  11. if info, err := os.Stat(filename); os.IsNotExist(err) && !info.IsDir() {
  12. // attempt to discover the filename relative to the kubeconfig location
  13. absPath, err := filepath.Abs(kubeconfigPath)
  14. if err != nil {
  15. return "", err
  16. }
  17. fPath := filepath.Join(filepath.Dir(absPath), filename)
  18. if info, err := os.Stat(fPath); !os.IsNotExist(err) && !info.IsDir() {
  19. return fPath, nil
  20. } else {
  21. return "", fmt.Errorf("%s not found", filename)
  22. }
  23. } else if info.IsDir() {
  24. return "", fmt.Errorf("%s is a directory", filename)
  25. }
  26. return filename, nil
  27. }