-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unsync command for if the user wanted to unsync
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters