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 ¶
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 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
Types ¶
This section is empty.