summaryrefslogtreecommitdiff
path: root/internal/api/utilities.go
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-08-08 12:10:43 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-08-08 12:52:03 -0400
commite641812d9fc55dff2e5d18ae5c557fc8c74c4c14 (patch)
tree59e4ffce74d07e205b53c7d7991e4d6c603a3a98 /internal/api/utilities.go
parentf1552ea8c2b5e3f09533c08b40ad7331a82d7a33 (diff)
Cleanup api impl
Diffstat (limited to 'internal/api/utilities.go')
-rw-r--r--internal/api/utilities.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/api/utilities.go b/internal/api/utilities.go
new file mode 100644
index 0000000..508e341
--- /dev/null
+++ b/internal/api/utilities.go
@@ -0,0 +1,74 @@
+// This file is a part of Taskflow.
+// Copyright (C) 2025 Robby Zambito
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package api
+
+import (
+ "math/rand"
+ "net"
+ "regexp"
+ "strings"
+)
+
+func runChance(likelihood float64, action func()) {
+ if likelihood <= 0 {
+ return // never run
+ }
+ if likelihood >= 1 {
+ action()
+ return
+ }
+
+ if rand.Float64() < likelihood {
+ action()
+ }
+}
+
+func redactIP(input string) string {
+ ipRegex := `\b(?:\d{1,3}\.){3}\d{1,3}\b`
+ re := regexp.MustCompile(ipRegex)
+
+ return re.ReplaceAllStringFunc(input, func(match string) string {
+ if ip := net.ParseIP(match); ip != nil {
+ parts := strings.Split(match, ".")
+ if len(parts) == 4 {
+ parts[3] = "XXX"
+ return strings.Join(parts, ".")
+ }
+ }
+ return match
+ })
+}
+
+func addRotLog(logs *[LogLength]string, last *int, parser chan string, value string) {
+ if strings.Contains(value, "\n") {
+ for _, v := range strings.Split(value, "\n") {
+ addRotLog(logs, last, parser, v)
+ }
+ } else {
+ if *last == LogLength {
+ for i := 0; i < LogLength-1; i++ {
+ logs[i] = logs[i+1]
+ }
+ logs[LogLength-1] = value
+ parser <- value
+ } else {
+ logs[*last] = value
+ *last++
+ parser <- value
+ }
+ }
+}