Skip to content

Commit

Permalink
Optionally compress the output of the UserGroup and GroupUser summari…
Browse files Browse the repository at this point in the history
…sers (#20)
  • Loading branch information
mjkw31 authored Jan 21, 2025
1 parent f37e9e8 commit 0292312
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions cmd/summarise.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ Summarise takes the following arguments
output all summarisers to here with the default names.
--userGroup,-u
usergroup output file. Defaults to DEFAULTDIR/byusergroup, if --defaultDir is set.
usergroup output file. Defaults to DEFAULTDIR/byusergroup.gz, if --defaultDir is set.
If filename ends in '.gz' the file will be gzip compressed.
--groupUser,-g
groupUser output file. Defaults to DEFAULTDIR/bygroup, if --defaultDir is set.
If filename ends in '.gz' the file will be gzip compressed.
--basedirsDB,-b
basedirs output file. Defaults to DEFAULTDIR/basedirs.db, if --defaultDir is set.
Expand Down Expand Up @@ -176,7 +178,7 @@ func setArgsDefaults() {
}

if userGroup == "" {
userGroup = filepath.Join(defaultDir, "byusergroup")
userGroup = filepath.Join(defaultDir, "byusergroup.gz")
}

if groupUser == "" {
Expand Down Expand Up @@ -224,18 +226,45 @@ func addUserGroupSummariser(s *summary.Summariser, userGroup string) error {
return fmt.Errorf("failed to create usergroup file: %w", err)
}

s.AddDirectoryOperation(usergroup.NewByUserGroup(uf))
s.AddDirectoryOperation(usergroup.NewByUserGroup(wrapCompressed(uf)))

return nil
}

type compressedFile struct {
*pgzip.Writer
file *os.File
}

func (c *compressedFile) Close() error {
err := c.Writer.Close()
errr := c.file.Close()

if err != nil {
return err
}

return errr
}

func wrapCompressed(wc *os.File) io.WriteCloser {
if !strings.HasSuffix(wc.Name(), ".gz") {
return wc
}

return &compressedFile{
Writer: pgzip.NewWriter(wc),
file: wc,
}
}

func addGroupUserSummariser(s *summary.Summariser, groupUser string) error {
gf, err := os.Create(groupUser)
if err != nil {
return fmt.Errorf("failed to create groupuser file: %w", err)
}

s.AddGlobalOperation(groupuser.NewByGroupUser(gf))
s.AddGlobalOperation(groupuser.NewByGroupUser(wrapCompressed(gf)))

return nil
}
Expand Down

0 comments on commit 0292312

Please sign in to comment.