From 940bb95587f15d975f6a3f649c7e9159d42a2586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erdem=20K=C3=B6=C5=9Fk?= Date: Sun, 16 Jun 2024 01:00:38 +0300 Subject: [PATCH] Added unsync command for if the user wanted to unsync --- cmd/root.go | 2 +- cmd/unsync.go | 22 +++++++++++ internal/command/factory.go | 3 ++ internal/command/unsync.go | 75 +++++++++++++++++++++++++++++++++++++ internal/config.go | 3 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 cmd/unsync.go create mode 100644 internal/command/unsync.go diff --git a/cmd/root.go b/cmd/root.go index 2ae8acd..6a29c20 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,7 +15,7 @@ type Command interface { var rootCmd = &cobra.Command{ Use: "envolve", - Version: "1.0.10", + Version: "1.0.12", Short: "Envolve CLI is a tool for effortless .env file management.", Long: `` + config.PASTEL_ORANGE + `Envolve ` + config.RESET + `is your solution for effortless .env file management. With ` + config.PASTEL_ORANGE + `Envolve ` + config.RESET + `,you can seamlessly gather, arrange, and fine-tune environment variables across all your projects, ensuring that your configuration data is always at your fingertips without the risk of loss. `, diff --git a/cmd/unsync.go b/cmd/unsync.go new file mode 100644 index 0000000..e2bc7f9 --- /dev/null +++ b/cmd/unsync.go @@ -0,0 +1,22 @@ +package cmd + +import ( + config "github.com/erdemkosk/envolve-go/internal" + command "github.com/erdemkosk/envolve-go/internal/command" + "github.com/spf13/cobra" +) + +var unsyncCdm = &cobra.Command{ + Use: "unsync", + Short: `The "unsync" command reverses the synchronization process of environment variables between local and remote projects. It restores the local .env files from the remote location + and removes the synchronization link. Additionally, it deletes the project folder from the remote location if it exists.`, + Long: `The "unsync" command reverses the synchronization process of environment variables between local and remote projects. It restores the local .env files from the remote location + and removes the synchronization link. Additionally, it deletes the project folder from the remote location if it exists.`, + Run: func(cmd *cobra.Command, args []string) { + command.CommandFactory(config.UNSYNC, "").Execute(cmd, args) + }, +} + +func init() { + rootCmd.AddCommand(unsyncCdm) +} diff --git a/internal/command/factory.go b/internal/command/factory.go index 20027ec..2b284ac 100644 --- a/internal/command/factory.go +++ b/internal/command/factory.go @@ -17,6 +17,9 @@ func CommandFactory(commandType config.CommandType, path string) ICommand { if commandType == config.EDIT { return &EditCommand{} } + if commandType == config.SYNCALL { + return &UnsyncCommand{} + } return nil } diff --git a/internal/command/unsync.go b/internal/command/unsync.go new file mode 100644 index 0000000..af1ca3d --- /dev/null +++ b/internal/command/unsync.go @@ -0,0 +1,75 @@ +package command + +import ( + "log" + "os" + "path/filepath" + + "github.com/erdemkosk/envolve-go/internal/logic" + "github.com/spf13/cobra" +) + +type UnsyncCommand struct { +} + +func (command *UnsyncCommand) Execute(cmd *cobra.Command, args []string) { + currentPath, currentFolderName := logic.GetCurrentPathAndFolder("") + envolvePath := logic.GetEnvolveHomePath() + targetPath := filepath.Join(envolvePath, currentFolderName) + currentEnvFilePath := filepath.Join(currentPath, ".env") + targetEnvFilePath := filepath.Join(targetPath, ".env") + + info, err := os.Lstat(currentEnvFilePath) + if err != nil { + log.Printf("Error accessing .env file: %v\n", err) + return + } + + if info.Mode()&os.ModeSymlink == 0 { + log.Println("Error: .env file is not a symlink!") + return + } + + symlinkTarget, err := os.Readlink(currentEnvFilePath) + if err != nil { + log.Printf("Error reading symlink: %v\n", err) + return + } + + if symlinkTarget != targetEnvFilePath { + log.Printf("Error: symlink does not point to the expected target path %s!\n", targetEnvFilePath) + return + } + + tempEnvFilePath := currentEnvFilePath + ".tmp" + copyErr := logic.CopyFile(targetEnvFilePath, tempEnvFilePath) + if copyErr != nil { + log.Printf("Error copying .env file back: %v\n", copyErr) + return + } + + logic.DeleteFile(currentEnvFilePath) + + renameErr := os.Rename(tempEnvFilePath, currentEnvFilePath) + if renameErr != nil { + log.Printf("Error renaming restored .env file: %v\n", renameErr) + return + } + + log.Println("Restore operation completed successfully!") + + projectPathToDelete := filepath.Join(envolvePath, currentFolderName) + if _, err := os.Stat(projectPathToDelete); err == nil { + log.Printf("Deleting project folder: %s\n", projectPathToDelete) + err := os.RemoveAll(projectPathToDelete) + if err != nil { + log.Printf("Error deleting project folder: %v\n", err) + return + } + log.Printf("Project folder deleted: %s\n", projectPathToDelete) + } else { + log.Printf("Project folder %s does not exist in Envolve path.\n", currentFolderName) + } + + os.Exit(0) +} diff --git a/internal/config.go b/internal/config.go index 3d8a882..6ae66d4 100644 --- a/internal/config.go +++ b/internal/config.go @@ -14,6 +14,7 @@ const ( SYNCALL CommandType = 1 SHOW CommandType = 2 EDIT CommandType = 3 + UNSYNC CommandType = 4 ) // colors @@ -34,5 +35,5 @@ const ( PASTEL_WHITE = "\033[97m" PASTEL_GRAY = "\033[37m" PASTEL_PURPLE = "\033[35m" - PASTEL_ORANGE = "\033[38;5;214m" // Bir pastel turuncu tonu ekledim + PASTEL_ORANGE = "\033[38;5;214m" )