Skip to content

Commit

Permalink
Add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
iamd3vil committed May 20, 2021
1 parent 5be699e commit 5c9e047
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
50 changes: 37 additions & 13 deletions cmd/shelf/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func CreateShelf(cliCtx *cli.Context) error {
shelfName := cliCtx.Args().First()

if shelfName == "" {
return errors.New("Shelf name has to be given")
return errors.New("shelf name has to be given")
}

shelfPath := path.Join(shelfDir, shelfName)

err = os.Mkdir(shelfPath, 0755)
if err != nil {
if os.IsExist(err) {
return fmt.Errorf("Shelf named: \"%s\" already exist", shelfName)
return fmt.Errorf("shelf named: \"%s\" already exist", shelfName)
}
return err
}
Expand Down Expand Up @@ -69,19 +69,19 @@ func TrackFile(cliCtx *cli.Context) error {

shelfName := cliCtx.Args().Get(0)
if shelfName == "" {
return errors.New("Shelf name has to be given")
return errors.New("shelf name has to be given")
}

filePath := cliCtx.Args().Get(1)
if filePath == "" {
return errors.New("File path to track can't be blank")
return errors.New("file path to track can't be blank")
}

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

url := cliCtx.Args().First()
if url == "" {
return errors.New("Git repo url for the shelf has to be provided")
return errors.New("git repo url for the shelf has to be provided")
}

fmt.Printf("[*] Cloning from %s\n", url)
Expand All @@ -186,12 +186,12 @@ func SnapshotGitShelf(cliCtx *cli.Context) error {
}
shelfName := cliCtx.Args().First()
if shelfName == "" {
return errors.New("Shelf name can't be empty")
return errors.New("shelf name can't be empty")
}
directory := path.Join(shelfDir, shelfName)
err = createGitSnapshot(directory)
if err != nil {
return fmt.Errorf("Error while creating a snapshot with git: %w", err)
return fmt.Errorf("error while creating a snapshot with git: %w", err)
}
return nil
}
Expand All @@ -205,16 +205,16 @@ func SnapshotArchiveShelf(cliCtx *cli.Context) error {
shelfName := cliCtx.Args().First()
outputDir := cliCtx.String("output")
if shelfName == "" {
return errors.New("Shelf name can't be empty")
return errors.New("shelf name can't be empty")
}
if outputDir == "" {
return errors.New("Output path can't be empty")
return errors.New("output path can't be empty")
}
directory := path.Join(shelfDir, shelfName)
outputPath := path.Join(outputDir, fmt.Sprintf("%s.tar.gz", shelfName))
err = createArchiveSnapshot(directory, outputPath)
if err != nil {
return fmt.Errorf("Error while creating a snapshot with archive: %w", err)
return fmt.Errorf("error while creating a snapshot with archive: %w", err)
}
return nil
}
Expand All @@ -228,7 +228,7 @@ func RestoreShelf(cliCtx *cli.Context) error {

shelfName := cliCtx.Args().First()
if shelfName == "" {
return errors.New("Shelf name can't be empty")
return errors.New("shelf name can't be empty")
}

shelfPath := path.Join(shelfDir, shelfName)
Expand All @@ -237,7 +237,7 @@ func RestoreShelf(cliCtx *cli.Context) error {
_, err = os.Stat(shelfPath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("Shelf named: %s doesn't exist", shelfName)
return fmt.Errorf("shelf named: %s doesn't exist", shelfName)
}
return err
}
Expand Down Expand Up @@ -313,3 +313,27 @@ func WhereShelf(cliCtx *cli.Context) error {
fmt.Println(shelfPath)
return nil
}

func GetListOfFilesInShelf(cliCtx *cli.Context) error {
home, err := GetOrCreateShelvesDir()
if err != nil {
return err
}
shelfName := cliCtx.Args().First()
if shelfName == "" {
return errors.New("shelf name can't be empty")
}

shelfPath := path.Join(home, shelfName)

db, _, err := GetDB(shelfPath)
if err != nil {
return err
}
fmt.Printf("List of files tracked in shelf %s are:\n", shelfName)
links := db.GetLinks()
for _, v := range links {
fmt.Println(v)
}
return nil
}
6 changes: 5 additions & 1 deletion cmd/shelf/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

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

func (db *DB) GetLinks() map[string]string {
return db.Links
}

// NewDB creates a shelf DB
func NewDB(shelfPath string, shelfName string) (*DB, error) {
db := DB{
Expand Down
6 changes: 6 additions & 0 deletions cmd/shelf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func main() {
Usage: "prints where the given shelf is",
Action: WhereShelf,
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "lists all the files tracked by shelf",
Action: GetListOfFilesInShelf,
},
},
Name: "shelf",
Description: "A Good Symlinks Manager",
Expand Down
10 changes: 5 additions & 5 deletions cmd/shelf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ func createGitSnapshot(dir string) error {
}
_, err = w.Add(".")
if err != nil {
return fmt.Errorf("Error while adding all files: %w", err)
return fmt.Errorf("error while adding all files: %w", err)
}
commitMsg := fmt.Sprintf("snapshot: Automatic commit for snapshot taken at %s", time.Now().Format("Mon Jan _2 15:04:05 2006"))
_, err = w.Commit(commitMsg, &git.CommitOptions{})
if err != nil {
return fmt.Errorf("Error while creating a commit: %s", err.Error())
return fmt.Errorf("error while creating a commit: %s", err.Error())
}
err = r.Push(&git.PushOptions{})
if err != nil {
return fmt.Errorf("Error while pushing the commit: %w", err)
return fmt.Errorf("error while pushing the commit: %w", err)
}
return nil
}
Expand All @@ -129,10 +129,10 @@ func createArchiveSnapshot(dir string, output string) error {
// write the .tar.gz
fileToWrite, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR, os.FileMode(0755))
if err != nil {
return fmt.Errorf("Error while creating output file: %w", err)
return fmt.Errorf("error while creating output file: %w", err)
}
if _, err := io.Copy(fileToWrite, &buf); err != nil {
return fmt.Errorf("Error while writing data to output: %w", err)
return fmt.Errorf("error while writing data to output: %w", err)
}
return nil
}

0 comments on commit 5c9e047

Please sign in to comment.