From 8ce5e268b9933ab9b7698dd826314e4d468f287c Mon Sep 17 00:00:00 2001 From: SwayKh Date: Mon, 16 Sep 2024 22:07:18 +0530 Subject: [PATCH] Update Remove Command prototype to handle case of multiple arguments provided --- cmd/commands.go | 70 +++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/cmd/commands.go b/cmd/commands.go index de65f6f..3c1114e 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -108,47 +108,49 @@ func Add(args []string) error { return nil } -func Remove(linkName string) error { - var linkPath string - var sourcePath, destinationPath string - var err error - var isDirectory bool - - configuration, err := config.LoadConfig(config.ConfigPath) - if err != nil { - return err - } +func Remove(args []string) error { + switch len(args) { + case 1: + linkName := args[0] + var linkPath string + var sourcePath, destinationPath string + var err error + var isDirectory bool - linkPath, err = filepath.Abs(linkName) - if err != nil { - return fmt.Errorf("Error getting absolute path of file %s: \n%w", linkPath, err) - } + linkPath, err = filepath.Abs(linkName) + if err != nil { + return fmt.Errorf("Error getting absolute path of file %s: \n%w", linkPath, err) + } - fileExists, fileInfo, err := config.CheckFile(linkPath) - if err != nil { - return err - } else if fileInfo.IsDir() { - isDirectory = true - } else if !fileExists { - return fmt.Errorf("File %s doesn't exist", linkPath) - } + fileExists, fileInfo, err := config.CheckFile(linkPath) + if err != nil { + return err + } else if fileInfo.IsDir() { + isDirectory = true + } else if !fileExists { + return fmt.Errorf("File %s doesn't exist", linkPath) + } - recordPathName := filepath.Join(filepath.Base(filepath.Dir(linkPath)), filepath.Base(linkPath)) + recordPathName := filepath.Join(filepath.Base(filepath.Dir(linkPath)), filepath.Base(linkPath)) - for i := range configuration.Records { - if configuration.Records[i].Name == recordPathName { - sourcePath = configuration.Records[i].Paths[0] - destinationPath = configuration.Records[i].Paths[1] - err = config.RemoveRecord(i) - if err != nil { - return nil + for i := range config.Configuration.Records { + if config.Configuration.Records[i].Name == recordPathName { + sourcePath = config.Configuration.Records[i].Paths[0] + destinationPath = config.Configuration.Records[i].Paths[1] + err = config.RemoveRecord(i) + if err != nil { + return nil + } } } - } - err = linker.UnLink(sourcePath, destinationPath, isDirectory) - if err != nil { - return nil + err = linker.UnLink(sourcePath, destinationPath, isDirectory) + if err != nil { + return nil + } + + default: + return fmt.Errorf("Invalid number of arguments") } return nil }