Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
34 lines
1 KiB
Go
34 lines
1 KiB
Go
package xos
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/ignite/cli/v29/ignite/pkg/errors"
|
|
)
|
|
|
|
// Rename copy oldPath to newPath and then delete oldPath.
|
|
// Unlike os.Rename, it doesn't fail when the oldPath and newPath are in
|
|
// different partitions (error: invalid cross-device link).
|
|
func Rename(oldPath, newPath string) error {
|
|
inputFile, err := os.Open(oldPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: couldn't open oldpath: %w", oldPath, newPath, err)
|
|
}
|
|
defer inputFile.Close()
|
|
outputFile, err := os.Create(newPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: couldn't open dest file: %w", oldPath, newPath, err)
|
|
}
|
|
defer outputFile.Close()
|
|
_, err = io.Copy(outputFile, inputFile)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: writing to output file failed: %w", oldPath, newPath, err)
|
|
}
|
|
// The copy was successful, so now delete the original file
|
|
err = os.Remove(oldPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: failed removing original file: %w", oldPath, newPath, err)
|
|
}
|
|
return nil
|
|
}
|