summaryrefslogtreecommitdiff
path: root/cmd/uptfs/main.go
blob: c7b98e1d21e337c43c20d3b18f11b00d944e7bf5 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main

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

	"github.com/alexflint/go-arg"
	"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:""`
	InputFile  string   `arg:"-i" help:"path to input file" default:""`
	OutputFile string   `arg:"-o" help:"path to output file" default:""`
	Filters    []string `arg:"-f" help:"list of filters" default:""`
}

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

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

	var configFilePath, inputFilePath, outFilePath string
	var err error

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

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

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

	fmt.Printf("%v %v %v\n",
		configFilePath,
		inputFilePath,
		outFilePath)

	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)

	lowercaseFilter := filter.NewLowercaseFilterWithExtraSteps()
	uppercaseFilter := filter.NewUppercaseFilter()

	for current := linkedTokens.GetHead(); current != nil; current = current.GetNextToken() {
		current.SetContent(lowercaseFilter.Filter(current.GetContent()))
		current.SetContent(uppercaseFilter.Filter(current.GetContent()))
	}
}