Skip to content

Commit

Permalink
Merge pull request #3 from iamd3vil/relative-path-for-home
Browse files Browse the repository at this point in the history
feat: For files in the home directory, only store relative path in db
  • Loading branch information
iamd3vil authored Jun 2, 2020
2 parents f164973 + f604728 commit 5be699e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
12 changes: 12 additions & 0 deletions cmd/shelf/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ func RestoreShelf(cliCtx *cli.Context) error {
return err
}

// If the symlink path in db is absolute, it will be put in home directory
if !path.IsAbs(lPath) {
fmt.Printf("lPath is abs: ")
home := getHomeDir()
lPath = path.Join(home, lPath)
}

err = os.MkdirAll(path.Dir(lPath), 0755)
if err != nil {
return err
}

err = os.Symlink(path.Join(shelfPath, fName), lPath)
if err != nil {
if os.IsExist(err) {
Expand Down
18 changes: 17 additions & 1 deletion cmd/shelf/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
)

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

// AddLink adds the file and link paths to the DB
func (db *DB) AddLink(filePath, linkPath string) {
db.Links[filePath] = linkPath
// If the linkpath is in the home directory, only put absolute path from home
home := getHomeDir()
if strings.HasPrefix(linkPath, home) {

// Strip home directory
l := strings.TrimPrefix(linkPath, home)

// Strip any extra slashes at the prefix
l = strings.TrimPrefix(l, "/")

fmt.Printf("linkpath: %s, home: %s\n", linkPath, home)
db.Links[filePath] = path.Clean(l)
return
}
db.Links[filePath] = path.Clean(linkPath)
}

// NewDB creates a shelf DB
Expand Down
7 changes: 6 additions & 1 deletion cmd/shelf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func GetOrCreateShelvesDir() (string, error) {
}

func getShelfDirPath() string {
return path.Join(getHomeDir(), shelvesDIR)
}

func getHomeDir() string {
var (
home string
)
Expand All @@ -47,7 +51,8 @@ func getShelfDirPath() string {
home = os.Getenv("HOME")
}
}
return path.Join(home, shelvesDIR)

return path.Clean(home)
}

// SOURCE: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726
Expand Down

0 comments on commit 5be699e

Please sign in to comment.