Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change full path name to relative from source files #7

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
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 @@
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 @@
util.ExitWithError("Unable to get current directory", err)
}

// linkSourcePath = filepath.Base(linkSourcePath)

return linkSourcePath
}

Expand All @@ -142,7 +163,7 @@

// TODO: implement ignore list
// Do not create symlink from .glink-ignore
func ignoreList(sourcePath string) []string {

Check failure on line 166 in cmds/link.go

View workflow job for this annotation

GitHub Actions / lint

func `ignoreList` is unused (unused)

Check failure on line 166 in cmds/link.go

View workflow job for this annotation

GitHub Actions / lint

func `ignoreList` is unused (unused)
_, err := os.Stat(sourcePath + "/.glink-ignore")

if errors.Is(err, os.ErrNotExist) {
Expand Down
Loading