Skip to content

Commit

Permalink
code reusability among bundle and bundleExtension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas authored and sophiewigmore committed Jun 6, 2023
1 parent 091c2dc commit 9073a20
Showing 1 changed file with 41 additions and 46 deletions.
87 changes: 41 additions & 46 deletions internal/file_bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ func NewFileBundler() FileBundler {
return FileBundler{}
}

func (b FileBundler) bundling(root string, path string) (File, error) {

file := File{Name: path}

var err error
file.Info, err = os.Lstat(filepath.Join(root, path))
if err != nil {
return file, fmt.Errorf("error stating included file: %s", err)
}

if file.Info.Mode()&os.ModeType != 0 {
link, err := os.Readlink(filepath.Join(root, path))
if err != nil {
return file, fmt.Errorf("error readlinking included file: %s", err)
}

if !strings.HasPrefix(link, string(filepath.Separator)) {
link = filepath.Clean(filepath.Join(root, link))
}

file.Link, err = filepath.Rel(root, link)
if err != nil {
return file, fmt.Errorf("error finding relative link path: %s", err)
}
} else {
file.ReadCloser, err = os.Open(filepath.Join(root, path))
if err != nil {
return file, fmt.Errorf("error opening included file: %s", err)
}
}

return file, nil
}

func (b FileBundler) Bundle(root string, paths []string, config cargo.Config) ([]File, error) {
var files []File

Expand All @@ -85,34 +119,15 @@ func (b FileBundler) Bundle(root string, paths []string, config cargo.Config) ([

default:
var err error
file.Info, err = os.Lstat(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error stating included file: %s", err)
}
file, err = b.bundling(root, path)

if file.Info.Mode()&os.ModeType != 0 {
link, err := os.Readlink(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error readlinking included file: %s", err)
}

if !strings.HasPrefix(link, string(filepath.Separator)) {
link = filepath.Clean(filepath.Join(root, link))
}

file.Link, err = filepath.Rel(root, link)
if err != nil {
return nil, fmt.Errorf("error finding relative link path: %s", err)
}
} else {
file.ReadCloser, err = os.Open(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error opening included file: %s", err)
}
if err != nil {
return nil, fmt.Errorf("%s", err)
}
}

files = append(files, file)

}

return files, nil
Expand All @@ -137,30 +152,10 @@ func (b FileBundler) BundleExtension(root string, paths []string, config cargo.E

default:
var err error
file.Info, err = os.Lstat(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error stating included file: %s", err)
}
file, err = b.bundling(root, path)

if file.Info.Mode()&os.ModeType != 0 {
link, err := os.Readlink(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error readlinking included file: %s", err)
}

if !strings.HasPrefix(link, string(filepath.Separator)) {
link = filepath.Clean(filepath.Join(root, link))
}

file.Link, err = filepath.Rel(root, link)
if err != nil {
return nil, fmt.Errorf("error finding relative link path: %s", err)
}
} else {
file.ReadCloser, err = os.Open(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error opening included file: %s", err)
}
if err != nil {
return nil, fmt.Errorf("%s", err)
}
}

Expand Down

0 comments on commit 9073a20

Please sign in to comment.