Skip to content

Commit

Permalink
fix : read yml
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Jul 14, 2023
1 parent e6533b7 commit edbef28
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,4 @@ module github.com/seipan/csql

go 1.20

require (
github.com/fatih/color v1.10.0 // indirect
github.com/goccy/go-yaml v1.11.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
require gopkg.in/yaml.v2 v2.4.0
18 changes: 4 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/goccy/go-yaml v1.11.0 h1:n7Z+zx8S9f9KgzG6KtQKf+kwqXZlLNR2F6018Dgau54=
github.com/goccy/go-yaml v1.11.0/go.mod h1:H+mJrWtjPTJAHvRbV09MCK9xYwODM+wRTVFFTWckfng=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
30 changes: 10 additions & 20 deletions yml.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
package csql

import (
"fmt"
"os"

"github.com/goccy/go-yaml/ast"

goyaml "github.com/goccy/go-yaml"
"gopkg.in/yaml.v2"
)

type Yml struct {
dns string `yaml:"dns"`
}

type YmlFile struct {
path string
file *ast.File
}

func (y *YmlFile) ReadYmlFile() (any, error) {
if y.path == "" {
return nil, fmt.Errorf("path is empty")
}
yamlPath, err := goyaml.PathString(y.path)
if err != nil {
return nil, fmt.Errorf("failed to parse path %s: %w", y.path, err)
}

node, err := yamlPath.FilterFile(y.file)
yml := Yml{}
b, err := os.ReadFile(y.path)
if err != nil {
return nil, err
}

var value interface{}
if err := goyaml.Unmarshal([]byte(node.String()), &value); err != nil {
return nil, err
}

return value, nil
yaml.Unmarshal(b, &yml)
return yml, nil
}

0 comments on commit edbef28

Please sign in to comment.