Skip to content

Instantly share code, notes, and snippets.

@Joxebus
Last active July 5, 2024 18:24
Show Gist options
  • Select an option

  • Save Joxebus/6ba83b3aab0c5498daf990b9ede10b19 to your computer and use it in GitHub Desktop.

Select an option

Save Joxebus/6ba83b3aab0c5498daf990b9ede10b19 to your computer and use it in GitHub Desktop.

Revisions

  1. Joxebus revised this gist Jan 31, 2021. 6 changed files with 71 additions and 95 deletions.
    25 changes: 11 additions & 14 deletions 1_yaml_slurper_sample.groovy
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,17 @@ import groovy.yaml.YamlSlurper
    def slurper = new YamlSlurper()

    def configuration = '''
    language: groovy
    sudo: required
    dist: trusty
    matrix:
    include:
    # - jdk: oraclejdk11
    - jdk: openjdk10
    - jdk: oraclejdk9
    - jdk: oraclejdk8
    before_script:
    - |
    unset _JAVA_OPTIONS
    version: 3.0
    environment: "dev"
    context:
    path: "/test"
    endpoints:
    url: "/people"
    operations:
    POST: "create"
    GET: "list or get"
    PUT: "/update"
    DELETE: "delete"
    '''

    def yaml = slurper.parseText(configuration)
    53 changes: 0 additions & 53 deletions 2_read_and_parse_into_and_specific_object.groovy
    Original file line number Diff line number Diff line change
    @@ -1,53 +0,0 @@
    import groovy.yaml.*
    import groovy.transform.*

    @Canonical
    class Service {
    String image
    String containerName
    List environment
    List ports
    List volumes
    }

    String dockerCompose = """
    version: '3'
    services:
    postgres:
    image: postgres:11.5
    containerName: postgresdb
    environment:
    - POSTGRES_USER=mses
    - POSTGRES_PASSWORD=t3cM0n
    - POSTGRES_DB=mses
    ports:
    - '5432:5432'
    volumes:
    - ./postgres-data:/var/lib/postgresql/data
    redis:
    containerName: mses-redis
    image: 'bitnami/redis:5.0'
    environment:
    # ALLOW_EMPTY_PASSWORD is recommended only for development.
    - ALLOW_EMPTY_PASSWORD=yes
    - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
    ports:
    - 6379:6379
    volumes:
    - 'redis_data:/bitnami/redis/data'
    volumes:
    redis_data:
    driver: local
    """

    def config = new YamlSlurper().parseText(dockerCompose)

    Service postgres = new Service(config.services.postgres)
    Service redis = new Service(config.services.redis)

    println postgres
    println redis


    17 changes: 17 additions & 0 deletions 2_yaml_builder_sample.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import groovy.yaml.YamlBuilder

    def yaml = new YamlBuilder()

    yaml {
    version 3.0
    environment "dev"
    context {
    path "/test"
    }
    endpoints {
    url "/people"
    operations ("POST":"create", "GET":"list or get", "PUT":"update", "DELETE":"delete")
    }
    }

    println yaml
    28 changes: 0 additions & 28 deletions 3_generate_yaml_file_with_YamlBuilder.groovy
    Original file line number Diff line number Diff line change
    @@ -1,28 +0,0 @@
    import groovy.yaml.*
    import groovy.transform.*

    @Canonical
    class Service {
    String image
    String containerName
    List environment
    List ports
    List volumes
    }

    Map configuration = [
    version: '3',
    services: [
    postgres : new Service("postgres:11.5", "postgresdb"),
    redis : new Service("bitnami/redis:5.0", "redisdb")
    ],
    volumes: [
    redis_data: [driver:'local']
    ]

    ]

    def yaml = new YamlBuilder()
    yaml(configuration)
    println yaml

    17 changes: 17 additions & 0 deletions 3_yaml_builder_from_map.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import groovy.yaml.YamlBuilder

    Map configuration = [
    version : 3.0,
    environment : "dev",
    context : [path:"/test"],
    endpoints : [url:"/people",
    operations: [POST:"create", GET:"list or get",
    PUT:"update", DELETE:"delete"]
    ]
    ]

    def yaml = new YamlBuilder()

    yaml(configuration)

    println yaml
    26 changes: 26 additions & 0 deletions 4_yaml_builder_from_object.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import groovy.yaml.YamlBuilder

    class Configuration {
    BigDecimal version
    String environment
    Map context
    Map endpoints
    }

    def configuration = new Configuration()

    configuration.with{
    version = 3.0
    environment = "dev"
    context = [path:"/test"]
    endpoints = [url:"/people",
    operations: [POST:"create", GET:"list or get",
    PUT:"update", DELETE:"delete"]
    ]
    }

    def yaml = new YamlBuilder()

    yaml(configuration)

    println yaml
  2. Joxebus revised this gist Jan 18, 2021. 4 changed files with 81 additions and 1 deletion.
    File renamed without changes.
    53 changes: 53 additions & 0 deletions 2_read_and_parse_into_and_specific_object.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    import groovy.yaml.*
    import groovy.transform.*

    @Canonical
    class Service {
    String image
    String containerName
    List environment
    List ports
    List volumes
    }

    String dockerCompose = """
    version: '3'
    services:
    postgres:
    image: postgres:11.5
    containerName: postgresdb
    environment:
    - POSTGRES_USER=mses
    - POSTGRES_PASSWORD=t3cM0n
    - POSTGRES_DB=mses
    ports:
    - '5432:5432'
    volumes:
    - ./postgres-data:/var/lib/postgresql/data
    redis:
    containerName: mses-redis
    image: 'bitnami/redis:5.0'
    environment:
    # ALLOW_EMPTY_PASSWORD is recommended only for development.
    - ALLOW_EMPTY_PASSWORD=yes
    - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
    ports:
    - 6379:6379
    volumes:
    - 'redis_data:/bitnami/redis/data'
    volumes:
    redis_data:
    driver: local
    """

    def config = new YamlSlurper().parseText(dockerCompose)

    Service postgres = new Service(config.services.postgres)
    Service redis = new Service(config.services.redis)

    println postgres
    println redis


    28 changes: 28 additions & 0 deletions 3_generate_yaml_file_with_YamlBuilder.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import groovy.yaml.*
    import groovy.transform.*

    @Canonical
    class Service {
    String image
    String containerName
    List environment
    List ports
    List volumes
    }

    Map configuration = [
    version: '3',
    services: [
    postgres : new Service("postgres:11.5", "postgresdb"),
    redis : new Service("bitnami/redis:5.0", "redisdb")
    ],
    volumes: [
    redis_data: [driver:'local']
    ]

    ]

    def yaml = new YamlBuilder()
    yaml(configuration)
    println yaml

    1 change: 0 additions & 1 deletion sample.groovy
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    kjhasdf
  3. Joxebus created this gist Dec 19, 2020.
    1 change: 1 addition & 0 deletions sample.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    kjhasdf
    28 changes: 28 additions & 0 deletions yaml_slurper_sample.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import groovy.yaml.YamlSlurper

    def slurper = new YamlSlurper()

    def configuration = '''
    language: groovy
    sudo: required
    dist: trusty
    matrix:
    include:
    # - jdk: oraclejdk11
    - jdk: openjdk10
    - jdk: oraclejdk9
    - jdk: oraclejdk8
    before_script:
    - |
    unset _JAVA_OPTIONS
    '''

    def yaml = slurper.parseText(configuration)

    println "YAML keys: ${yaml.keySet()}"

    yaml.keySet().each { key ->
    println "Key: $key \t Type: ${yaml[key].getClass().name}"
    }