Skip to content

Commit

Permalink
Merge pull request #7 from waldirborbajr/short-path
Browse files Browse the repository at this point in the history
feat: change full path name to relative from source files
  • Loading branch information
waldirborbajr authored Mar 2, 2024
2 parents 1d39275 + 34d6bcc commit f846014
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions cmds/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"os"
"path/filepath"

cli "github.com/urfave/cli/v2"
"github.com/waldirborbajr/glink/internal/util"
Expand Down Expand Up @@ -40,51 +41,59 @@ func createSymlink() {
linkSourcePath := sourceLinkPath()

// TODO: implement logic to ignore files and directories
ignoreList(linkSourcePath)
// ignoreList(linkSourcePath)

filesToLink := listFilestoLink(linkSourcePath)

for _, file := range filesToLink {

fileName := file.Name()

// ignoring the .glink-ignore file
if file.Name() == ".glink-ignore" || file.Name() == "glink-ignore" {
if file.Name() == ".glink-ignore" || fileName == "glink-ignore" {
continue
}

// If it is a directory, must validate if exists on target
// Existing must enter into and create symlink from the content inside
// Not existing, create directory as symlink
if isDirectory(file.Name()) {
// Check if the file is a directory and has content
if isDirectory(fileName) {
if !hasContent(fileName) {
continue
}

newUserHomeDirectory := userHomeDir + "/" + file.Name()
newUserHomeDirectory := userHomeDir + "/" + fileName

if isTargetExists(newUserHomeDirectory) {

newSourcePath := sourceLinkPath() + "/" + file.Name()
newSourcePath := fileName

pwd, _ := os.Getwd()
filesIntoDiretory := listFilestoLink(pwd + "/" + newSourcePath)

for _, fileIntoDirectory := range filesIntoDiretory {
fileFromDiretory := fileIntoDirectory.Name()

filesFromDiretory := listFilestoLink(newSourcePath)
if !isTargetExists(newUserHomeDirectory + "/" + fileFromDiretory) {

for _, file := range filesFromDiretory {
if !isTargetExists(newUserHomeDirectory + "/" + file.Name()) {
if err := makeSymlink(newSourcePath+"/"+file.Name(), newUserHomeDirectory+"/"+file.Name()); err != nil {
pwd, _ := os.Getwd()
os.Chdir(newUserHomeDirectory)
if err := makeSymlink("../"+filepath.Base(pwd)+"/"+fileName+"/"+fileFromDiretory, fileFromDiretory); err != nil {
util.ExitWithError("Error creating symlink", err)
}
os.Chdir(pwd)
}
}

} else {
if err := makeSymlink(linkSourcePath+"/"+file.Name(), userHomeDir+"/"+file.Name()); err != nil {
if err := makeSymlink(linkSourcePath+"/"+fileName, userHomeDir+"/"+fileName); err != nil {
util.ExitWithError("Error creating symlink", err)
}
}
continue
}

// TODO: implement check is alredy exists symlink. If exists, bypass

if !isTargetExists(userHomeDir + "/" + file.Name()) {
if err := makeSymlink(linkSourcePath+"/"+file.Name(), userHomeDir+"/"+file.Name()); err != nil {
util.ExitWithError("Error creating symlink", err)
} else {
if !isTargetExists(userHomeDir + "/" + fileName) {
if err := makeSymlink(linkSourcePath+"/"+fileName, userHomeDir+"/"+fileName); err != nil {
util.ExitWithError("Error creating symlink", err)
}
}
}
}
Expand All @@ -103,6 +112,16 @@ func isDirectory(path string) bool {
return fileInfo.IsDir()
}

// hasContent determines if a directory has content to be created as a symlink
func hasContent(path string) bool {
contents, err := os.ReadDir(path)
if err != nil {
return false
}

return len(contents) != 0
}

// Validate if diretory exists on target symlink
func isTargetExists(target string) bool {
_, err := os.Stat(target)
Expand All @@ -127,6 +146,8 @@ func sourceLinkPath() string {
util.ExitWithError("Unable to get current directory", err)
}

// linkSourcePath = filepath.Base(linkSourcePath)

return linkSourcePath
}

Expand Down

0 comments on commit f846014

Please sign in to comment.