| 1 | package dataformat |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | type DataFormat string |
| 9 | |
| 10 | const ( |
| 11 | JSON DataFormat = "json" |
| 12 | YAML DataFormat = "yaml" |
| 13 | XML DataFormat = "xml" |
| 14 | INI DataFormat = "ini" |
| 15 | CNF DataFormat = "cnf" |
| 16 | CFG DataFormat = "cfg" |
| 17 | ENV DataFormat = "env" |
| 18 | Properties DataFormat = "properties" |
| 19 | Command DataFormat = "command" |
| 20 | ) |
| 21 | |
| 22 | func FromPath(path string) DataFormat { |
| 23 | extension := strings.ToLower(filepath.Ext(path)) |
| 24 | switch extension { |
| 25 | case ".json": |
| 26 | return JSON |
| 27 | case ".yaml", ".yml": |
| 28 | return YAML |
| 29 | case ".xml": |
| 30 | return XML |
| 31 | case ".ini": |
| 32 | return INI |
| 33 | case ".cnf", ".conf", ".cf": |
| 34 | return CNF |
| 35 | case ".cfg": |
| 36 | return CFG |
| 37 | case ".env": |
| 38 | return ENV |
| 39 | case ".properties": |
| 40 | return Properties |
| 41 | } |
| 42 | return "" |
| 43 | } |