configuration

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package configuration manages the loading and parsing of task configurations for the K8sBlackPearl project, supporting both JSON and YAML formats. It abstracts the complexity of deserialization and provides a unified interface for accessing task details across various components of the application.

The package defines a Task struct that represents the structure of a task configuration. It includes methods such as LoadTasksFromJSON and LoadTasksFromYAML to facilitate the reading and parsing of configuration files.

The LoadTasks function is exposed to load tasks from a file whose extension determines the format of the tasks to be loaded (either JSON or YAML). This function abstracts away the specific parsing logic and provides a simple interface for loading tasks.

Usage

The LoadTasks function should be used to load task configurations at the start of the application.

Example:

// Loading tasks from a configuration file (JSON or YAML)
tasks, err := configuration.LoadTasks("tasks.json") // .json or .yaml file
if err != nil {
    // Handle error
}

// Application logic using the loaded tasks
for _, task := range tasks {
    fmt.Printf("Task: %+v\n", task)
}

Important Note: Always validate task configurations after loading to prevent issues during runtime. The package is designed to be flexible and extensible, allowing for additional task parameters and types to be easily incorporated into the existing structure.

The choice between JSON and YAML configuration formats is provided to accommodate different user preferences and use cases. Developers can select the format that best suits their operational environment and configuration management practices.

Copyright (c) 2023 H0llyW00dzZ

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDuration added in v0.2.6

func ParseDuration(durationStr string) (time.Duration, error)

ParseDuration converts a duration string into a time.Duration object. It returns an error if the string is empty or if the format is not recognized.

Types

type Task

type Task struct {
	// Name is a unique identifier for the task.
	Name string `json:"name" yaml:"name"`
	// ShipsNamespace specifies the Kubernetes namespace in which the task is relevant.
	ShipsNamespace     string        `json:"shipsNamespace" yaml:"shipsNamespace"`
	MaxRetries         int           `json:"maxRetries" yaml:"maxRetries"`
	RetryDelay         string        `json:"retryDelay" yaml:"retryDelay"` // Original string from JSON/YAML
	RetryDelayDuration time.Duration // Parsed duration
	// Type indicates the kind of operation this task represents, such as "GetPods" or "CrewWriteLabelPods".
	Type string `json:"type" yaml:"type"`
	// Parameters is a map of key-value pairs that provide additional details required to execute the task.
	Parameters map[string]interface{} `json:"parameters" yaml:"parameters"`
}

Task represents a unit of work that is to be processed by the system. Each Task is defined with a unique name, a designated Kubernetes namespace, a type that determines how the task will be executed, and a set of parameters that provide context for task execution.

func LoadTasks added in v0.2.3

func LoadTasks(filePath string) ([]Task, error)

LoadTasks determines the file extension of the provided filePath and calls the appropriate function to load tasks from either a JSON or YAML file. It supports .json, .yaml, and .yml file extensions and returns an error if the file extension is unsupported.

filePath is the path to the configuration file containing an array of task definitions.

This function is a convenience wrapper that allows the application to load task configurations without needing to specify the file format explicitly.

func LoadTasksFromJSON

func LoadTasksFromJSON(filePath string) ([]Task, error)

LoadTasksFromJSON reads a JSON file from the provided file path, unmarshals it into a slice of Task structs, and returns them. It handles file reading errors and JSON unmarshalling errors by returning an error.

filePath is the path to the JSON file containing an array of task definitions.

This function is particularly useful when initializing tasks from a configuration file in a JSON format at the start of an application.

func LoadTasksFromYAML

func LoadTasksFromYAML(filePath string) ([]Task, error)

LoadTasksFromYAML reads a YAML file from the provided file path, unmarshals it into a slice of Task structs, and returns them. It handles file reading errors and YAML unmarshalling errors by returning an error.

filePath is the path to the YAML file containing an array of task definitions.

Use this function to load task configurations from YAML files, which are often preferred for their readability and ease of use in configuration management.

Jump to

Keyboard shortcuts

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