aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/utilities.go
blob: 4461755f267ce25eb2bd17499035a6aebd229caf (plain) (blame)
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
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

package utilities

import (
	"math/rand"
	"os"
	"time"
	"unicode/utf8"

	"github.com/spf13/viper"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func GetRandomQuote() string {
	quotes := viper.GetStringSlice("space.footer.quotes")

	return quotes[rand.Intn(len(quotes))]
}

func GetCopyright() string {
	return viper.GetString("space.footer.copyright")
}

// DoesFilesExist Check if the following files exist, return files that don't exist
func DoesFilesExist(files []string) []string {
	var nonExistent []string

	for _, file := range files {
		// https://stackoverflow.com/a/12518877
		if _, err := os.Stat(file); os.IsNotExist(err) {
			nonExistent = append(nonExistent, file)
		}
	}

	return nonExistent
}

func TrimLastChar(s string) string {
	r, size := utf8.DecodeLastRuneInString(s)
	if r == utf8.RuneError && (size == 0 || size == 1) {
		size = 0
	}
	return s[:len(s)-size]
}