Skip to content

Commit

Permalink
refactoring add command (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 18, 2023
1 parent 4d0cfc7 commit 226b05f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

"github.com/JunNishimura/Goit/internal/file"
"github.com/JunNishimura/Goit/internal/object"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)

func add(path string) error {
func add(rootGoitPath, path string, index *store.Index) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("%w: %s", ErrIOHandling, path)
Expand All @@ -40,7 +41,7 @@ func add(path string) error {
byteRelPath := []byte(cleanedRelPath)

// update index
isUpdated, err := client.Idx.Update(client.RootGoitPath, object.Hash, byteRelPath)
isUpdated, err := index.Update(rootGoitPath, object.Hash, byteRelPath)
if err != nil {
return fmt.Errorf("fail to update index: %w", err)
}
Expand All @@ -49,7 +50,7 @@ func add(path string) error {
}

// write object to file
if err := object.Write(client.RootGoitPath); err != nil {
if err := object.Write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write object: %w", err)
}

Expand Down Expand Up @@ -117,12 +118,12 @@ var addCmd = &cobra.Command{
return fmt.Errorf("fail to get file path under directory: %w", err)
}
for _, filePath := range filePaths {
if err := add(filePath); err != nil {
if err := add(client.RootGoitPath, filePath, client.Idx); err != nil {
return err
}
}
} else {
if err := add(path); err != nil {
if err := add(client.RootGoitPath, path, client.Idx); err != nil {
return err
}
}
Expand Down

0 comments on commit 226b05f

Please sign in to comment.