diff options
| author | ayyansea <ayyansea@gmail.com> | 2024-11-18 21:53:29 +0300 |
|---|---|---|
| committer | ayyansea <ayyansea@gmail.com> | 2024-11-18 21:53:29 +0300 |
| commit | 574e76ae935c4931ec50b14a94dee930ed6f3d5a (patch) | |
| tree | a289a98536930cd90a0d8e7e05420f3a35934923 /util | |
| parent | d741a3187f0e8abb00ee34356b3e61ab9087c0d9 (diff) | |
feat: restructure project + add a basic cli arg parser
Diffstat (limited to 'util')
| -rw-r--r-- | util/config/config.go | 56 | ||||
| -rw-r--r-- | util/split/split.go | 45 |
2 files changed, 0 insertions, 101 deletions
diff --git a/util/config/config.go b/util/config/config.go deleted file mode 100644 index ad35ee7..0000000 --- a/util/config/config.go +++ /dev/null @@ -1,56 +0,0 @@ -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/util/split/split.go b/util/split/split.go deleted file mode 100644 index 2fc29db..0000000 --- a/util/split/split.go +++ /dev/null @@ -1,45 +0,0 @@ -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 -} |
