dodaj KOD1 i KOD2 do siebie, aby  program mógł konwertować YAML na JSON lub HCL

KOD1:
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
    "strings"

    "github.com/hashicorp/hcl/v2/hclsimple"
    "gopkg.in/yaml.v2"
)

func main() {
    // Get the YAML or HCL file path as a command line argument
    if len(os.Args) < 2 {
        log.Fatal("Please provide the path to the YAML or HCL file as an argument")
    }

    filePath := os.Args[1]

    // Check the file extension
    fileExt := filepath.Ext(filePath)

    if fileExt == ".yaml" || fileExt == ".yml" {
        // Convert YAML to JSON
        jsonData, err := convertYamlToJson(filePath)
        if err != nil {
            log.Fatal(err)
        }

        // Display the converted JSON
        fmt.Println(string(jsonData))

        // Convert YAML to HCL
        hclData, err := convertYamlToHcl(filePath)
        if err != nil {
            log.Fatal(err)
        }

        // Display the converted HCL
        fmt.Println(string(hclData))
    } else if fileExt == ".hcl" {
        // Convert HCL to JSON
        jsonData, err := convertHclToJson(filePath)
        if err != nil {
            log.Fatal(err)
        }

        // Display the converted JSON
        fmt.Println(string(jsonData))
    } else {
        log.Fatal("Unsupported file extension. Supported extensions are .yaml, .yml, and .hcl")
    }
}

func convertYamlToJson(filePath string) ([]byte, error) {
    yamlData, err := ioutil.ReadFile(filePath)
    if err != nil {
        return nil, err
    }

    var data interface{}
    err = yaml.Unmarshal(yamlData, &data)
    if err != nil {
        return nil, err
    }

    jsonData, err := json.Marshal(data)
    if err != nil {
        return nil, err
    }

    return jsonData, nil
}

func convertYamlToHcl(filePath string) ([]byte, error) {
    yamlData, err := ioutil.ReadFile(filePath)
    if err != nil {
        return nil, err
    }

    var data map[interface{}]interface{}
    err = yaml.Unmarshal(yamlData, &data)
    if err != nil {
        return nil, err
    }

    hclData := convertMapToHcl(data, "")
    return hclData, nil
}

func convertMapToHcl(data map[interface{}]interface{}, indent string) []byte {
    hcl := ""
    for key, value := range data {
        if subMap, ok := value.(map[interface{}]interface{}); ok {
            hcl += fmt.Sprintf("%s%s {\n", indent, key)
            hcl += string(convertMapToHcl(subMap, indent+"  "))
            hcl += fmt.Sprintf("%s}\n", indent)
        } else {
            hcl += fmt.Sprintf("%s%s = %v\n", indent, key, value)
        }
    }

    return []byte(hcl)
}

func convertHclToJson(filePath string) ([]byte, error) {
    hclData, err := ioutil.ReadFile(filePath)
    if err != nil {
        return nil, err
    }

    var data map[string]interface{}
    err = hclsimple.Decode(filePath, hclData, nil, &data)
    if err != nil {
        return nil, err
    }

    jsonData, err := json.Marshal(data)
    if err != nil {
        return nil, err
    }

    return jsonData, nil
}

KOD2:
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"

    "gopkg.in/yaml.v2"
)

func main() {
    // Get the YAML file path as a command line argument
    if len(os.Args) < 2 {
        log.Fatal("Please provide the path to the YAML file as an argument")
    }

    filePath := os.Args[1]

    // Check the file extension
    fileExt := filepath.Ext(filePath)

    if fileExt == ".yaml" || fileExt == ".yml" {
        // Convert YAML to JSON
        jsonData, err := convertYamlToJson(filePath)
        if err != nil {
            log.Fatal(err)
        }

        // Display the converted JSON
        fmt.Println(string(jsonData))
    } else {
        log.Fatal("Unsupported file extension. Supported extensions are .yaml and .yml")
    }
}

func convertYamlToJson(filePath string) ([]byte, error) {
    yamlData, err := ioutil.ReadFile(filePath)
    if err != nil {
        return nil, err
    }

    var data interface{}
    err = yaml.Unmarshal(yamlData, &data)
    if err != nil {
        return nil, err
    }

    jsonData, err := json.Marshal(data)
    if err != nil {
        return nil, err
    }

    return jsonData, nil
}