Skip to content

Commit

Permalink
Update Add subcommand logic of handling all different case using a Sw…
Browse files Browse the repository at this point in the history
…itch Statement, Needs Some Improvement

Need to bunch together the Error returning cases
And Handle the Cases where destination path doesn't exist better
  • Loading branch information
SwayKh committed Sep 19, 2024
1 parent 13a41f2 commit 52235a1
Showing 1 changed file with 90 additions and 9 deletions.
99 changes: 90 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,51 @@ func Add(args []string) error {
// }
// }
// }
switch {
case sourceFileExists && destinationFileExists && !sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
return fmt.Errorf("Destination path %s already exists", destinationPath)

if sourceFileExists && sourceFileInfo.IsDir() && destinationFileExists && destinationFileInfo.IsDir() {
case sourceFileExists && destinationFileExists && !sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = false

err = linker.MoveAndLink(sourcePath, destinationPath, isDirectory)
if err != nil {
return err
}
return nil

// If the Destination path doesn't exist, It can't not be a directory or be
// a file, Can't check for a trailing / in the destination path since that
// might be removed by the shell
case sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = false

err = linker.MoveAndLink(sourcePath, destinationPath, isDirectory)
if err != nil {
return err
}
return nil

// Need to handle these !destinationFileExists cases better
case sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = false

err = linker.MoveAndLink(sourcePath, destinationPath, isDirectory)
if err != nil {
return err
}
return nil

case sourceFileExists && destinationFileExists && sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
return fmt.Errorf("Can't link directory: %s to a file: %s", sourcePath, destinationPath)

case sourceFileExists && destinationFileExists && sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = true
Expand All @@ -143,25 +186,63 @@ func Add(args []string) error {
return err
}
return nil
}

if sourceFileExists && sourceFileInfo.IsDir() && destinationFileExists {
filename := filepath.Base(destinationPath)
sourcePath = filepath.Join(sourcePath, filename)
case sourceFileExists && !destinationFileExists && sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = true

err := linker.Link(sourcePath, destinationPath)
err = linker.MoveAndLink(sourcePath, destinationPath, isDirectory)
if err != nil {
return err
}
return nil

case sourceFileExists && !destinationFileExists && sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)
isDirectory = true

err = linker.MoveAndLink(sourcePath, destinationPath, isDirectory)
if err != nil {
return err
}
return nil
}

if destinationFileExists && !sourceFileExists {
err := linker.Link(sourcePath, destinationPath)
case !sourceFileExists && destinationFileExists && !sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
err = linker.Link(sourcePath, destinationPath)
if err != nil {
return err
}
return nil

case !sourceFileExists && destinationFileExists && !sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
return fmt.Errorf("Can't link Source File: %s to a Directory: %s", sourcePath, destinationPath)

case !sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
case !sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
return fmt.Errorf("Source and destinationPath path doesn't exist, Nothing to Link")

case !sourceFileExists && destinationFileExists && sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
return fmt.Errorf("Can't link Source Directory: %s to a File: %s", sourcePath, destinationPath)

case !sourceFileExists && destinationFileExists && sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
filename := filepath.Base(sourcePath)
destinationPath = filepath.Join(destinationPath, filename)

err = linker.Link(sourcePath, destinationPath)
if err != nil {
return err
}
return nil

case !sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && !destinationFileInfo.IsDir():
case !sourceFileExists && !destinationFileExists && !sourceFileInfo.IsDir() && destinationFileInfo.IsDir():
return fmt.Errorf("Source and destinationPath path doesn't exist, Nothing to Link")

default:
return fmt.Errorf("Invalid arguments")

}

default:
Expand Down

0 comments on commit 52235a1

Please sign in to comment.