Skip to content

Commit

Permalink
refactor: use builtin copy method instead of cp command
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 13, 2024
1 parent f86eb66 commit dcafa30
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Test Files
/internal/constants/assets/RUNTIPI_VERSION
/internal/constants/assets/CLI_VERSION
/.env.local
/.env.local
/cpDest.txt
/cpSrc.txt
4 changes: 3 additions & 1 deletion internal/backups/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"
"path/filepath"
"time"

"github.com/steveiliop56/runtipi-cli-go/internal/system"
)

func CreateBackup() (string, error) {
Expand Down Expand Up @@ -36,7 +38,7 @@ func CreateBackup() (string, error) {
}

// Copy archive to backups folder
_, cpErr := exec.Command("cp", tmpArchivePath, archivePath).Output()
cpErr := system.Copy(tmpArchivePath, archivePath)

if cpErr != nil {
return "", cpErr
Expand Down
2 changes: 1 addition & 1 deletion internal/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func BackupCurrentCLI() (error) {
cliPath := path.Join(rootFolder, "runtipi-cli-go")
cliBackupPath := path.Join(rootFolder, "runtipi-cli-go.bak")

_, copyErr := exec.Command("cp", cliPath, cliBackupPath).Output()
copyErr := system.Copy(cliPath, cliBackupPath)

if copyErr != nil {
return copyErr
Expand Down
39 changes: 39 additions & 0 deletions internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -152,5 +153,43 @@ func EnsureTar() (error) {
return err
}

return nil
}

func Copy(src string, dest string) (error) {
source, sourceErr := os.Open(src)

if sourceErr != nil {
return sourceErr
}

defer source.Close()

sourceStat, sourceStatErr := source.Stat()

if sourceStatErr != nil {
return sourceStatErr
}

destination, destinationErr := os.Create(dest)

if destinationErr != nil {
return destinationErr
}

defer destination.Close()

_, copyErr := io.Copy(destination, source)

if copyErr != nil {
return copyErr
}

chmodErr := os.Chmod(dest, sourceStat.Mode())

if chmodErr != nil {
return chmodErr
}

return nil
}
57 changes: 57 additions & 0 deletions internal/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,61 @@ func TestEnsureFilePermissions(t *testing.T) {
}
}
}
}

// Test copy
func TestCopy(t *testing.T) {
// Get root folder
rootFolder, osErr := os.Getwd()

if osErr != nil {
t.Fatalf("Failed to get root folder, error: %s\n", osErr)
}

// Define paths
srcFilePath := path.Join(rootFolder, "cpSrc.txt")
destFilePath := path.Join(rootFolder, "cpDest.txt")

// Delete old files
os.Remove(srcFilePath)
os.Remove(destFilePath)

// Create src file
writeErr := os.WriteFile(srcFilePath, []byte("test"), 0644)

// Check for errors
if writeErr != nil {
t.Fatalf("Failed to write test file, error: %s\n", writeErr)
}

// Copy file
cpErr := system.Copy(srcFilePath, destFilePath)

// Check for errors
if cpErr != nil {
t.Fatalf("Failed to copy test file, error: %s\n", cpErr)
}

// Check if file got copied
destStatus, statErr := os.Stat(destFilePath)

if statErr != nil {
t.Fatalf("Test file doesn't exist, error: %s\n", statErr)
}

// Check if permissions are the same
if destStatus.Mode() != os.FileMode(0644) {
t.Fatalf("Permissions are not the same! Expected -rw-r--r--, got %s\n", destStatus.Mode().String())
}

// Check contents
contents, readErr := os.ReadFile(destFilePath)

if readErr != nil {
t.Fatalf("Failed to read destination file, error: %s\n", readErr)
}

if string(contents) != "test" {
t.Fatalf("Contents not the same! Expected test got %s\n", string(contents))
}
}

0 comments on commit dcafa30

Please sign in to comment.