Skip to content

Commit 5be699e

Browse files
authored
Merge pull request #3 from iamd3vil/relative-path-for-home
feat: For files in the home directory, only store relative path in db
2 parents f164973 + f604728 commit 5be699e

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

cmd/shelf/actions.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ func RestoreShelf(cliCtx *cli.Context) error {
261261
return err
262262
}
263263

264+
// If the symlink path in db is absolute, it will be put in home directory
265+
if !path.IsAbs(lPath) {
266+
fmt.Printf("lPath is abs: ")
267+
home := getHomeDir()
268+
lPath = path.Join(home, lPath)
269+
}
270+
271+
err = os.MkdirAll(path.Dir(lPath), 0755)
272+
if err != nil {
273+
return err
274+
}
275+
264276
err = os.Symlink(path.Join(shelfPath, fName), lPath)
265277
if err != nil {
266278
if os.IsExist(err) {

cmd/shelf/db.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
67
"os"
78
"path"
9+
"strings"
810
)
911

1012
// DB is the db for shelf to store where each symlink is supposed to go.
@@ -27,7 +29,21 @@ func (db *DB) Marshal(w io.Writer) error {
2729

2830
// AddLink adds the file and link paths to the DB
2931
func (db *DB) AddLink(filePath, linkPath string) {
30-
db.Links[filePath] = linkPath
32+
// If the linkpath is in the home directory, only put absolute path from home
33+
home := getHomeDir()
34+
if strings.HasPrefix(linkPath, home) {
35+
36+
// Strip home directory
37+
l := strings.TrimPrefix(linkPath, home)
38+
39+
// Strip any extra slashes at the prefix
40+
l = strings.TrimPrefix(l, "/")
41+
42+
fmt.Printf("linkpath: %s, home: %s\n", linkPath, home)
43+
db.Links[filePath] = path.Clean(l)
44+
return
45+
}
46+
db.Links[filePath] = path.Clean(linkPath)
3147
}
3248

3349
// NewDB creates a shelf DB

cmd/shelf/utils.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func GetOrCreateShelvesDir() (string, error) {
3838
}
3939

4040
func getShelfDirPath() string {
41+
return path.Join(getHomeDir(), shelvesDIR)
42+
}
43+
44+
func getHomeDir() string {
4145
var (
4246
home string
4347
)
@@ -47,7 +51,8 @@ func getShelfDirPath() string {
4751
home = os.Getenv("HOME")
4852
}
4953
}
50-
return path.Join(home, shelvesDIR)
54+
55+
return path.Clean(home)
5156
}
5257

5358
// SOURCE: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726

0 commit comments

Comments
 (0)