better architecture for file system
This commit is contained in:
		
							parent
							
								
									4f43d824a6
								
							
						
					
					
						commit
						cca5052df8
					
				|  | @ -20,14 +20,15 @@ var registryCmd = &cobra.Command{ | ||||||
| 	Long: `Distributes tokens from a file or a direcotry | 	Long: `Distributes tokens from a file or a direcotry | ||||||
| 	into a HashMap and prints the Map`, | 	into a HashMap and prints the Map`, | ||||||
| 	Run: func(cmd *cobra.Command, args []string) { | 	Run: func(cmd *cobra.Command, args []string) { | ||||||
| 		tokens := splitter.SplitFile(args[0]) | 		entrys := splitter.SplitFile(args[0]) | ||||||
| 		tr := registry.TokenRegistry{ Map: make(map[string]int64)} | 		tr := registry.TokenRegistry{ Map: make(map[string]int64)} | ||||||
| 		tr.DistributeTokens(tokens) | 
 | ||||||
|  | 		tr.DistributeTokens(entrys.CreateMatrix()) | ||||||
| 
 | 
 | ||||||
| 		tw := new(tabwriter.Writer) | 		tw := new(tabwriter.Writer) | ||||||
| 		tw.Init(os.Stdout, 0, 8, 0, '\t', 0) | 		tw.Init(os.Stdout, 0, 8, 0, '\t', 0) | ||||||
| 		for v, k := range tr.Map { | 		for v, k := range tr.Map { | ||||||
| 			fmt.Fprintf(tw, "value: \"%s\" \t key: %b \n", v, k) | 			fmt.Fprintf(tw, "value: \"%s\" \t key: %d \n", v, k) | ||||||
| 		} | 		} | ||||||
| 		tw.Flush() | 		tw.Flush() | ||||||
| 	}, | 	}, | ||||||
|  |  | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | package splitter | ||||||
|  | 
 | ||||||
|  | type RegistryEntry struct { | ||||||
|  | 	Name string | ||||||
|  | 	ChildEntrys []RegistryEntry | ||||||
|  | 	Tokens []string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (re RegistryEntry) CreateMatrix() [][]string { | ||||||
|  | 	if re.ChildEntrys != nil { | ||||||
|  | 		mat := [][]string{} | ||||||
|  | 		for _, child := range re.ChildEntrys { | ||||||
|  | 			mat = addToMatrix(mat, child.CreateMatrix()) | ||||||
|  | 		} | ||||||
|  | 		return mat | ||||||
|  | 	} else { | ||||||
|  | 		return [][]string{re.Tokens} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | @ -6,10 +6,7 @@ import ( | ||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func SplitFile(path string) [][]string { | func SplitFile(path string) RegistryEntry { | ||||||
| 	if path == "" { |  | ||||||
| 		return make([][]string, 0) |  | ||||||
| 	} |  | ||||||
| 	info, err := os.Stat(path) | 	info, err := os.Stat(path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		panic(err) | 		panic(err) | ||||||
|  | @ -17,19 +14,27 @@ func SplitFile(path string) [][]string { | ||||||
| 
 | 
 | ||||||
| 	if info.IsDir() { | 	if info.IsDir() { | ||||||
| 		paths := getFilePathsFromDir(path) | 		paths := getFilePathsFromDir(path) | ||||||
| 		matrix := make([][]string, len(paths)) | 		arr := make([]RegistryEntry, len(paths)) | ||||||
|  | 
 | ||||||
| 		for _, filePath := range paths { | 		for _, filePath := range paths { | ||||||
| 			matrix = addToMatrix(matrix, SplitFile(filePath)) | 			if filePath != "" { | ||||||
|  | 				arr = append(arr, SplitFile(filePath)) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return RegistryEntry{ | ||||||
|  | 			Name: info.Name(),  | ||||||
|  | 			Tokens: nil, | ||||||
|  | 			ChildEntrys: arr, | ||||||
| 		} | 		} | ||||||
| 		return matrix |  | ||||||
| 	} else { | 	} else { | ||||||
| 		matrix := make([][]string, 1) | 		return RegistryEntry{ | ||||||
| 		matrix[0] = tokenize(getFileContent(path)) | 			Name: info.Name(), | ||||||
| 		return matrix | 			Tokens: tokenize(getFileContent(path)), | ||||||
|  | 			ChildEntrys: nil, | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| func getFileContent(path string) string{ | func getFileContent(path string) string{ | ||||||
| 	data, err := os.ReadFile(path) | 	data, err := os.ReadFile(path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue