Last active
November 14, 2018 22:22
-
-
Save kardolus/512cbcc49321ffd08e7d8e80ad7bc875 to your computer and use it in GitHub Desktop.
toml_parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[groups] | |
labels = ["nodejs"] | |
buildpacks = [ | |
{ id = "org.cloudfoundry.buildpacks.nodejs", version = '0.0.1' }, | |
{ id = "org.cloudfoundry.buildpacks.npm", version = '0.0.1' } | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"regexp" | |
) | |
func main() { | |
fmt.Println("hello world") | |
fileName := "order.tml" | |
fileBytes, err := ioutil.ReadFile(filepath.Join(fileName)) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Error reading file\n") | |
os.Exit(1) | |
} | |
contents := string(fileBytes) | |
regex := regexp.MustCompile(`id = "([a-zA-Z]+\.[a-zA-Z]+\.[a-zA-Z]+\.[a-zA-Z]+)", version = .{1}([0-9]+\.[0-9]+\.[0-9]+).{1}`) | |
matches := regex.FindAllStringSubmatch(contents, -1) | |
for i := 0; i < len(matches); i++ { | |
if len(matches[i]) > 1 { | |
fmt.Println("id is " + matches[i][1]) | |
fmt.Println("version is " + matches[i][2]) | |
} | |
} | |
fmt.Println(matches) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex='id = "([a-zA-Z]+\.[a-zA-Z]+\.[a-zA-Z]+\.[a-zA-Z]+)", version = .{1}([0-9]+\.[0-9]+\.[0-9]+).{1}' | |
input="$(cat order.tml)" | |
[[ $input =~ $regex ]] | |
echo ${BASH_REMATCH[1]} | |
echo ${BASH_REMATCH[2]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"github.com/BurntSushi/toml" | |
) | |
type format struct { | |
TomlGroups groups `toml:"groups"` | |
} | |
type groups struct { | |
Labels []string `toml:"labels"` | |
Buildpacks []buildpack `toml:"buildpacks"` | |
} | |
type buildpack struct { | |
Id string `toml:"id"` | |
Version string `toml:"version"` | |
} | |
func main() { | |
//fileName := "order.tml" | |
fileContents := ` | |
[groups] | |
labels = ["nodejs"] | |
buildpacks = [ | |
{ id = "org.cloudfoundry.buildpacks.nodejs", version = '0.0.1' }, | |
{ id = "org.cloudfoundry.buildpacks.npm", version = '0.0.1' } | |
]` | |
var tomlContents format | |
if _, err := toml.Decode(fileContents, &tomlContents); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
log.Println(tomlContents.TomlGroups) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment