|
| 1 | +package filetype |
| 2 | + |
| 3 | +import ( |
| 4 | + pathpkg "path" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// ParseFileByExtension parses a file based on its file extension. |
| 10 | +// It determines the format from the extension, not the content. |
| 11 | +// Supported extensions: |
| 12 | +// - .json → JSON parsing. |
| 13 | +// - .yaml, .yml → YAML parsing. |
| 14 | +// - .hcl, .tf, .tfvars → HCL parsing. |
| 15 | +// - All others (including .txt or no extension) → raw string. |
| 16 | +func ParseFileByExtension(readFileFunc func(string) ([]byte, error), filename string) (any, error) { |
| 17 | + // Extract clean filename from potential URL. |
| 18 | + cleanFilename := ExtractFilenameFromPath(filename) |
| 19 | + ext := GetFileExtension(cleanFilename) |
| 20 | + |
| 21 | + // Read the file content. |
| 22 | + data, err := readFileFunc(filename) |
| 23 | + if err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + |
| 27 | + // Parse based on extension. |
| 28 | + return ParseByExtension(data, ext, filename) |
| 29 | +} |
| 30 | + |
| 31 | +// ParseFileRaw always returns the file content as a raw string, |
| 32 | +// regardless of the file extension or content. |
| 33 | +func ParseFileRaw(readFileFunc func(string) ([]byte, error), filename string) (any, error) { |
| 34 | + data, err := readFileFunc(filename) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + return string(data), nil |
| 39 | +} |
| 40 | + |
| 41 | +// ParseByExtension parses data based on the provided extension. |
| 42 | +func ParseByExtension(data []byte, ext string, filename string) (any, error) { |
| 43 | + switch ext { |
| 44 | + case ".json": |
| 45 | + return parseJSON(data) |
| 46 | + case ".yaml", ".yml": |
| 47 | + return parseYAML(data) |
| 48 | + case ".hcl", ".tf", ".tfvars": |
| 49 | + return parseHCL(data, filename) |
| 50 | + default: |
| 51 | + // Return as raw string for unknown extensions. |
| 52 | + return string(data), nil |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// ExtractFilenameFromPath extracts the actual filename from a path or URL. |
| 57 | +// It removes query strings and fragments from URLs. |
| 58 | +// Examples: |
| 59 | +// - "https://example.com/file.json?v=1#section" → "file.json". |
| 60 | +// - "/path/to/file.yaml" → "file.yaml". |
| 61 | +// - "file.txt" → "file.txt". |
| 62 | +func ExtractFilenameFromPath(path string) string { |
| 63 | + // Remove fragment (everything after #). |
| 64 | + if idx := strings.Index(path, "#"); idx != -1 { |
| 65 | + path = path[:idx] |
| 66 | + } |
| 67 | + |
| 68 | + // Remove query string (everything after ?). |
| 69 | + if idx := strings.Index(path, "?"); idx != -1 { |
| 70 | + path = path[:idx] |
| 71 | + } |
| 72 | + |
| 73 | + // For URLs, use path package instead of filepath to ensure forward slashes. |
| 74 | + // This handles URLs correctly on Windows where filepath.Base expects backslashes. |
| 75 | + if strings.Contains(path, "://") { |
| 76 | + // Use path package for URLs (always uses forward slashes). |
| 77 | + return pathpkg.Base(path) |
| 78 | + } |
| 79 | + |
| 80 | + // For local files, use filepath package (handles OS-specific separators). |
| 81 | + return filepath.Base(path) |
| 82 | +} |
| 83 | + |
| 84 | +// GetFileExtension returns the lowercase file extension including the dot. |
| 85 | +// Examples: |
| 86 | +// - "file.json" → ".json". |
| 87 | +// - "FILE.JSON" → ".json". |
| 88 | +// - "file.backup.json" → ".json". |
| 89 | +// - "file" → "". |
| 90 | +// - ".hidden" → "". |
| 91 | +func GetFileExtension(filename string) string { |
| 92 | + // Handle special cases. |
| 93 | + if filename == "" || filename == "." { |
| 94 | + return "" |
| 95 | + } |
| 96 | + |
| 97 | + ext := filepath.Ext(filename) |
| 98 | + |
| 99 | + // If the extension is the whole filename (e.g., ".env"), check if it looks like a known extension. |
| 100 | + if ext == filename { |
| 101 | + // Check if it's actually an extension (has letters after the dot). |
| 102 | + if len(ext) > 1 && !strings.Contains(ext[1:], ".") { |
| 103 | + // It looks like an extension file (e.g., ".json", ".yaml"). |
| 104 | + // Check if it's a known extension. |
| 105 | + lowerExt := strings.ToLower(ext) |
| 106 | + knownExts := []string{".json", ".yaml", ".yml", ".hcl", ".tf", ".tfvars", ".txt", ".md"} |
| 107 | + for _, known := range knownExts { |
| 108 | + if lowerExt == known { |
| 109 | + return lowerExt |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + // Otherwise it's a hidden file without an extension (e.g., ".env", ".gitignore"). |
| 114 | + return "" |
| 115 | + } |
| 116 | + |
| 117 | + // If filename ends with a dot, there's no extension. |
| 118 | + if ext == "." { |
| 119 | + return "" |
| 120 | + } |
| 121 | + |
| 122 | + return strings.ToLower(ext) |
| 123 | +} |
0 commit comments