mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-24 04:54:00 +00:00
bf8c3bab72
Signed-off-by: Evan Lezar <elezar@nvidia.com>
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
/**
|
|
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
|
#
|
|
# 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 test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// GetModuleRoot returns the path to the root of the go module
|
|
func GetModuleRoot() (string, error) {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
return hasGoMod(filename)
|
|
}
|
|
|
|
// PrependToPath prefixes the specified additional paths to the PATH environment variable
|
|
func PrependToPath(additionalPaths ...string) string {
|
|
paths := strings.Split(os.Getenv("PATH"), ":")
|
|
paths = append(additionalPaths, paths...)
|
|
|
|
return strings.Join(paths, ":")
|
|
}
|
|
|
|
func hasGoMod(dir string) (string, error) {
|
|
if dir == "" || dir == "/" {
|
|
return "", fmt.Errorf("module root not found")
|
|
}
|
|
|
|
_, err := os.Stat(filepath.Join(dir, "go.mod"))
|
|
if err != nil {
|
|
return hasGoMod(filepath.Dir(dir))
|
|
}
|
|
return dir, nil
|
|
}
|