diff --git a/cmd/add.go b/cmd/add.go index f836170..b2f30d3 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -33,10 +33,10 @@ func newAddCmd() *cobra.Command { basename := filepath.Base(filePath) if host != "" { printf(cmd, "✨ \033[1mAdded %s to lnk (host: %s)\033[0m\n", basename, host) - printf(cmd, " 🔗 \033[90m%s\033[0m → \033[36m~/.config/lnk/%s.lnk/%s\033[0m\n", filePath, host, basename) + printf(cmd, " 🔗 \033[90m%s\033[0m → \033[36m~/.config/lnk/%s.lnk/%s\033[0m\n", filePath, host, filePath) } else { printf(cmd, "✨ \033[1mAdded %s to lnk\033[0m\n", basename) - printf(cmd, " 🔗 \033[90m%s\033[0m → \033[36m~/.config/lnk/%s\033[0m\n", filePath, basename) + printf(cmd, " 🔗 \033[90m%s\033[0m → \033[36m~/.config/lnk/%s\033[0m\n", filePath, filePath) } printf(cmd, " 📝 Use \033[1mlnk push\033[0m to sync to remote\n") return nil diff --git a/cmd/root_test.go b/cmd/root_test.go index 525bef7..a9a63d3 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -5,7 +5,6 @@ import ( "os" "os/exec" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/suite" @@ -107,19 +106,10 @@ func (suite *CLITestSuite) TestAddCommand() { suite.NoError(err) suite.Equal(os.ModeSymlink, info.Mode()&os.ModeSymlink) - // Verify some file exists in repo with .bashrc in the name + // Verify the file exists in repo with preserved directory structure lnkDir := filepath.Join(suite.tempDir, "lnk") - entries, err := os.ReadDir(lnkDir) - suite.NoError(err) - - found := false - for _, entry := range entries { - if strings.Contains(entry.Name(), ".bashrc") && entry.Name() != ".lnk" { - found = true - break - } - } - suite.True(found, "Repository should contain a file with .bashrc in the name") + repoFile := filepath.Join(lnkDir, suite.tempDir, ".bashrc") + suite.FileExists(repoFile) } func (suite *CLITestSuite) TestRemoveCommand() { @@ -397,19 +387,10 @@ func (suite *CLITestSuite) TestAddDirectory() { suite.NoError(err) suite.Equal(os.ModeSymlink, info.Mode()&os.ModeSymlink) - // Verify some directory exists in repo with .config in the name + // Verify the directory exists in repo with preserved directory structure lnkDir := filepath.Join(suite.tempDir, "lnk") - entries, err := os.ReadDir(lnkDir) - suite.NoError(err) - - found := false - for _, entry := range entries { - if strings.Contains(entry.Name(), ".config") && entry.Name() != ".lnk" { - found = true - break - } - } - suite.True(found, "Repository should contain a directory with .config in the name") + repoDir := filepath.Join(lnkDir, suite.tempDir, ".config") + suite.DirExists(repoDir) } func (suite *CLITestSuite) TestSameBasenameFilesBug() { diff --git a/internal/core/lnk.go b/internal/core/lnk.go index f78f4b7..7b2e4ce 100644 --- a/internal/core/lnk.go +++ b/internal/core/lnk.go @@ -67,16 +67,9 @@ func getRepoPath() string { // generateRepoName creates a repository path from a relative path func generateRepoName(relativePath string, host string) string { - if host != "" { - // For host-specific files, preserve the directory structure - return relativePath - } - - // For common files, replace slashes and backslashes with underscores to create valid filename - repoName := strings.ReplaceAll(relativePath, "/", "_") - repoName = strings.ReplaceAll(repoName, "\\", "_") - - return repoName + // Always preserve the directory structure for consistency + // Both common and host-specific files should maintain their path structure + return relativePath } // getHostStoragePath returns the storage path for host-specific or common files diff --git a/internal/core/lnk_test.go b/internal/core/lnk_test.go index 058c12c..1b3e1fa 100644 --- a/internal/core/lnk_test.go +++ b/internal/core/lnk_test.go @@ -82,19 +82,11 @@ func (suite *CoreTestSuite) TestCoreFileOperations() { suite.Require().NoError(err) suite.Equal(os.ModeSymlink, info.Mode()&os.ModeSymlink) - // The repository file will have a generated name based on the relative path + // The repository file will preserve the directory structure lnkDir := filepath.Join(suite.tempDir, "lnk") - entries, err := os.ReadDir(lnkDir) - suite.Require().NoError(err) - var repoFile string - for _, entry := range entries { - if strings.Contains(entry.Name(), ".bashrc") && entry.Name() != ".lnk" { - repoFile = filepath.Join(lnkDir, entry.Name()) - break - } - } - suite.NotEmpty(repoFile, "Repository should contain a file with .bashrc in the name") + // Find the .bashrc file in the repository (it should be at the relative path) + repoFile := filepath.Join(lnkDir, suite.tempDir, ".bashrc") suite.FileExists(repoFile) // Verify content is preserved @@ -141,19 +133,11 @@ func (suite *CoreTestSuite) TestCoreDirectoryOperations() { suite.Require().NoError(err) suite.Equal(os.ModeSymlink, info.Mode()&os.ModeSymlink) - // Check that some repository directory exists with testdir in the name + // Check that the repository directory preserves the structure lnkDir := filepath.Join(suite.tempDir, "lnk") - entries, err := os.ReadDir(lnkDir) - suite.Require().NoError(err) - var repoDir string - for _, entry := range entries { - if strings.Contains(entry.Name(), "testdir") && entry.Name() != ".lnk" { - repoDir = filepath.Join(lnkDir, entry.Name()) - break - } - } - suite.NotEmpty(repoDir, "Repository should contain a directory with testdir in the name") + // The directory should be at the relative path + repoDir := filepath.Join(lnkDir, suite.tempDir, "testdir") suite.DirExists(repoDir) // Remove the directory