Skip to content

Commit 5c9e047

Browse files
committed
Add list command
1 parent 5be699e commit 5c9e047

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

cmd/shelf/actions.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ func CreateShelf(cliCtx *cli.Context) error {
2121
shelfName := cliCtx.Args().First()
2222

2323
if shelfName == "" {
24-
return errors.New("Shelf name has to be given")
24+
return errors.New("shelf name has to be given")
2525
}
2626

2727
shelfPath := path.Join(shelfDir, shelfName)
2828

2929
err = os.Mkdir(shelfPath, 0755)
3030
if err != nil {
3131
if os.IsExist(err) {
32-
return fmt.Errorf("Shelf named: \"%s\" already exist", shelfName)
32+
return fmt.Errorf("shelf named: \"%s\" already exist", shelfName)
3333
}
3434
return err
3535
}
@@ -69,19 +69,19 @@ func TrackFile(cliCtx *cli.Context) error {
6969

7070
shelfName := cliCtx.Args().Get(0)
7171
if shelfName == "" {
72-
return errors.New("Shelf name has to be given")
72+
return errors.New("shelf name has to be given")
7373
}
7474

7575
filePath := cliCtx.Args().Get(1)
7676
if filePath == "" {
77-
return errors.New("File path to track can't be blank")
77+
return errors.New("file path to track can't be blank")
7878
}
7979

8080
// Check if the given shelf exists
8181
_, err = os.Stat(path.Join(shelfDir, shelfName))
8282
if err != nil {
8383
if os.IsNotExist(err) {
84-
return fmt.Errorf("Shelf named: %s doesn't exist", shelfName)
84+
return fmt.Errorf("shelf named: %s doesn't exist", shelfName)
8585
}
8686
return err
8787
}
@@ -165,7 +165,7 @@ func CloneShelf(cliCtx *cli.Context) error {
165165

166166
url := cliCtx.Args().First()
167167
if url == "" {
168-
return errors.New("Git repo url for the shelf has to be provided")
168+
return errors.New("git repo url for the shelf has to be provided")
169169
}
170170

171171
fmt.Printf("[*] Cloning from %s\n", url)
@@ -186,12 +186,12 @@ func SnapshotGitShelf(cliCtx *cli.Context) error {
186186
}
187187
shelfName := cliCtx.Args().First()
188188
if shelfName == "" {
189-
return errors.New("Shelf name can't be empty")
189+
return errors.New("shelf name can't be empty")
190190
}
191191
directory := path.Join(shelfDir, shelfName)
192192
err = createGitSnapshot(directory)
193193
if err != nil {
194-
return fmt.Errorf("Error while creating a snapshot with git: %w", err)
194+
return fmt.Errorf("error while creating a snapshot with git: %w", err)
195195
}
196196
return nil
197197
}
@@ -205,16 +205,16 @@ func SnapshotArchiveShelf(cliCtx *cli.Context) error {
205205
shelfName := cliCtx.Args().First()
206206
outputDir := cliCtx.String("output")
207207
if shelfName == "" {
208-
return errors.New("Shelf name can't be empty")
208+
return errors.New("shelf name can't be empty")
209209
}
210210
if outputDir == "" {
211-
return errors.New("Output path can't be empty")
211+
return errors.New("output path can't be empty")
212212
}
213213
directory := path.Join(shelfDir, shelfName)
214214
outputPath := path.Join(outputDir, fmt.Sprintf("%s.tar.gz", shelfName))
215215
err = createArchiveSnapshot(directory, outputPath)
216216
if err != nil {
217-
return fmt.Errorf("Error while creating a snapshot with archive: %w", err)
217+
return fmt.Errorf("error while creating a snapshot with archive: %w", err)
218218
}
219219
return nil
220220
}
@@ -228,7 +228,7 @@ func RestoreShelf(cliCtx *cli.Context) error {
228228

229229
shelfName := cliCtx.Args().First()
230230
if shelfName == "" {
231-
return errors.New("Shelf name can't be empty")
231+
return errors.New("shelf name can't be empty")
232232
}
233233

234234
shelfPath := path.Join(shelfDir, shelfName)
@@ -237,7 +237,7 @@ func RestoreShelf(cliCtx *cli.Context) error {
237237
_, err = os.Stat(shelfPath)
238238
if err != nil {
239239
if os.IsNotExist(err) {
240-
return fmt.Errorf("Shelf named: %s doesn't exist", shelfName)
240+
return fmt.Errorf("shelf named: %s doesn't exist", shelfName)
241241
}
242242
return err
243243
}
@@ -313,3 +313,27 @@ func WhereShelf(cliCtx *cli.Context) error {
313313
fmt.Println(shelfPath)
314314
return nil
315315
}
316+
317+
func GetListOfFilesInShelf(cliCtx *cli.Context) error {
318+
home, err := GetOrCreateShelvesDir()
319+
if err != nil {
320+
return err
321+
}
322+
shelfName := cliCtx.Args().First()
323+
if shelfName == "" {
324+
return errors.New("shelf name can't be empty")
325+
}
326+
327+
shelfPath := path.Join(home, shelfName)
328+
329+
db, _, err := GetDB(shelfPath)
330+
if err != nil {
331+
return err
332+
}
333+
fmt.Printf("List of files tracked in shelf %s are:\n", shelfName)
334+
links := db.GetLinks()
335+
for _, v := range links {
336+
fmt.Println(v)
337+
}
338+
return nil
339+
}

cmd/shelf/db.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// DB is the db for shelf to store where each symlink is supposed to go.
13-
// This is a JSON File generally stored in
13+
// This is a JSON File generally stored in the shelf directory.
1414
type DB struct {
1515
Name string `json:"name"`
1616
Links map[string]string `json:"links"`
@@ -46,6 +46,10 @@ func (db *DB) AddLink(filePath, linkPath string) {
4646
db.Links[filePath] = path.Clean(linkPath)
4747
}
4848

49+
func (db *DB) GetLinks() map[string]string {
50+
return db.Links
51+
}
52+
4953
// NewDB creates a shelf DB
5054
func NewDB(shelfPath string, shelfName string) (*DB, error) {
5155
db := DB{

cmd/shelf/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ func main() {
7373
Usage: "prints where the given shelf is",
7474
Action: WhereShelf,
7575
},
76+
{
77+
Name: "list",
78+
Aliases: []string{"ls"},
79+
Usage: "lists all the files tracked by shelf",
80+
Action: GetListOfFilesInShelf,
81+
},
7682
},
7783
Name: "shelf",
7884
Description: "A Good Symlinks Manager",

cmd/shelf/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ func createGitSnapshot(dir string) error {
109109
}
110110
_, err = w.Add(".")
111111
if err != nil {
112-
return fmt.Errorf("Error while adding all files: %w", err)
112+
return fmt.Errorf("error while adding all files: %w", err)
113113
}
114114
commitMsg := fmt.Sprintf("snapshot: Automatic commit for snapshot taken at %s", time.Now().Format("Mon Jan _2 15:04:05 2006"))
115115
_, err = w.Commit(commitMsg, &git.CommitOptions{})
116116
if err != nil {
117-
return fmt.Errorf("Error while creating a commit: %s", err.Error())
117+
return fmt.Errorf("error while creating a commit: %s", err.Error())
118118
}
119119
err = r.Push(&git.PushOptions{})
120120
if err != nil {
121-
return fmt.Errorf("Error while pushing the commit: %w", err)
121+
return fmt.Errorf("error while pushing the commit: %w", err)
122122
}
123123
return nil
124124
}
@@ -129,10 +129,10 @@ func createArchiveSnapshot(dir string, output string) error {
129129
// write the .tar.gz
130130
fileToWrite, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR, os.FileMode(0755))
131131
if err != nil {
132-
return fmt.Errorf("Error while creating output file: %w", err)
132+
return fmt.Errorf("error while creating output file: %w", err)
133133
}
134134
if _, err := io.Copy(fileToWrite, &buf); err != nil {
135-
return fmt.Errorf("Error while writing data to output: %w", err)
135+
return fmt.Errorf("error while writing data to output: %w", err)
136136
}
137137
return nil
138138
}

0 commit comments

Comments
 (0)