forked from dundee/gdu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
509 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package analyze | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// EncodeJSON writes JSON representation of dir | ||
func (f *Dir) EncodeJSON(writer io.Writer, topLevel bool) error { | ||
buff := make([]byte, 0, 20) | ||
|
||
buff = append(buff, []byte(`[{"name":`)...) | ||
|
||
if topLevel { | ||
if err := addString(&buff, f.GetPath()); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := addString(&buff, f.GetName()); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
buff = append(buff, '}') | ||
if f.Files.Len() > 0 { | ||
buff = append(buff, ',') | ||
} | ||
buff = append(buff, '\n') | ||
|
||
if _, err := writer.Write(buff); err != nil { | ||
return err | ||
} | ||
|
||
for i, item := range f.Files { | ||
if i > 0 { | ||
if _, err := writer.Write([]byte(",\n")); err != nil { | ||
return err | ||
} | ||
} | ||
err := item.EncodeJSON(writer, false) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if _, err := writer.Write([]byte("]")); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// EncodeJSON writes JSON representation of file | ||
func (f *File) EncodeJSON(writer io.Writer, topLevel bool) error { | ||
buff := make([]byte, 0, 20) | ||
|
||
buff = append(buff, []byte(`{"name":`)...) | ||
if err := addString(&buff, f.GetName()); err != nil { | ||
return err | ||
} | ||
buff = append(buff, []byte(`,"asize":`)...) | ||
buff = append(buff, []byte(fmt.Sprint(f.GetSize()))...) | ||
buff = append(buff, []byte(`,"dsize":`)...) | ||
buff = append(buff, []byte(fmt.Sprint(f.GetUsage()))...) | ||
buff = append(buff, '}') | ||
|
||
if _, err := writer.Write(buff); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func addString(buff *[]byte, val string) error { | ||
b, err := json.Marshal(val) | ||
if err != nil { | ||
return err | ||
} | ||
*buff = append(*buff, b...) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package analyze | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
log.SetLevel(log.WarnLevel) | ||
} | ||
|
||
func TestEncode(t *testing.T) { | ||
dir := &Dir{ | ||
File: &File{ | ||
Name: "test_dir", | ||
Size: 10, | ||
Usage: 18, | ||
}, | ||
ItemCount: 4, | ||
BasePath: ".", | ||
} | ||
|
||
subdir := &Dir{ | ||
File: &File{ | ||
Name: "nested", | ||
Size: 9, | ||
Usage: 14, | ||
Parent: dir, | ||
}, | ||
ItemCount: 3, | ||
} | ||
file := &File{ | ||
Name: "file2", | ||
Size: 3, | ||
Usage: 4, | ||
Parent: subdir, | ||
} | ||
file2 := &File{ | ||
Name: "file", | ||
Size: 5, | ||
Usage: 6, | ||
Parent: subdir, | ||
} | ||
dir.Files = Files{subdir} | ||
subdir.Files = Files{file, file2} | ||
|
||
var buff bytes.Buffer | ||
err := dir.EncodeJSON(&buff, true) | ||
|
||
assert.Nil(t, err) | ||
assert.Contains(t, buff.String(), `"name":"nested"`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.