Skip to content

Commit

Permalink
Merge pull request #6 from waldirborbajr/is-symlink
Browse files Browse the repository at this point in the history
feat: validation the verify if previously exists target
  • Loading branch information
waldirborbajr authored Feb 29, 2024
2 parents e31c8c5 + 7db4d12 commit 1d39275
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions cmds/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ func Link() *cli.Command {
}
}

// Create the Symlink for file or directory
func createSymlink() {
userHomeDir := userHomeDir()

linkSourcePath := sourceLinkPath()

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

filesToLink := listFilestoLink(linkSourcePath)

for _, file := range filesToLink {
fmt.Println(file)

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

Expand All @@ -64,29 +65,36 @@ func createSymlink() {
filesFromDiretory := listFilestoLink(newSourcePath)

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

} else {

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

err := os.Symlink(linkSourcePath+"/"+file.Name(), userHomeDir+"/"+file.Name())
if err != nil {
util.ExitWithError("Error creating symlink", err)
// 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)
}
}
}
}

// makeSymlink creates a symlink and return error
func makeSymlink(source string, target string) error {
return os.Symlink(source, target)
}

// isDirectory determines if a file represented
// by `path` is a directory or not
func isDirectory(path string) bool {
Expand Down Expand Up @@ -132,8 +140,8 @@ func listFilestoLink(sourcePath string) []fs.DirEntry {
return files
}

// TODO: implement ignore list
// Do not create symlink from .glink-ignore
// TODO implement ignore list
func ignoreList(sourcePath string) []string {
_, err := os.Stat(sourcePath + "/.glink-ignore")

Expand Down Expand Up @@ -163,6 +171,5 @@ func ignoreList(sourcePath string) []string {
_ = line // GET the line string
}

// TODO implement ignore list
return make([]string, 0)
}

0 comments on commit 1d39275

Please sign in to comment.