Skip to content

Instantly share code, notes, and snippets.

@kardolus
Last active November 14, 2018 22:22
Show Gist options
  • Save kardolus/512cbcc49321ffd08e7d8e80ad7bc875 to your computer and use it in GitHub Desktop.
Save kardolus/512cbcc49321ffd08e7d8e80ad7bc875 to your computer and use it in GitHub Desktop.
toml_parsing
[groups]
labels = ["nodejs"]
buildpacks = [
{ id = "org.cloudfoundry.buildpacks.nodejs", version = '0.0.1' },
{ id = "org.cloudfoundry.buildpacks.npm", version = '0.0.1' }
]
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)
}
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]}
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