将一个 YAML 文件中的值分配给 Makefile 内的变量。

huangapple go评论60阅读模式
英文:

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

问题

I have a config file called conf.yaml 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 ."" conf.yaml)
endef

define extract_from_yaml2
	$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" 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. It seems that 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 prettiest way to do so.

英文:

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 ."" conf.yaml)
endef

define extract_from_yaml2
	$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" 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.

答案1

得分: 1

以下是翻译好的部分:

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 ."" conf.yaml)
endef

define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" 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}
英文:

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 ."" conf.yaml)
endef

define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" 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}

huangapple
  • 本文由 发表于 2023年2月24日 04:05:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定