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
13 changed files
with
458 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[1,2,{"progname":"gdu","progver":"development","timestamp":1626807263}, | ||
[{"name":"/home/gdu"}, | ||
[{"name":"app"}, | ||
{"name":"app.go","asize":4638,"dsize":8192}, | ||
{"name":"app_linux_test.go","asize":1410,"dsize":4096}, | ||
{"name":"app_test.go","asize":4974,"dsize":8192}], | ||
{"name":"main.go","asize":3205,"dsize":4096}]] |
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 @@ | ||
[1,2,3,4] |
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,82 @@ | ||
package report | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"strings" | ||
|
||
"github.com/dundee/gdu/v5/pkg/analyze" | ||
) | ||
|
||
// ReadAnalysis reads analysis report from JSON file and returns directory item | ||
func ReadAnalysis(input io.Reader) (*analyze.Dir, error) { | ||
var data interface{} | ||
|
||
var buff bytes.Buffer | ||
buff.ReadFrom(input) | ||
json.Unmarshal(buff.Bytes(), &data) | ||
|
||
dataArray, ok := data.([]interface{}) | ||
if !ok { | ||
return nil, errors.New("JSON file does not contain top level array") | ||
} | ||
if len(dataArray) < 4 { | ||
return nil, errors.New("Top level array must have at least 4 items") | ||
} | ||
|
||
items, ok := dataArray[3].([]interface{}) | ||
if !ok { | ||
return nil, errors.New("Array of maps not found in the top level array on 4th position") | ||
} | ||
|
||
return processDir(items) | ||
} | ||
|
||
func processDir(items []interface{}) (*analyze.Dir, error) { | ||
dir := &analyze.Dir{ | ||
File: &analyze.File{ | ||
Flag: ' ', | ||
}, | ||
} | ||
dirMap, ok := items[0].(map[string]interface{}) | ||
if !ok { | ||
return nil, errors.New("Directory item is not a map") | ||
} | ||
name, ok := dirMap["name"].(string) | ||
if !ok { | ||
return nil, errors.New("Directory name is not a string") | ||
} | ||
|
||
slashPos := strings.LastIndex(name, "/") | ||
if slashPos > -1 { | ||
dir.Name = name[slashPos+1:] | ||
dir.BasePath = name[:slashPos+1] | ||
} else { | ||
dir.Name = name | ||
} | ||
|
||
for _, v := range items[1:] { | ||
switch item := v.(type) { | ||
case map[string]interface{}: | ||
file := &analyze.File{} | ||
file.Name = item["name"].(string) | ||
file.Size = int64(item["asize"].(float64)) | ||
file.Usage = int64(item["dsize"].(float64)) | ||
file.Parent = dir | ||
file.Flag = ' ' | ||
|
||
dir.Files.Append(file) | ||
case []interface{}: | ||
subdir, err := processDir(item) | ||
if err != nil { | ||
return nil, err | ||
} | ||
subdir.Parent = dir | ||
dir.Files.Append(subdir) | ||
} | ||
} | ||
|
||
return dir, nil | ||
} |
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,81 @@ | ||
package report | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
log.SetLevel(log.WarnLevel) | ||
} | ||
|
||
func TestReadAnalysis(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(` | ||
[1,2,{"progname":"gdu","progver":"development","timestamp":1626806293}, | ||
[{"name":"/home/xxx"}, | ||
{"name":"gdu.json","asize":33805233,"dsize":33808384}, | ||
[{"name":"app"}, | ||
{"name":"app.go","asize":4638,"dsize":8192}, | ||
{"name":"app_linux_test.go","asize":1410,"dsize":4096}, | ||
{"name":"app_test.go","asize":4974,"dsize":8192}], | ||
{"name":"main.go","asize":3205,"dsize":4096}]] | ||
`)) | ||
|
||
dir, err := ReadAnalysis(buff) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, "xxx", dir.GetName()) | ||
assert.Equal(t, "/home/xxx", dir.GetPath()) | ||
} | ||
|
||
func TestReadAnalysisWithEmptyInput(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(``)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "JSON file does not contain top level array", err.Error()) | ||
} | ||
|
||
func TestReadAnalysisWithEmptyArray(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(`[]`)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "Top level array must have at least 4 items", err.Error()) | ||
} | ||
|
||
func TestReadAnalysisWithWrongContent(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(`[1,2,3,4]`)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "Array of maps not found in the top level array on 4th position", err.Error()) | ||
} | ||
|
||
func TestReadAnalysisWithEmptyDirContent(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(`[1,2,3,[{}]]`)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "Directory name is not a string", err.Error()) | ||
} | ||
|
||
func TestReadAnalysisWithWrongDirItem(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(`[1,2,3,[1, 2, 3]]`)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "Directory item is not a map", err.Error()) | ||
} | ||
|
||
func TestReadAnalysisWithWrongSubdirItem(t *testing.T) { | ||
buff := bytes.NewBuffer([]byte(`[1,2,3,[{"name":"xxx"}, [1,2,3]]]`)) | ||
|
||
_, err := ReadAnalysis(buff) | ||
|
||
assert.Equal(t, "Directory item is not a map", err.Error()) | ||
} |
Oops, something went wrong.