Help us shape the future of Go! Take the 2026 Go Developer Survey

filetype

package
v1.192.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Example (FormatDetection)

Example_formatDetection shows how to detect and handle different file formats.

package main

import (
	"fmt"

	"github.com/cloudposse/atmos/pkg/filetype"
)

func main() {
	inputs := []struct {
		name    string
		content string
	}{
		{name: "config.yaml", content: "key: value"},
		{name: "config.json", content: `{"key": "value"}`},
		{name: "config.hcl", content: `key = "value"`},
		{name: "readme.txt", content: "Plain text content"},
	}

	for _, input := range inputs {
		switch {
		case filetype.IsJSON(input.content):
			fmt.Printf("%s is JSON\n", input.name)
		case filetype.IsYAML(input.content):
			fmt.Printf("%s is YAML\n", input.name)
		case filetype.IsHCL(input.content):
			fmt.Printf("%s is HCL\n", input.name)
		default:
			fmt.Printf("%s is plain text\n", input.name)
		}
	}
}
Output:
config.yaml is YAML
config.json is JSON
config.hcl is HCL
readme.txt is plain text

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrFailedToProcessHclFile = errors.New("failed to process HCL file")

Functions

func DetectFormatAndParseFile

func DetectFormatAndParseFile(readFileFunc func(string) ([]byte, error), filename string) (any, error)

DetectFormatAndParseFile detects the format of the file (JSON, YAML, HCL) and parses the file into a Go type. For all other formats, it just reads the file and returns the content as a string.

Example

ExampleDetectFormatAndParseFile demonstrates automatic format detection and parsing.

package main

import (
	"fmt"
	"os"

	"github.com/cloudposse/atmos/pkg/filetype"
)

func main() {
	// Create a mock file reader for demonstration.
	mockFileReader := func(filename string) ([]byte, error) {
		// In a real scenario, this would read from the file system.
		if filename == "config.json" {
			return []byte(`{"setting": "value", "enabled": true}`), nil
		}
		return nil, os.ErrNotExist
	}

	// Parse the file with automatic format detection.
	result, err := filetype.DetectFormatAndParseFile(mockFileReader, "config.json")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}

	// The result is parsed into appropriate Go types.
	if config, ok := result.(map[string]any); ok {
		fmt.Printf("Setting: %v\n", config["setting"])
		fmt.Printf("Enabled: %v\n", config["enabled"])
	}
}
Output:
Setting: value
Enabled: true

func ExtractFilenameFromPath added in v1.191.0

func ExtractFilenameFromPath(path string) string

ExtractFilenameFromPath extracts the actual filename from a path or URL. It removes query strings and fragments from URLs. Examples:

func GetFileExtension added in v1.191.0

func GetFileExtension(filename string) string

GetFileExtension returns the lowercase file extension including the dot. Examples:

  • "file.json" → ".json".
  • "FILE.JSON" → ".json".
  • "file.backup.json" → ".json".
  • "file" → "".
  • ".hidden" → "".

func IsHCL

func IsHCL(data string) bool

IsHCL checks if data is in HCL format.

Example

ExampleIsHCL demonstrates how to check if a string is valid HCL.

package main

import (
	"fmt"

	"github.com/cloudposse/atmos/pkg/filetype"
)

func main() {
	input := `
name = "example"
values = ["one", "two"]
metadata = {
  version = "1.0"
}
`
	if filetype.IsHCL(input) {
		fmt.Println("Valid HCL")
	} else {
		fmt.Println("Invalid HCL")
	}
}
Output:
Valid HCL

func IsJSON

func IsJSON(data string) bool

IsJSON checks if data is in JSON format.

Example

ExampleIsJSON demonstrates how to check if a string is valid JSON.

package main

import (
	"fmt"

	"github.com/cloudposse/atmos/pkg/filetype"
)

func main() {
	input := `{
  "name": "example",
  "values": ["one", "two"],
  "metadata": {
    "version": "1.0"
  }
}`
	if filetype.IsJSON(input) {
		fmt.Println("Valid JSON")
	} else {
		fmt.Println("Invalid JSON")
	}
}
Output:
Valid JSON

func IsYAML

func IsYAML(data string) bool

IsYAML checks if data is in YAML format.

Example

ExampleIsYAML demonstrates how to check if a string is valid YAML.

package main

import (
	"fmt"

	"github.com/cloudposse/atmos/pkg/filetype"
)

func main() {
	input := `
name: example
values:
  - one
  - two
metadata:
  version: 1.0
`
	if filetype.IsYAML(input) {
		fmt.Println("Valid YAML")
	} else {
		fmt.Println("Invalid YAML")
	}
}
Output:
Valid YAML

func ParseByExtension added in v1.191.0

func ParseByExtension(data []byte, ext string, filename string) (any, error)

ParseByExtension parses data based on the provided extension.

func ParseFileByExtension added in v1.191.0

func ParseFileByExtension(readFileFunc func(string) ([]byte, error), filename string) (any, error)

ParseFileByExtension parses a file based on its file extension. It determines the format from the extension, not the content. Supported extensions: - .json → JSON parsing. - .yaml, .yml → YAML parsing. - .hcl, .tf, .tfvars → HCL parsing. - All others (including .txt or no extension) → raw string.

func ParseFileRaw added in v1.191.0

func ParseFileRaw(readFileFunc func(string) ([]byte, error), filename string) (any, error)

ParseFileRaw always returns the file content as a raw string, regardless of the file extension or content.

Types

This section is empty.

Jump to

Keyboard shortcuts

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