当前位置:网站首页>golang编写程序时,shell在子目录批量执行go mod tidy

golang编写程序时,shell在子目录批量执行go mod tidy

2020-12-07 09:56:27 osc_vrvcjh1l

最近在go的项目里,用到了mod,但目录太多了,有时有的依赖用了后来又废弃了,后来发现go.sum go.mod文件内容好大,了解到go mod tidy可以完成完成检测依赖添加移除工作,于是就执行了,但目录太多了,真的很麻烦,于是乎,写了个shell脚本来批量做这个事...

chmod +x tidy.sh

./tidy.sh

 

#!/usr/bin/env bash
###########################################################
#Author:mengdj@outlook.com
#Created Time:2020.12.04 11:56
#Description:execute go mod tidy in current directory
#Version:0.0.2
#File:tidy.sh
###########################################################

CURRENT_DIR=$(pwd)
SEARCH_DIR=$CURRENT_DIR
SEARCH_TOTAL=0
EXECUTE_CMD="go mod tidy"

function GoTidy() {
	for file in $(ls $1); do
		local target="$1/$file"
		if [ -d $target ]; then
			cd $target
			#case
			if [ -f "go.mod" ];then
				`$EXECUTE_CMD`
				if [ $? -ne 0 ]; then
					break
				fi
				echo "process $target"
				let "SEARCH_TOTAL+=1"
			fi
			GoTidy $target
		fi
	done
}

#test
if [ $# -ne 0 ]; then
	if [ -d $1 ]; then
		cd $1
		SEARCH_DIR=$(pwd)
	else
		echo "$1 is not exist directory."
		exit
	fi
fi
#start
GoTidy $SEARCH_DIR
echo "processed($SEARCH_TOTAL)."
#back directory
cd $CURRENT_DIR

 

版权声明
本文为[osc_vrvcjh1l]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4268886/blog/4777434