@@ -177,3 +177,60 @@ func CloneShelf(cliCtx *cli.Context) error {
177
177
}
178
178
return nil
179
179
}
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
+ }
0 commit comments