utils

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 15 Imported by: 0

README

Go Utils

Overview

go-utils is a collection of DiPhyx utility functions.

Installation

To install go-utils, use the following command:

go get github.com/diphyx/go-utils

Usage

Import the package in your Go code:

import "github.com/diphyx/go-utils"
Encryption

The Encryption struct provides methods for encoding, decoding, encrypting, and decrypting strings.

Example
package main

import (
    "fmt"
    "github.com/diphyx/go-utils"
)

func main() {
    encryption, encryptionError := utils.NewEncryption("ABCDEFGHIJKLMNOPQRSTUVWX12345678", "1234567890123456")
    if encryptionError != nil {
        fmt.Println("Error creating encryption:", encryptionError)

        return
    }

    encoded, encodeError := encryption.Encode("Sample")
    if encodeError != nil {
        fmt.Println("Error encoding:", encodeError)

        return
    }

    decoded, decodeError := encryption.Decode(encoded)
    if decodeError != nil {
        fmt.Println("Error decoding:", decodeError)

        return
    }

    fmt.Println("Encoded:", encoded)
    fmt.Println("Decoded:", decoded)

    encrypted, encryptError := encryption.Encrypt("Sample")
    if encryptError != nil {
        fmt.Println("Error encrypting:", encryptError)

        return
    }

    decrypted, decryptError := encryption.Decrypt(encrypted)
    if decryptError != nil {
        fmt.Println("Error decrypting:", decryptError)

        return
    }

    fmt.Println("Encrypted:", encrypted)
    fmt.Println("Decrypted:", decrypted)
}
IP Conversion

The IpToNumber and NumberToIp functions convert IPv4 addresses to their integer representations and vice versa.

Example
package main

import (
    "fmt"
    "github.com/diphyx/go-utils"
)

func main() {
    ip := "192.168.0.1"
    number, convertToNumberError := utils.IpToNumber(ip)
    if convertToNumberError != nil {
        fmt.Println("Error converting IP to number:", convertToNumberError)

        return
    }

    fmt.Println("IP to Number:", number)

    ip, convertToIpError = utils.NumberToIp(number)
    if convertToIpError != nil {
        fmt.Println("Error converting number to IP:", convertToIpError)

        return
    }

    fmt.Println("Number to IP:", ip)
}
Secret Generation

The NewSecret function generates a new secret string with the given prefix.

Example
package main

import (
    "fmt"
    "github.com/diphyx/go-utils"
)

func main() {
    secret, secretError := utils.NewSecret("prefix_")
    if secretError != nil {
        fmt.Println("Error generating secret:", secretError)

        return
    }

    fmt.Println("Generated Secret:", secret)
}
YAML Template Rendering

The RenderYamlTemplate function renders a YAML template with the given variables.

Example
package main

import (
    "fmt"
    "github.com/diphyx/go-utils"
)

func main() {
    template := "PLACEHOLDER:\n    default: placeholder\n    required: true\n#---\nname: {{ PLACEHOLDER }}"

    _, parseError := utils.ParseYamlTemplate(template)
    if parseError != nil {
        fmt.Println("Error parsing YAML template:", parseError)

        return
    }

    variables := map[string]string{
        "PLACEHOLDER": "placeholder",
    }

    rendered, renderError := utils.RenderYamlTemplate(template, variables)
    if renderError != nil {
        fmt.Println("Error rendering YAML template:", renderError)

        return
    }

    fmt.Println("Rendered YAML Template:", rendered)
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IpToNumber

func IpToNumber(input string) (int64, error)

IpToNumber converts an IPv4 address string to its integer representation.

func NewSecret

func NewSecret(prefix string) (string, error)

NewSecret generates a new secret string with the given prefix.

func NumberToIp

func NumberToIp(input int64) (string, error)

NumberToIp converts an integer representation of an IPv4 address back to its string form.

Types

type Encryption

type Encryption struct {
	// contains filtered or unexported fields
}

func NewEncryption

func NewEncryption(encoder string, secret string) (*Encryption, error)

NewEncryption creates a new Encryption instance with the given encoder and secret.

func (*Encryption) Decode

func (encryption *Encryption) Decode(input string) (string, error)

Decode decodes the given input string using the encoder.

func (*Encryption) Decrypt

func (encryption *Encryption) Decrypt(input string) (string, error)

Decrypt decrypts the given input string using the secret.

func (*Encryption) Encode

func (encryption *Encryption) Encode(input string) (string, error)

Encode encodes the given input string using the encoder.

func (*Encryption) Encrypt

func (encryption *Encryption) Encrypt(input string) (string, error)

Encrypt encrypts the given input string using the secret.

type YamlTemplate added in v0.2.0

type YamlTemplate struct {
	Content  string                          `json:"content"`
	Metadata map[string]YamlTemplateMetadata `json:"metadata"`
}

func ParseYamlTemplate added in v0.2.0

func ParseYamlTemplate(input string) (*YamlTemplate, error)

ParseYamlTemplate parses a YAML template string into a YamlTemplate struct.

func RenderYamlTemplate added in v0.2.0

func RenderYamlTemplate(input string, variables map[string]string) (*YamlTemplate, error)

RenderYamlTemplate renders a YAML template with the provided variables.

type YamlTemplateMetadata added in v0.2.0

type YamlTemplateMetadata struct {
	Default  string `yaml:"default"`
	Required bool   `yaml:"required"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL