Skip to content

Commit

Permalink
Optionally compressed the output of the UserGroup and GroupUser summa…
Browse files Browse the repository at this point in the history
…risers
  • Loading branch information
mjkw31 committed Jan 20, 2025
1 parent a9b5480 commit 4468fed
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 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.
groupUser output file. Defaults to DEFAULTDIR/bygroup.gz, 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,11 +178,11 @@ func setArgsDefaults() {
}

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

if groupUser == "" {
groupUser = filepath.Join(defaultDir, "bygroup")
groupUser = filepath.Join(defaultDir, "bygroup.gz")
}

if basedirsDB == "" {
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 4468fed

Please sign in to comment.