From ec347cdd1527fbfe21492c0df378073537cff1a8 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 9 Apr 2024 20:20:20 +0200 Subject: cursor fucked up but ye --- go.mod | 2 ++ go.sum | 4 ++++ src/i_insert.go | 34 +++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 914f74c..e106c87 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,12 @@ require ( github.com/gdamore/tcell/v2 v2.7.0 golang.org/x/term v0.17.0 gopkg.in/yaml.v3 v3.0.1 + k8s.io/apimachinery v0.29.3 ) require ( github.com/gdamore/encoding v1.0.0 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/rivo/uniseg v0.4.3 // indirect diff --git a/go.sum b/go.sum index 81494ad..292bd53 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA= github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -51,3 +53,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU= +k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU= diff --git a/src/i_insert.go b/src/i_insert.go index ea1310d..5028c49 100644 --- a/src/i_insert.go +++ b/src/i_insert.go @@ -43,7 +43,7 @@ * POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/i_insert.go - * Tue Apr 09 14:56:56 2024 + * Tue Apr 09 15:36:02 2024 * Joe * * insert a new host @@ -53,13 +53,14 @@ package main import ( "errors" - "fmt" "os" + "regexp" "strconv" "strings" "github.com/gdamore/tcell/v2" "gopkg.in/yaml.v3" + "k8s.io/apimachinery/pkg/util/uuid" ) func i_insert_format_filename(name, path string) string { @@ -68,21 +69,36 @@ func i_insert_format_filename(name, path string) string { if len(name) == 0 { return "" } + re := regexp.MustCompile("[^a-zA-Z0-9]+") + replace := "_" + str = re.ReplaceAllString(str, replace) + _, err := os.Stat(path + str + ".yml") + for err == nil { + uid := uuid.NewUUID() + str += "_" + string(uid[0:4]) + _, err = os.Stat(path + str + ".yml") + } str = strings.ToLower(str) + ".yml" return str } func i_insert_host(data *HardData, insert *HostNode) { - data.ui.s.Fini() - fmt.Println(i_insert_format_filename(insert.Name, - data.data_dir + insert.parent.path())) - fmt.Println(data.data_dir + insert.parent.path()) - os.Exit(0) - _, err := yaml.Marshal(insert) + filename := i_insert_format_filename(insert.Name, + data.data_dir + insert.parent.path()) + insert.filename = filename + fmt, err := yaml.Marshal(insert) + if err != nil { + c_error_mode("yaml", err, &data.ui) + data.insert = nil + return + } + err = os.WriteFile(data.data_dir + insert.parent.path() + filename, + fmt, 0644) if err != nil { + c_error_mode("can't write file", err, &data.ui) + data.insert = nil return } - // err = os.WriteFile(i_insert_format_filename(insert.Name)) item := &ItemsNode{ data.litems.curr.ID + 1, nil, -- cgit v1.2.3