Skip to content

Commit c163636

Browse files
committed
Add restore shelf command
1 parent cdc93c0 commit c163636

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

cmd/shelf/actions.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,60 @@ func CloneShelf(cliCtx *cli.Context) error {
177177
}
178178
return nil
179179
}
180+
181+
// RestoreShelf restores all the symlinks from the given shelf
182+
func RestoreShelf(cliCtx *cli.Context) error {
183+
home, err := GetHomeDirectory()
184+
if err != nil {
185+
return err
186+
}
187+
188+
shelfName := cliCtx.Args().First()
189+
if shelfName == "" {
190+
return errors.New("Shelf name can't be empty")
191+
}
192+
193+
shelfPath := path.Join(home, shelfName)
194+
195+
// Check if the given shelf exists
196+
_, err = os.Stat(shelfPath)
197+
if err != nil {
198+
if os.IsNotExist(err) {
199+
return fmt.Errorf("Shelf named: %s doesn't exist", shelfName)
200+
}
201+
return err
202+
}
203+
204+
// Read the db
205+
db, _, err := GetDB(shelfPath)
206+
if err != nil {
207+
return err
208+
}
209+
210+
// Loop over each link and put a symlink
211+
for fName, lPath := range db.Links {
212+
// Check if there is a file
213+
// If there is no file with the file name in the shelf, skip over it
214+
_, err := os.Stat(path.Join(shelfPath, fName))
215+
if err != nil {
216+
if os.IsNotExist(err) {
217+
fmt.Printf("[*] Warning: File missing in the shelf: %s. Skipping...\n", fName)
218+
continue
219+
}
220+
return err
221+
}
222+
223+
err = os.Symlink(path.Join(shelfPath, fName), lPath)
224+
if err != nil {
225+
if os.IsExist(err) {
226+
fmt.Printf("[*] Warning: There is a already a file at %s. Skipping restoring: %s\n", lPath, fName)
227+
continue
228+
}
229+
return err
230+
}
231+
}
232+
233+
fmt.Printf("[*] Restored %s shelf\n", shelfName)
234+
235+
return nil
236+
}

cmd/shelf/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ func main() {
3131
Description: "Clones a shelf from a git clone url",
3232
Action: CloneShelf,
3333
},
34+
{
35+
Name: "restore",
36+
Aliases: []string{"r"},
37+
Usage: "restores all the links from a shelf",
38+
ArgsUsage: "[shelfname]",
39+
Description: "Restores all the symlinks from the given shelf",
40+
Action: RestoreShelf,
41+
},
3442
},
3543
Name: "shelf",
3644
Description: "A Good Symlinks Manager",

0 commit comments

Comments
 (0)