Skip to content

Commit

Permalink
refactor: Improve File methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom5521 committed Jun 1, 2024
1 parent 3fcd929 commit 978abe2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 73 deletions.
72 changes: 0 additions & 72 deletions file.go

This file was deleted.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func RunE(cmd *cobra.Command, args []string) (err error) {
}
for _, f := range args {
var file stat.File
file, err = Read(f)
file, err = stat.NewFile(f)
if err != nil {
return
}
Expand Down
64 changes: 64 additions & 0 deletions stat/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package stat

import (
"io/fs"
"os"
"os/user"
"path/filepath"
"time"

"github.com/Tom5521/fsize/checkos"
"github.com/Tom5521/fsize/filecount"
"github.com/Tom5521/fsize/flags"
)

type File struct {
Expand All @@ -23,4 +29,62 @@ type File struct {
// IsDir vars

FilesNumber int64

// Raw information.

info os.FileInfo
}

func NewFile(path string) (f File, err error) {
err = f.Load(path)
if err != nil {
return
}

if flags.Progress && f.info.IsDir() && !flags.NoWalk {
err = filecount.Progress(&f.FilesNumber, &f.Size, path)
} else if f.info.IsDir() && !flags.NoWalk {
err = filecount.Print(&f.FilesNumber, &f.Size, path)
}

return
}

func (f *File) Load(path string) (err error) {
_, f.info, f.AbsPath, err = RawInfo(path)

f.Size = f.info.Size()
f.Name = f.info.Name()
f.IsDir = f.info.IsDir()
f.ModTime = f.info.ModTime()
f.Perms = f.info.Mode().Perm()

// Values which do not work on some systems.

// Only on windows systems.
if checkos.Windows {
f.CreationDate = CreationDate(f.info)
}
// Only on unix systems.
if checkos.Unix {
f.User, f.Group, err = UsrAndGroup(f.info)
}

return
}

func RawInfo(name string) (file *os.File, stat os.FileInfo, abspath string, err error) {
file, err = os.Open(name)
if err != nil {
return
}
stat, err = file.Stat()
if err != nil {
return
}
abspath, err = filepath.Abs(name)
if err != nil {
return
}
return
}

0 comments on commit 978abe2

Please sign in to comment.