csv

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVConfig

type CSVConfig struct {
	// HasHeader indicates if first line is header
	// true: first line has column names
	// false: fixed format (instance_id, cloud, account, region)
	HasHeader bool

	// Delimiter column separator character (default: comma)
	Delimiter rune

	// RequiredFields list of required fields in CSV
	// Parser will validate these fields exist and are not empty
	RequiredFields []string

	// CloudDefault default value for cloud if column doesn't exist
	// Useful for legacy CSVs that didn't have cloud column
	CloudDefault string
}

CSVConfig defines CSV parser configurations. Allows customizing parser behavior.

func DefaultCSVConfig

func DefaultCSVConfig() CSVConfig

DefaultCSVConfig returns default configuration. Format: instance_id,account,region (with header, comma, aws default)

type CSVRecord

type CSVRecord struct {
	InstanceID string            // Instance ID (required)
	Cloud      string            // Provider: aws, azure, gcp (default: aws)
	Account    string            // Account/Subscription/Project (required)
	Region     string            // Instance region (required)
	Extra      map[string]string // Optional extra columns (environment, team, etc)
}

CSVRecord represents a row from the instances CSV file. Each CSV line becomes a CSVRecord.

func (*CSVRecord) ToInstance

func (r *CSVRecord) ToInstance() *cloud.Instance

ToInstance converts CSVRecord to cloud.Instance. This decouples CSV format from internal structure.

type ParseError

type ParseError struct {
	Line    int    // Line number where error occurred
	Column  string // Column name with problem (if applicable)
	Message string // Error message
	Err     error  // Original error (if any)
}

ParseError represents error during CSV parsing

func (*ParseError) Error

func (pe *ParseError) Error() string

Error implements error interface

func (*ParseError) Unwrap

func (pe *ParseError) Unwrap() error

Unwrap allows using errors.Is() and errors.As()

type Parser

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

Parser parses CSV files containing instance information. Supports flexible formats with/without headers and extra columns.

func NewParser

func NewParser(config CSVConfig) *Parser

NewParser creates a new CSV parser with given configuration. If config is empty, uses DefaultCSVConfig().

func (*Parser) ParseFile

func (p *Parser) ParseFile(filePath string) ([]*cloud.Instance, error)

ParseFile reads CSV file and returns list of instances. Validates required fields and converts each row to cloud.Instance.

CSV Format Examples:

With header:

instance_id,account,region,environment
i-123,111111111111,us-east-1,prod

Without header (fixed order):

i-123,111111111111,us-east-1

Returns ParseError with line/column information if validation fails.

func (*Parser) ParseString

func (p *Parser) ParseString(content string) ([]*cloud.Instance, error)

ParseString parses CSV content from a string instead of file. Useful for testing or when CSV content comes from other sources.

Jump to

Keyboard shortcuts

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