Skip to content

Commit

Permalink
add spilt
Browse files Browse the repository at this point in the history
  • Loading branch information
scottafk committed Mar 23, 2023
1 parent 798ff80 commit a4a7ca4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
39 changes: 31 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,37 @@ import (
var (
outputName string
inputName string
unpackMode bool
packMode bool
unpackMode bool //json to dir
packMode bool //dir to json
debug bool
version bool
singleSeparate bool
importNew bool
withGraph bool
split bool
number uint
dirs = []string{dirSnippet, dirMenu, dirLang, dirTable, dirParam, dirData, dirPage, dirCon}
)

func main() {
func init() {
flag.BoolVar(&unpackMode, "unpack", false, "-u, unpacking mode")
flag.StringVar(&inputName, "input", ".", "-i, path for input files, filename for pack and dirname/ (slashed) for unpack")
flag.StringVar(&outputName, "output", "output", "-o, output filename for JSON if input file name not pointed")

flag.BoolVar(&split, "s", false, "split json file by type")
flag.UintVar(&number, "n", 0, "split json file by number")

// shorthand
flag.StringVar(&outputName, "o", "output", "-output")
flag.StringVar(&inputName, "i", ".", "input")
flag.BoolVar(&unpackMode, "u", false, "-unpack")
flag.BoolVar(&version, "v", false, "-version")
flag.BoolVar(&debug, "d", false, "debug")
flag.BoolVar(&withGraph, "g", false, "make graphical structure in dot-file")
flag.Parse()
}

func main() {
flag.Parse()
args := flag.Args()
if argsCount := len(args); argsCount == 0 {
// without args run gui
Expand Down Expand Up @@ -74,9 +81,12 @@ func checkOutput() {
pLen := len(parts)
outputName = parts[pLen-1]
if unpackMode {
ext := filepath.Ext(outputName)
outputName = outputName[:len(outputName)-len(ext)]
outputName = outputName + separator
if !split {
ext := filepath.Ext(outputName)
outputName = outputName[:len(outputName)-len(ext)]
outputName = outputName + separator
}

} else {
if strings.HasSuffix(inputName, separator) {
outputName = parts[pLen-2]
Expand All @@ -92,7 +102,7 @@ func checkOutput() {
fmt.Println(helpMsg)
return
}
if !strings.HasSuffix(outputName, separator) {
if !strings.HasSuffix(outputName, separator) && !split {
outputName = outputName + separator
}
if debug {
Expand Down Expand Up @@ -127,6 +137,19 @@ func writeFileString(filename, content string) {
}
}

func writeFileString2(filename, content string) {
outFile, err := os.Create(filepath.Join(filename))
if err != nil {
fmt.Println("error write file:", err)
return
}
defer outFile.Close()
if _, err := outFile.WriteString(content); err != nil {
fmt.Println(err)
return
}
}

func stringInSlice(arr []string, val string) bool {
for _, v := range arr {
if v == val {
Expand Down
67 changes: 61 additions & 6 deletions unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/spkg/bom"
"os"
"path/filepath"
"strconv"
)

func unpackJSON(filename string) {
bs, err := ioutil.ReadFile(filename)
bs, err := os.ReadFile(filename)
if err != nil {
fmt.Println(err)
return
Expand All @@ -28,8 +28,12 @@ func unpackJSON(filename string) {
fmt.Println("unmarshal file error_2:", err)
return
}
unpackDataFile(file.Data)

if split {
splitDataFile(file)
return
} else {
unpackDataFile(file.Data)
}
} else {
file := importFile{}
if err := json.Unmarshal(bs, &file); err != nil {
Expand Down Expand Up @@ -97,3 +101,54 @@ func unpackDataFile(items []importStruct) {
writeFileString(fullName, value)
}
}

func splitDataFile(items dataFile) {
var tp = make(map[string][]importStruct)
for _, out := range items.Data {
var path string
ext := filepath.Ext(outputName)
path = outputName[:len(outputName)-len(ext)] + "." + out.dir() + ext
tp[path] = append(tp[path], out)
}
for path, out := range tp {
segments := splitArray(out, int64(number))
for i := 0; i < len(segments); i++ {
split := segments[i]
data := dataFile{}
data.Name = items.Name
data.Conditions = items.Conditions
data.Data = append(data.Data, split...)
result, _ := _JSONMarshal(data, true)
ext := filepath.Ext(path)
path := path[:len(path)-len(ext)] + strconv.Itoa(i+1) + ext
writeFileString2(path, string(result))
}
}

}

func splitArray(arr []importStruct, num int64) [][]importStruct {
max := int64(len(arr))

if max <= num || num <= 0 {
return [][]importStruct{arr}
}
var quantity int64
if max%num == 0 {
quantity = max / num
} else {
quantity = (max / num) + 1
}
var segments = make([][]importStruct, 0)
var start, end, i int64
for i = 1; i <= quantity; i++ {
end = i * num
if i != quantity {
segments = append(segments, arr[start:end])
} else {
segments = append(segments, arr[start:])
}
start = i * num
}
return segments
}

0 comments on commit a4a7ca4

Please sign in to comment.