summaryrefslogtreecommitdiff
path: root/cmd/uptfs/main.go
blob: db7312cedfe959f516f955a350935bc1a94e4836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/alexflint/go-arg"
	"github.com/ayyansea/uptfs/internal/config"
	"github.com/ayyansea/uptfs/internal/filter"
	"github.com/ayyansea/uptfs/internal/split"
	"github.com/ayyansea/uptfs/internal/token"
)

var args struct {
	ConfigFile string `arg:"-c" help:"path to config file" default:""`
}

func errExit(err error) {
	fmt.Println(err)
	os.Exit(1)
}

func main() {
	arg.MustParse(&args)

	var configFilePath string
	var err error

	if args.ConfigFile != "" {
		configFilePath, err = filepath.Abs(args.ConfigFile)
	}
	if err != nil {
		errExit(err)
	}

	var config config.Config
	config.LoadConfig(configFilePath)
	fmt.Printf("Config: %v\n", config)

	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 = split.FormatInput(tokens, additionalDelimeters)

	if len(tokens) == 0 {
		err := errors.New("the slice is empty")
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

	var linkedTokens token.LinkedTokenList
	token.SliceToLinkedTokenSlice(tokens, &linkedTokens)

	for current := linkedTokens.GetHead(); current != nil; current = current.GetNextToken() {
		for _, filterName := range config.Filters {
			filter := filter.FilterList[filterName]()
			current.SetContent(filter.Filter(current.GetContent()))
		}
		fmt.Println(current.GetContent())
	}
}