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 ¶
- Variables
- func DetectFormatAndParseFile(readFileFunc func(string) ([]byte, error), filename string) (any, error)
- func ExtractFilenameFromPath(path string) string
- func GetFileExtension(filename string) string
- func IsHCL(data string) bool
- func IsJSON(data string) bool
- func IsYAML(data string) bool
- func ParseByExtension(data []byte, ext string, filename string) (any, error)
- func ParseFileByExtension(readFileFunc func(string) ([]byte, error), filename string) (any, error)
- func ParseFileRaw(readFileFunc func(string) ([]byte, error), filename string) (any, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
ExtractFilenameFromPath extracts the actual filename from a path or URL. It removes query strings and fragments from URLs. Examples:
- "https://example.com/file.json?v=1#section" → "file.json".
- "/path/to/file.yaml" → "file.yaml".
- "file.txt" → "file.txt".
func GetFileExtension ¶ added in v1.191.0
GetFileExtension returns the lowercase file extension including the dot. Examples:
- "file.json" → ".json".
- "FILE.JSON" → ".json".
- "file.backup.json" → ".json".
- "file" → "".
- ".hidden" → "".
func IsHCL ¶
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 ¶
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 ¶
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
ParseByExtension parses data based on the provided extension.
func ParseFileByExtension ¶ added in v1.191.0
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.
Types ¶
This section is empty.