Search code examples
bashmakefileyq

how to assign a value from a yaml file to a variable inside makefile


I have a config file called conf.yaml

run:
  mode: test
  model_version: v1.0.0

inside a Makefile I am using yq to parse the conf file and assign values

here is the makefile

define extract_from_yaml
    $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."${1}" conf.yaml)
endef

define extract_from_yaml2
    $(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."${1}" conf.yaml))
endef

MODEL_VERSION := $(call extract_from_yaml,run.model_version)
MODEL_PATH := src/sad/models/${MODEL_VERSION}/dir

MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir


MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir

mytest:
    ${MODEL_VERSION}

mytest2:
    ${MODEL_PATH}

mytest3:
    ${MODEL_PATH2}

mytest4:
    ${MODEL_PATH3}

if I do make mytest the output is as expected and will be v1.0.0 however if I do make mytest2 the output is "src/sad/models/ v1.0.0/dir" I don't understand where those white spaces come from. so seems the mytest3 is the solution where I remove the white space using $(strip).

it is annoyingly unbelievable that if I move the $(strip) inside the function still those whitespaces stay. you can see that by running make mytest4

so I wonder what is the pretties way to do so.


Solution

  • The problem lies in spaces in extract_from_yaml.

    Try this :

    define extract_from_yaml
    $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."${1}" conf.yaml)
    endef
    
    define extract_from_yaml2
    $(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."${1}" conf.yaml))
    endef
    
    MODEL_VERSION:=$(call extract_from_yaml2,run.model_version)
    MODEL_PATH:=src/sad/models/${MODEL_VERSION}/dir
    
    MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
    MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir
    
    
    MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
    MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir
    
    mytest:
        echo ${MODEL_VERSION}
    
    mytest2:
        echo ${MODEL_PATH}
    
    mytest3:
        echo ${MODEL_PATH2}
    
    mytest4:
        echo ${MODEL_PATH3}