From 0d779785fabbaf0f92fc501f040f4dfb34685fb4 Mon Sep 17 00:00:00 2001 From: Jonathan Seth Mainguy Date: Mon, 16 Sep 2024 16:07:51 -0400 Subject: [PATCH] feat: remove unused functions --- config.go | 3 +-- main.go | 6 +----- payload.go | 58 ++---------------------------------------------------- 3 files changed, 4 insertions(+), 63 deletions(-) diff --git a/config.go b/config.go index ef84215..13652b9 100644 --- a/config.go +++ b/config.go @@ -2,7 +2,6 @@ package main import ( "crypto/x509" - "io/ioutil" "os" ) @@ -35,7 +34,7 @@ func getConfig() (config Config, err error) { if config.Certificate != "" { - certificate, err := ioutil.ReadFile(config.Certificate) + certificate, err := os.ReadFile(config.Certificate) if err != nil { return config, err } diff --git a/main.go b/main.go index d09c377..6256cb8 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,5 @@ func main() { config, err := getConfig() check(err) - fieldList := getFields(config) - - body := formatPayload(fieldList, config) - - submitPayload(config, body) + submitPayload(config) } diff --git a/payload.go b/payload.go index 9a22ab9..390ab48 100644 --- a/payload.go +++ b/payload.go @@ -1,55 +1,13 @@ package main import ( - "bytes" "crypto/tls" "crypto/x509" - "encoding/json" "fmt" "io" - "io/ioutil" "net/http" - "strings" ) -func formatPayload(fieldList *FieldList, config Config) (body io.Reader) { - payload := Payload{} - payload.Attributes.Title = "filebeat-*" - payload.Attributes.TimeFieldName = "@timestamp" - - payload.Attributes.Fields = "[" - if config.KibanaFilter != "" { - fmt.Printf("Filtering out %s\n", config.KibanaFilter) - } - for _, v := range fieldList.Fields { - // If you want to filter out some fields - if config.KibanaFilter != "" { - if !strings.Contains(v.Name, config.KibanaFilter) { - fields, err := json.Marshal(v) - check(err) - stringFields := string(fields) - payload.Attributes.Fields = fmt.Sprintf("%s%s,", payload.Attributes.Fields, stringFields) - } - } else { - fields, err := json.Marshal(v) - check(err) - stringFields := string(fields) - payload.Attributes.Fields = fmt.Sprintf("%s%s,", payload.Attributes.Fields, stringFields) - } - } - payload.Attributes.Fields = strings.TrimRight(payload.Attributes.Fields, ",") - payload.Attributes.Fields = fmt.Sprintf("%s]", payload.Attributes.Fields) - - jsonNewData, err := json.Marshal(payload) - if err != nil { - fmt.Println(err) - } - - body = bytes.NewReader(jsonNewData) - - return body -} - func curl(config Config, url, requestMethod string) (bodyBytes []byte) { cfg := &tls.Config{ InsecureSkipVerify: false, @@ -73,7 +31,7 @@ func curl(config Config, url, requestMethod string) (bodyBytes []byte) { check(err) defer resp.Body.Close() - bodyBytes, err = ioutil.ReadAll(resp.Body) + bodyBytes, err = io.ReadAll(resp.Body) check(err) if resp.StatusCode != 200 { fmt.Println(string(bodyBytes)) @@ -82,19 +40,7 @@ func curl(config Config, url, requestMethod string) (bodyBytes []byte) { } -func getFields(config Config) (fieldList *FieldList) { - - url := fmt.Sprintf("https://%s/api/index_patterns/_fields_for_wildcard?pattern=%s&meta_fields=_source&meta_fields=_id&meta_fields=_type&meta_fields=_index&meta_fields=_score", config.KibanaURL, config.KibanaIndex) - fieldList = &FieldList{} - - bodyBytes := curl(config, url, "GET") - - json.Unmarshal(bodyBytes, fieldList) - - return fieldList -} - -func submitPayload(config Config, body io.Reader) { +func submitPayload(config Config) { url := fmt.Sprintf("https://%s/api/saved_objects/index-pattern/%s", config.KibanaURL, config.KibanaIndex) curl(config, url, "PUT")