Skip to content

Commit

Permalink
Added unsync command for if the user wanted to unsync
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Jun 15, 2024
1 parent 86ddda3 commit 940bb95
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. `,
Expand Down
22 changes: 22 additions & 0 deletions cmd/unsync.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions internal/command/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
75 changes: 75 additions & 0 deletions internal/command/unsync.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
SYNCALL CommandType = 1
SHOW CommandType = 2
EDIT CommandType = 3
UNSYNC CommandType = 4
)

// colors
Expand All @@ -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"
)

0 comments on commit 940bb95

Please sign in to comment.