From 75cac910a8afbd3c622d20a51cc54a467ca8c241 Mon Sep 17 00:00:00 2001 From: SwayKh Date: Mon, 9 Sep 2024 22:03:06 +0530 Subject: [PATCH] Move main.go to top level directory for now --- cmd/main.go | 84 ----------------------------------------------------- main.go | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 84 deletions(-) delete mode 100644 cmd/main.go create mode 100644 main.go diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index fefda97..0000000 --- a/cmd/main.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/SwayKh/linksym/pkg/linker" -) - -var ( - homeDirectory string - configDirectory string - currentWorkingDirectory string -) - -// var ( -// add = flag.Bool("add", false, "add a symlink to given path") -// remove = flag.Bool("remove", false, "Remove a symlink") -// help = flag.Bool("--help", false, "Print this help message") -// h = flag.Bool("-h", false, "Print this help message") -// ) - -var usage string = ` -Usage: linksym [option...] [path...] - -Options: - add add a symlink to given path - remove Remove a symlink - -h Print this help message -` - -func setupDirectories() error { - var err error - homeDirectory, err = os.UserHomeDir() - if err != nil { - return errors.New("Couldn't get the home directory") - } - - configDirectory, err = os.UserConfigDir() - if err != nil { - return errors.New("Couldn't get the config directory") - } - - currentWorkingDirectory, err = os.Getwd() - if err != nil { - return errors.New("Couldn't get the current working directory") - } - - return nil -} - -func main() { - // Get Home, config and current directory - err := setupDirectories() - if err != nil { - fmt.Println(err) - } - - if len(os.Args) < 2 { - fmt.Println(usage) - os.Exit(1) - } - - path := os.Args[1] - filename := filepath.Base(path) - newFilePath := currentWorkingDirectory + "/" + filename - - err = linker.Link(path, newFilePath) - if err != nil { - fmt.Println(err) - } -} - -// Create a init function, that create the config files, stores the working -// directory, and other stuff, every other command needs to check if the config -// file exists, before it works. -// The config package will be separates, that adds and reads config, the init -// function should probably call that package - -func initialise() error { - return nil -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..99783f5 --- /dev/null +++ b/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/SwayKh/linksym/pkg/config" + "github.com/SwayKh/linksym/pkg/linker" +) + +// var ( +// add = flag.Bool("add", false, "add a symlink to given path") +// remove = flag.Bool("remove", false, "Remove a symlink") +// help = flag.Bool("--help", false, "Print this help message") +// h = flag.Bool("-h", false, "Print this help message") +// ) + +var usage string = ` +Usage: linksym [option...] [path...] + +Options: + add add a symlink to given path + remove Remove a symlink + -h Print this help message +` + +func main() { + if len(os.Args) < 2 { + fmt.Println(usage) + os.Exit(1) + } + + configPath, err := config.Initialise() + if err != nil { + // fmt.Errorf("Error initialising config: %s", err) + fmt.Println(err) + } + + cfg, err := config.LoadConfig("./.linksym.yaml") + if err != nil { + // fmt.Errorf("Error loading config: %s", err) + fmt.Println(err) + } + + sourcePath := os.Args[1] + filename := filepath.Base(sourcePath) + // destinationPath := os.Args[2] + destinationPath := cfg.InitDirectory + "/" + filename + + fmt.Println(sourcePath, filename, destinationPath) + + err = linker.Link(sourcePath, destinationPath, configPath) + if err != nil { + fmt.Println(err) + } +}