github.com/anishathalye/periscope@v0.3.5/internal/periscope/export.go (about) 1 package periscope 2 3 import ( 4 "github.com/anishathalye/periscope/internal/db" 5 "github.com/anishathalye/periscope/internal/herror" 6 7 "encoding/json" 8 ) 9 10 type ExportFormat int 11 12 const ( 13 JsonFormat ExportFormat = iota 14 ) 15 16 type ExportOptions struct { 17 Format ExportFormat 18 } 19 20 func (ps *Periscope) Export(options *ExportOptions) herror.Interface { 21 c, err := ps.db.AllDuplicatesC("") 22 if err != nil { 23 return err 24 } 25 return ps.jsonExport(c) 26 } 27 28 type exportDuplicateInfo struct { 29 Paths []string `json:"paths"` 30 Size int64 `json:"size"` 31 } 32 33 type exportResult struct { 34 Duplicates []exportDuplicateInfo `json:"duplicates"` 35 } 36 37 func (ps *Periscope) jsonExport(c <-chan db.DuplicateSet) herror.Interface { 38 duplicates := make([]exportDuplicateInfo, 0) 39 for set := range c { 40 size := set[0].Size // all files within a set are the same size 41 var paths []string 42 for _, info := range set { 43 paths = append(paths, info.Path) 44 } 45 duplicates = append(duplicates, exportDuplicateInfo{ 46 Paths: paths, 47 Size: size, 48 }) 49 } 50 res := exportResult{ 51 Duplicates: duplicates, 52 } 53 enc := json.NewEncoder(ps.outStream) 54 enc.SetIndent("", " ") 55 err := enc.Encode(res) 56 if err != nil { 57 return herror.Internal(err, "") 58 } 59 return nil 60 }