duan0403788996 2019-05-30 09:57
浏览 96
已采纳

一键构建目录下的多个go插件

I have a list of plugins under a subdir. I want to build all of them with a single command. There is an example dir layout.

plugins/cat/cat.go
plugins/dog/dog.go
plugins/cow/cow.go

I build them right now like:

go build -i -buildmode=plugin -o build/cat.so plugins/cat/cat.go
go build -i -buildmode=plugin -o build/dog.so plugins/dog/dog.go
go build -i -buildmode=plugin -o build/cow.so plugins/cow/cow.go

There is an other command (because it's in a Makefile), which help me to get closer:

# Ex.: make bin-so TARGET=cat
bin-so: builddir
    go build -i -buildmode=plugin -o build/$(TARGET).so plugins/$(TARGET)/$(TARGET).go

I want to create a single line which builds these plugins. I found out how I can list the folder names, but I have to use it somehow in the command above.

find ./plugins -mindepth 1 -maxdepth 1 -type d | awk 'sub(/^.*\//, "")'

So it will list the name of the folders, these are good to me, but I have to redirect it to the plugin builder command.

I want to have something similar (just an example):

find ./plugins -mindepth 1 -maxdepth 1 -type d | awk 'sub(/^.*\//, "")' | go build -i -buildmode=plugin -o build/$1).so plugins/$1/$1.go
  • 写回答

1条回答 默认 最新

  • dqw7121 2019-05-30 10:06
    关注

    You can just do this in one find command with the -execdir option that allows you run commands directly on the basename of the files

    find ./plugins -mindepth 1 -maxdepth 1 -type d -execdir bash -c '
        for arg; do
            name="${arg##*./}"
            go build -i -buildmode=plugin -o build/"${name}".so plugins/"${name}"/"${name}".go
        done' _ {} +
    

    This is much better than using multiple pipelines after find to achieve the same result. Imagine the part within the sh -c '..' as a separate script and you are passing the arguments to the script with the names returned ./cow.go etc.

    The advantage of -execdir here is you don't need to worry about the immediate paths before the directory names. You get the final base-name of the directories found.

    See Understanding the -exec option of find on Unix.SE to learn more about each of the options used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog