mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-22 07:28:06 +00:00
84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
package system
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Run executes the provided command within this context
|
|
func Run(name string, args ...string) (string, error) {
|
|
cmd := exec.Command(name, args...)
|
|
|
|
rootDir, err := GetProjectRoot()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Command will run from project root
|
|
cmd.Dir = rootDir
|
|
|
|
command := strings.Join(cmd.Args, " ")
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return string(output), fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
|
|
}
|
|
|
|
return string(output), nil
|
|
}
|
|
|
|
func GetProjectRoot() (string, error) {
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for {
|
|
// check if go.mod exists in current dir
|
|
modFile := filepath.Join(dir, "go.mod")
|
|
if _, err := os.Stat(modFile); err == nil {
|
|
return dir, nil
|
|
}
|
|
|
|
// move one level up
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
// reached filesystem root
|
|
return "", fmt.Errorf("project root not found (no go.mod)")
|
|
}
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
func ReplaceFile(src, dst string) error {
|
|
rootDir, err := GetProjectRoot()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Open the source file
|
|
sourceFile, err := os.Open(filepath.Join(rootDir, src))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer sourceFile.Close()
|
|
|
|
// Create (or overwrite) the destination file
|
|
destFile, err := os.Create(filepath.Join(rootDir, dst))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer destFile.Close()
|
|
|
|
// Copy contents
|
|
if _, err = io.Copy(destFile, sourceFile); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Ensure data is written to disk
|
|
return destFile.Sync()
|
|
}
|