From aaad56aee47a32d42e691f7bd52a576e8a63dba1 Mon Sep 17 00:00:00 2001 From: Ignacio Horcada Bernal Date: Tue, 28 Feb 2023 09:28:57 +0100 Subject: [PATCH 1/2] Data parser [DNM] --- pkg/data_parser/data_parser.go | 81 ++++++++++++++++++++++++++++++++++ pkg/data_parser/parseador.go | 64 +++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 pkg/data_parser/data_parser.go create mode 100644 pkg/data_parser/parseador.go diff --git a/pkg/data_parser/data_parser.go b/pkg/data_parser/data_parser.go new file mode 100644 index 0000000..4aaee21 --- /dev/null +++ b/pkg/data_parser/data_parser.go @@ -0,0 +1,81 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This is a reimplementation of the amazing work done by Mathew Topol. +// Thanks to his work, reading parquet files was made incredibly easy. + +package parseador + +import ( + "bufio" + "encoding/json" + "os" + "path/filepath" + "strings" + + "github.com/sirupsen/logrus" +) + +// ParseLines reads the contents of a file and returns the lines that contain a certain element +func ParseLines(file string, element string) ([]string, error) { + var lines []string + + // Open the file + f, err := os.Open(filepath.Clean(file)) + if err != nil { + return lines, err + } + defer func() { + if err := f.Close(); err != nil { + logrus.Printf("Error closing file: %s\n", err) + } + }() + + // Read the file line by line + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + + // Check if the line contains the element + if strings.Contains(line, element) { + if !strings.HasPrefix(line, "#") { + lines = append(lines, line) + } + } + } + + if err := scanner.Err(); err != nil { + return lines, err + } + + return lines, nil +} + +func OutputData(file string, data []string) error { + // Marshal the data into JSON + bytes, err := json.Marshal(data) + if err != nil { + return err + } + + // Write the JSON to the file + err = os.WriteFile(file, bytes, 0600) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/data_parser/parseador.go b/pkg/data_parser/parseador.go new file mode 100644 index 0000000..d827aec --- /dev/null +++ b/pkg/data_parser/parseador.go @@ -0,0 +1,64 @@ +package parseador + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "path/filepath" + "strings" + + "github.com/sirupsen/logrus" +) + +type Config struct { + Connection string `json:"connection"` + Enabled_Backends []string `json:"enabled_backends"` + NumberOfBackends string `json:"numberofbackends"` +} + +func Parseador(file string) { + // Read the contents of the configuration file into a byte slice + data, err := ioutil.ReadFile(filepath.Clean(file)) + if err != nil { + logrus.Fatal("Can't read file", err) + } + + // Split the byte slice into a slice of strings, with each string representing a line in the file + lines := strings.Split(string(data), "\n") + logrus.Info("Lines split in slice of strings") + logrus.Info("Number of lines: ", len(lines)) + + // Initialize an empty Config struct + var config Config + + // Iterate through the lines in the file + for _, line := range lines { + //logrus.Info("Untreated lines: ", lines) + + // Skip empty lines + if strings.HasPrefix(line, "#") || !strings.Contains(line, "=") { + continue + } + // Split the line into a key-value pair + parts := strings.Split(line, "=") + logrus.Info(line, " --- ", "output: ", parts) + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + + // Set the appropriate field in the Config struct based on the key + var x int + switch key { + case "enabled_backends": + config.Enabled_Backends = append(config.Enabled_Backends, value) + config.NumberOfBackends = fmt.Sprint(x + 1) + } + } + + // Marshal the Config struct into a JSON byte slice + configJSON, err := json.MarshalIndent(config, "", " ") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(configJSON)) +} From adffaea188254eb90376f623f2ed3f20b6c6fc2d Mon Sep 17 00:00:00 2001 From: Ignacio Horcada Bernal Date: Tue, 28 Feb 2023 09:39:52 +0100 Subject: [PATCH 2/2] Fixed static check --- pkg/data_parser/parseador.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/data_parser/parseador.go b/pkg/data_parser/parseador.go index d827aec..841b027 100644 --- a/pkg/data_parser/parseador.go +++ b/pkg/data_parser/parseador.go @@ -3,8 +3,8 @@ package parseador import ( "encoding/json" "fmt" - "io/ioutil" "log" + "os" "path/filepath" "strings" @@ -19,7 +19,7 @@ type Config struct { func Parseador(file string) { // Read the contents of the configuration file into a byte slice - data, err := ioutil.ReadFile(filepath.Clean(file)) + data, err := os.ReadFile(filepath.Clean(file)) if err != nil { logrus.Fatal("Can't read file", err) }