diff options
| -rw-r--r-- | .gitignore | 31 | ||||
| -rw-r--r-- | Makefile | 18 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | cmd/uptfs/main.go | 44 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 3 | ||||
| -rw-r--r-- | internal/config/config.go | 56 | ||||
| -rw-r--r-- | internal/split/split.go | 44 |
8 files changed, 202 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15a7c3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env + +# binary +./uptfs + +# config +config.yaml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6451759 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +BINARY_DIR=./cmd/uptfs + +.PHONY: all +all: build + +.PHONY: build +build: + @echo "Building Go uptfs..." + go build $(BINARY_DIR) + +.PHONY: clean +clean: + @echo "Cleaning build artifacts..." + rm -f ./uptfs + +.PHONY: run +run: + ./uptfs
\ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5b0d9c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Undeclared Purpose Text Filtering System (uptfs) diff --git a/cmd/uptfs/main.go b/cmd/uptfs/main.go new file mode 100644 index 0000000..c8fbf89 --- /dev/null +++ b/cmd/uptfs/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "os" + "strings" + "uptfs/internal/config" + "uptfs/internal/split" +) + +const filepath = "" + +func main() { + var config config.Config + config.LoadConfig(filepath) + + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + inputString := scanner.Text() + + if inputString == "" { + err := errors.New("the input string is empty") + fmt.Printf("%v\n", err) + os.Exit(1) + } + + additionalDelimeters := []string{",", "."} + tokens := strings.Split(inputString, " ") + tokens = formatInput(tokens, additionalDelimeters) + + fmt.Println(tokens) +} + +func formatInput(tokenArray []string, delimeterArray []string) []string { + for _, delimeter := range delimeterArray { + for index, element := range tokenArray { + tokenArray = split.NewArrayWithSplit(tokenArray, index, element, delimeter) + } + } + + return tokenArray +} @@ -0,0 +1,5 @@ +module uptfs + +go 1.23.2 + +require gopkg.in/yaml.v3 v3.0.1 // indirect @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..ad35ee7 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,56 @@ +package config + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Filters []string `yaml:"filters"` + Iterations int `yaml:"iterations"` +} + +func getDefaultConfigPath() (defaultPath string, err error) { + programName := "uptfs" + configFileName := "config.yaml" + + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { + return filepath.Join(xdg, programName, configFileName), nil + } + + if home := os.Getenv("HOME"); home != "" { + return filepath.Join(home, programName, configFileName), nil + } + + return "", errors.New("both XDG_CONFIG_HOME and HOME are not set, can't proceed") +} + +func (c *Config) LoadConfig(filepath string) *Config { + if filepath == "" { + var err error + filepath, err = getDefaultConfigPath() + + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + } + + yamlFile, err := os.ReadFile(filepath) + + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + err = yaml.Unmarshal(yamlFile, c) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + + return c +} diff --git a/internal/split/split.go b/internal/split/split.go new file mode 100644 index 0000000..65e1a14 --- /dev/null +++ b/internal/split/split.go @@ -0,0 +1,44 @@ +package split + +import ( + "strings" +) + +func checkArrayElementsEmpty(array []string) bool { + for _, str := range array { + if str != "" { + return false + } + } + return true +} + +func NewArrayWithSplit(initialArray []string, index int, token string, delimeter string) (result []string) { + split := strings.Split(token, delimeter) + splitLength := len(split) + + /* + When a token only consists of delimeter * N (N >= 0), + the resulting split consists of N empty elements. + Here we check if it is so and essentialy remove that token + from resulting array. + */ + splitIsEmpty := checkArrayElementsEmpty(split) + if splitIsEmpty { + result = append(initialArray[:index], initialArray[index+1:]...) + return result + } + + if splitLength > 1 { + if split[splitLength-1] == "" { + split = split[:splitLength-1] + } + + result = append(initialArray[:index], append(split, initialArray[index+1:]...)...) + } + if splitLength == 1 { + result = initialArray + } + + return result +} |
