Skip to content

Commit

Permalink
Merge pull request #22 from DiSiqueira/tree
Browse files Browse the repository at this point in the history
Updating prints to utilize GoTree library
  • Loading branch information
disiqueira authored Jan 16, 2017
2 parents 627e10f + da84b40 commit 36d7f91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
46 changes: 43 additions & 3 deletions gorganizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"strings"
"github.com/disiqueira/gotree"
)

const (
Expand All @@ -19,6 +20,36 @@ var cfg *ini.File
var cfgFile string
var language string

func addToTree(folder, file string, tree gotree.GTStructure) gotree.GTStructure {

found := false

for i, item := range tree.Items {
if item.Name == folder {

var newFile gotree.GTStructure
newFile.Name = file

item.Items = append(item.Items, newFile)
tree.Items[i] = item
}
}

if !found {
var newFolder gotree.GTStructure
var newFile gotree.GTStructure

newFolder.Name = folder
newFile.Name = file

newFolder.Items = append(newFolder.Items, newFile)

tree.Items = append(tree.Items, newFolder)
}

return tree
}

func main() {
outputFolder := flag.String("output", ".", "Main directory to put organized folders")
inputFolder := flag.String("directory", ".", "The directory whose files to classify")
Expand Down Expand Up @@ -64,6 +95,10 @@ func main() {
files, _ := ioutil.ReadDir(*inputFolder)
fmt.Println("GOrganizing your Files")

var tree gotree.GTStructure

tree.Name = "Files"

for _, f := range files {
if f.IsDir() {
continue
Expand All @@ -76,18 +111,23 @@ func main() {

if len(newFolder) > 0 {

folder := filepath.Join(*outputFolder, iniGet(ext))
folder := filepath.Join(*outputFolder, newFolder)
newFile := filepath.Join(folder, f.Name())

fmt.Println(file + " --> " + newFile)

if !*preview {
_ = os.Mkdir(folder, os.ModePerm)
os.Rename(file, newFile)
}
} else {
newFolder = "Unknown extension (will not be moved)"
}

tree = addToTree(newFolder, f.Name(), tree)

}

gotree.PrintTree(tree)

fmt.Println("All files have been gorganized!")

}
22 changes: 19 additions & 3 deletions ini.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"strings"
"github.com/disiqueira/gotree"
)

func iniGet(key string) string {
Expand Down Expand Up @@ -49,15 +49,31 @@ func iniDelete(key string) bool {
func iniScanExt() {

names := cfg.SectionStrings()
fmt.Println("Extension | Folder")

var tree gotree.GTStructure

tree.Name = "Rules"

for _, section := range names[1:] {

var treeFolder gotree.GTStructure

treeFolder.Name = section

keys := cfg.Section(section).KeyStrings()

for _, key := range keys {
fmt.Printf("%s | %s\n", key, section)

var treeItem gotree.GTStructure
treeItem.Name = key

treeFolder.Items = append(treeFolder.Items, treeItem)
}

tree.Items = append(tree.Items, treeFolder)

}

gotree.PrintTree(tree)

}

0 comments on commit 36d7f91

Please sign in to comment.