mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
3 changed files with 0 additions and 54 deletions
@ -1,8 +0,0 @@ |
|||||
module toJSON |
|
||||
|
|
||||
go 1.18 |
|
||||
|
|
||||
require ( |
|
||||
github.com/BurntSushi/toml v1.2.0 |
|
||||
gopkg.in/yaml.v3 v3.0.1 |
|
||||
) |
|
@ -1,6 +0,0 @@ |
|||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= |
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= |
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
|
@ -1,40 +0,0 @@ |
|||||
package main |
|
||||
|
|
||||
import ( |
|
||||
"encoding/json" |
|
||||
"fmt" |
|
||||
"github.com/BurntSushi/toml" |
|
||||
"gopkg.in/yaml.v3" |
|
||||
"os" |
|
||||
) |
|
||||
|
|
||||
func parser(raw []byte) (interface{}, interface{}) { |
|
||||
var object interface{} |
|
||||
if err := json.Unmarshal(raw, &object); err == nil { // try json
|
|
||||
return object, nil // json format
|
|
||||
} |
|
||||
if err := toml.Unmarshal(raw, &object); err == nil { // try toml
|
|
||||
return object, nil // toml format
|
|
||||
} |
|
||||
if err := yaml.Unmarshal(raw, &object); err == nil { // try yaml
|
|
||||
return object, nil // yaml format
|
|
||||
} |
|
||||
return nil, nil // parser error
|
|
||||
} |
|
||||
|
|
||||
func main() { |
|
||||
if len(os.Args) < 2 { // without argument
|
|
||||
fmt.Printf("usage: toJSON [file]\n") |
|
||||
os.Exit(0) |
|
||||
} |
|
||||
raw, err := os.ReadFile(os.Args[1]) |
|
||||
if err != nil { |
|
||||
os.Exit(2) // file open failed
|
|
||||
} |
|
||||
if object, err := parser(raw); err == nil { |
|
||||
ret, _ := json.Marshal(object) |
|
||||
fmt.Println(string(ret)) |
|
||||
os.Exit(0) |
|
||||
} |
|
||||
os.Exit(1) // unmarshal failed
|
|
||||
} |
|
Loading…
Reference in new issue