mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-26 21:22:16 +08:00 
			
		
		
		
	fix: fix scripts path module
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
This commit is contained in:
		
							parent
							
								
									be6f189365
								
							
						
					
					
						commit
						18728fc993
					
				
							
								
								
									
										1
									
								
								go.work
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.work
									
									
									
									
									
								
							| @ -5,4 +5,5 @@ use ( | ||||
| 	./tools/changelog | ||||
| 	./tools/infra | ||||
| 	./tools/ncpu | ||||
| 	./tools/yamlfmt | ||||
| ) | ||||
|  | ||||
| @ -88,7 +88,6 @@ func (u *UserApi) GetUsersOnlineStatus(c *gin.Context) { | ||||
| 			log.ZWarn(c, "GetUsersOnlineStatus rpc err", err) | ||||
| 
 | ||||
| 			parseError := apiresp.ParseError(err) | ||||
| 			log.ZDebug(c, "errcode bantanger", "errcode", parseError.ErrCode) | ||||
| 			if parseError.ErrCode == errs.NoPermissionError { | ||||
| 				apiresp.GinError(c, err) | ||||
| 				return | ||||
|  | ||||
| @ -18,9 +18,8 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | ||||
| OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. | ||||
| 
 | ||||
| #Include shell font styles and some basic information | ||||
| source $SCRIPTS_ROOT/style_info.sh | ||||
| source $SCRIPTS_ROOT/lib/init.sh | ||||
| source $SCRIPTS_ROOT/path_info.sh | ||||
| source $SCRIPTS_ROOT/function.sh | ||||
| 
 | ||||
| cd $SCRIPTS_ROOT | ||||
| 
 | ||||
| @ -43,7 +42,7 @@ service_port_name=( | ||||
| ) | ||||
| for i in ${service_port_name[*]}; do | ||||
|   list=$(cat $config_path | grep -w ${i} | awk -F '[:]' '{print $NF}') | ||||
|   openim::util:list-to-string $list | ||||
|   openim::util::list-to-string $list | ||||
|   for j in ${ports_array}; do | ||||
|     port=$(ss -tunlp| grep openim | awk '{print $5}' | grep -w ${j} | awk -F '[:]' '{print $NF}') | ||||
|     if [[ ${port} -ne ${j} ]]; then | ||||
|  | ||||
							
								
								
									
										72
									
								
								scripts/demo.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										72
									
								
								scripts/demo.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,72 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| clear | ||||
| . $(dirname ${BASH_SOURCE})/lib/util.sh | ||||
| 
 | ||||
| openim::util::desc "You can learn a lot about automation using make help" | ||||
| openim::util::run "make help" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "You can learn a lot about automation using make help-all" | ||||
| openim::util::run "make help-all" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Run tidy" | ||||
| openim::util::run "make tidy" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Vendor go.mod" | ||||
| openim::util::run "make vendor" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Code style: fmt, vet, lint" | ||||
| openim::util::run "make style" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Run unit tests" | ||||
| openim::util::run "make test" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Run unit tests and get test coverage" | ||||
| openim::util::run "make cover" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Check for updates to go.mod dependencies" | ||||
| openim::util::run "make updates" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Clean" | ||||
| openim::util::run "make clean" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Generate all necessary files" | ||||
| openim::util::run "make gen" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Generate swagger document" | ||||
| openim::util::run "make swagger" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Serve swagger spec and docs" | ||||
| openim::util::run "make serve-swagger" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Verify the license headers for all files" | ||||
| openim::util::run "make verify-copyright" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Add copyright" | ||||
| openim::util::run "make add-copyright" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Project introduction, become a contributor" | ||||
| openim::util::run "make advertise" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Release the project" | ||||
| openim::util::run "make release" | ||||
| clear | ||||
| 
 | ||||
| openim::util::desc "Run demo" | ||||
| openim::util::run "make demo" | ||||
| clear | ||||
| @ -38,7 +38,7 @@ service_port_name=( | ||||
| 
 | ||||
| for i in ${service_port_name[*]}; do | ||||
|   list=$(cat $config_path | grep -w ${i} | awk -F '[:]' '{print $NF}') | ||||
|   openim::util:list-to-string $list | ||||
|   openim::util::list-to-string $list | ||||
|   for j in ${ports_array}; do | ||||
|     port=$(ss -tunlp| grep openim | awk '{print $5}' | grep -w ${j} | awk -F '[:]' '{print $NF}') | ||||
|     if [[ ${port} -ne ${j} ]]; then | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
| 
 | ||||
| #input:[10023,2323,3434] | ||||
| #output:10023 2323 3434 | ||||
| openim::util:list-to-string(){ | ||||
| openim::util::list-to-string(){ | ||||
|     ports_list=$* | ||||
|     sub_s1=`echo $ports_list | sed 's/ //g'` | ||||
|     sub_s2=${sub_s1//,/ } | ||||
|  | ||||
| @ -14,25 +14,24 @@ | ||||
| # See the License for the specific language governing permissions and | ||||
| # limitations under the License. | ||||
| 
 | ||||
| 
 | ||||
| # Define color variables | ||||
| # Feature | ||||
| # --- Feature ---  | ||||
| COLOR_NORMAL='\033[0m';COLOR_BOLD='\033[1m';COLOR_DIM='\033[2m';COLOR_UNDER='\033[4m'; | ||||
| COLOR_ITALIC='\033[3m';COLOR_NOITALIC='\033[23m';COLOR_BLINK='\033[5m'; | ||||
| COLOR_REVERSE='\033[7m';COLOR_CONCEAL='\033[8m';COLOR_NOBOLD='\033[22m'; | ||||
| COLOR_NOUNDER='\033[24m';COLOR_NOBLINK='\033[25m'; | ||||
| 
 | ||||
| # Front color | ||||
| # --- Front color ---  | ||||
| COLOR_BLACK='\033[30m';COLOR_RED='\033[31m';COLOR_GREEN='\033[32m';COLOR_YELLOW='\033[33m'; | ||||
| COLOR_BLUE='\033[34m';COLOR_MAGENTA='\033[35m';COLOR_CYAN='\033[36m';COLOR_WHITE='\033[37m'; | ||||
| 
 | ||||
| # background color | ||||
| # --- background color ---  | ||||
| COLOR_BBLACK='\033[40m';COLOR_BRED='\033[41m'; | ||||
| COLOR_BGREEN='\033[42m';COLOR_BYELLOW='\033[43m'; | ||||
| COLOR_BBLUE='\033[44m';COLOR_BMAGENTA='\033[45m'; | ||||
| COLOR_BCYAN='\033[46m';COLOR_BWHITE='\033[47m'; | ||||
| 
 | ||||
| # Color definitions | ||||
| # --- Color definitions ---  | ||||
| COLOR_SUFFIX="\033[0m"      # End all colors and special effects | ||||
| BLACK_PREFIX="\033[30m"     # Black prefix | ||||
| RED_PREFIX="\033[31m"       # Red prefix | ||||
| @ -48,11 +47,11 @@ BRIGHT_GREEN_PREFIX='\033[1;32m' # Bright green prefix | ||||
| CYAN_PREFIX="\033[0;36m"     # Cyan prefix | ||||
| 
 | ||||
| # --- make demo (run demo) --- | ||||
| readonly reset=$(tput sgr0) | ||||
| readonly green=$(tput bold; tput setaf 2) | ||||
| readonly yellow=$(tput bold; tput setaf 3) | ||||
| readonly blue=$(tput bold; tput setaf 6) | ||||
| readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi) | ||||
| reset=$(tput sgr0) | ||||
| green=$(tput bold; tput setaf 2) | ||||
| yellow=$(tput bold; tput setaf 3) | ||||
| blue=$(tput bold; tput setaf 6) | ||||
| timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi) | ||||
| 
 | ||||
| # --- helper functions for logs --- | ||||
| info() | ||||
|  | ||||
| @ -17,6 +17,8 @@ | ||||
| # Controls verbosity of the script output and logging. | ||||
| OPENIM_VERBOSE="${OPENIM_VERBOSE:-5}" | ||||
| 
 | ||||
| souorce $(dirname ${BASH_SOURCE})/color.sh | ||||
| 
 | ||||
| # Handler for when we exit automatically on an error. | ||||
| # Borrowed from https://gist.github.com/ahendrix/7030300 | ||||
| openim::log::errexit() { | ||||
|  | ||||
| @ -13,6 +13,70 @@ | ||||
| # See the License for the specific language governing permissions and | ||||
| # limitations under the License. | ||||
| 
 | ||||
| # this script is used to check whether the code is formatted by gofmt or not | ||||
| # | ||||
| # Usage: source scripts/lib/util.sh | ||||
| 
 | ||||
| 
 | ||||
| #1、将IP写在一个文件里,比如文件名为hosts_file,一行一个IP地址。 | ||||
| #2、修改ssh-mutual-trust.sh里面的用户名及密码,默认为root用户及密码123。 | ||||
| # hosts_file_path="path/to/your/hosts/file" | ||||
| # openim:util::setup_ssh_key_copy "$hosts_file_path" "root" "123" | ||||
| function openim:util::setup_ssh_key_copy() { | ||||
|   local hosts_file="$1" | ||||
|   local username="${2:-root}" | ||||
|   local password="${3:-123}" | ||||
| 
 | ||||
|   local sshkey_file=~/.ssh/id_rsa.pub | ||||
| 
 | ||||
|   # check sshkey file  | ||||
|   if [[ ! -e $sshkey_file ]]; then | ||||
|     expect -c " | ||||
|     spawn ssh-keygen -t rsa | ||||
|     expect \"Enter*\" { send \"\n\"; exp_continue; } | ||||
|     " | ||||
|   fi | ||||
| 
 | ||||
|   # get hosts list | ||||
|   local hosts=$(awk '/^[^#]/ {print $1}' "${hosts_file}") | ||||
| 
 | ||||
|   ssh_key_copy() { | ||||
|     local target=$1 | ||||
| 
 | ||||
|     # delete history | ||||
|     sed -i "/$target/d" ~/.ssh/known_hosts | ||||
| 
 | ||||
|     # copy key  | ||||
|     expect -c " | ||||
|     set timeout 100 | ||||
|     spawn ssh-copy-id $username@$target | ||||
|     expect { | ||||
|       \"yes/no\" { send \"yes\n\"; exp_continue; } | ||||
|       \"*assword\" { send \"$password\n\"; } | ||||
|       \"already exist on the remote system\" { exit 1; } | ||||
|     } | ||||
|     expect eof | ||||
|     " | ||||
|   } | ||||
| 
 | ||||
|   # auto sshkey pair | ||||
|   for host in $hosts; do | ||||
|     if ! ping -i 0.2 -c 3 -W 1 "$host" > /dev/null 2>&1; then | ||||
|       echo "[ERROR]: Can't connect $host" | ||||
|       continue | ||||
|     fi | ||||
| 
 | ||||
|     local host_entry=$(awk "/$host/"'{print $1, $2}' /etc/hosts) | ||||
|     if [[ $host_entry ]]; then | ||||
|       local hostaddr=$(echo "$host_entry" | awk '{print $1}') | ||||
|       local hostname=$(echo "$host_entry" | awk '{print $2}') | ||||
|       ssh_key_copy "$hostaddr" | ||||
|       ssh_key_copy "$hostname" | ||||
|     else | ||||
|       ssh_key_copy "$host" | ||||
|     fi | ||||
|   done | ||||
| } | ||||
| 
 | ||||
| function openim::util::sourced_variable { | ||||
|   # Call this function to tell shellcheck that a variable is supposed to | ||||
| @ -774,7 +838,7 @@ trap "echo" EXIT | ||||
| # input: [10023, 2323, 3434] | ||||
| # output: 10023 2323 3434 | ||||
| # Function function: Converts a list to a string, removing Spaces and parentheses | ||||
| function openim::util:list-to-string() { | ||||
| function openim::util::list-to-string() { | ||||
|     ports_list=$*  # 获取传入的参数列表 | ||||
|     sub_s1=$(echo $ports_list | sed 's/ //g')  # 去除空格 | ||||
|     sub_s2=${sub_s1//,/ }  # 将逗号替换为空格 | ||||
|  | ||||
| @ -36,11 +36,11 @@ ulimit -n 200000 | ||||
| list1=$(cat $config_path | grep openImMessageGatewayPort | awk -F '[:]' '{print $NF}') | ||||
| list2=$(cat $config_path | grep openImWsPort | awk -F '[:]' '{print $NF}') | ||||
| list3=$(cat $config_path | grep messageGatewayPrometheusPort | awk -F '[:]' '{print $NF}') | ||||
| openim::util:list-to-string $list1 | ||||
| openim::util::list-to-string $list1 | ||||
| rpc_ports=($ports_array) | ||||
| openim::util:list-to-string $list2 | ||||
| openim::util::list-to-string $list2 | ||||
| ws_ports=($ports_array) | ||||
| openim::util:list-to-string $list3 | ||||
| openim::util::list-to-string $list3 | ||||
| prome_ports=($ports_array) | ||||
| if [ ${#rpc_ports[@]} -ne ${#ws_ports[@]} ]; then | ||||
| 
 | ||||
|  | ||||
| @ -32,7 +32,7 @@ logs_dir="$OPENIM_ROOT/logs" | ||||
| cd $OPENIM_ROOT | ||||
| 
 | ||||
| list1=$(cat $config_path | grep messageTransferPrometheusPort | awk -F '[:]' '{print $NF}') | ||||
| openim::util:list-to-string $list1 | ||||
| openim::util::list-to-string $list1 | ||||
| prome_ports=($ports_array) | ||||
| 
 | ||||
| #Check if the service exists | ||||
|  | ||||
| @ -37,9 +37,9 @@ cd "$OPENIM_ROOT/scripts/" | ||||
| 
 | ||||
| list1=$(cat $config_path | grep openImPushPort | awk -F '[:]' '{print $NF}') | ||||
| list2=$(cat $config_path | grep pushPrometheusPort | awk -F '[:]' '{print $NF}') | ||||
| openim::util:list-to-string $list1 | ||||
| openim::util::list-to-string $list1 | ||||
| rpc_ports=($ports_array) | ||||
| openim::util:list-to-string $list2 | ||||
| openim::util::list-to-string $list2 | ||||
| prome_ports=($ports_array) | ||||
| 
 | ||||
| #Check if the service exists | ||||
|  | ||||
| @ -90,11 +90,11 @@ for ((i = 0; i < ${#service_filename[*]}; i++)); do | ||||
|   cd $BIN_DIR | ||||
|   # Get the rpc port in the configuration file | ||||
|   portList=$(cat $config_path | grep ${service_port_name[$i]} | awk -F '[:]' '{print $NF}') | ||||
|   openim::util:list-to-string ${portList} | ||||
|   openim::util::list-to-string ${portList} | ||||
|   service_ports=($ports_array) | ||||
| 
 | ||||
|   portList2=$(cat $config_path | grep ${service_prometheus_port_name[$i]} | awk -F '[:]' '{print $NF}') | ||||
|   openim::util:list-to-string $portList2 | ||||
|   openim::util::list-to-string $portList2 | ||||
|   prome_ports=($ports_array) | ||||
|   #Start related rpc services based on the number of ports | ||||
|   for ((j = 0; j < ${#service_ports[*]}; j++)); do | ||||
|  | ||||
| @ -19,7 +19,7 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | ||||
| OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. | ||||
| 
 | ||||
| #Include shell font styles and some basic information | ||||
| source $OPENIM_ROOT/scripts/style_info.sh | ||||
| source $OPENIM_ROOT/scripts/lib/init.sh | ||||
| source $OPENIM_ROOT/scripts/path_info.sh | ||||
| 
 | ||||
| bin_dir="$BIN_DIR" | ||||
|  | ||||
							
								
								
									
										29
									
								
								scripts/update-yamlfmt.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								scripts/update-yamlfmt.sh
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| set -o errexit | ||||
| set -o nounset | ||||
| set -o pipefail | ||||
| 
 | ||||
| OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. | ||||
| source "${OPENIM_ROOT}/hack/lib/init.sh" | ||||
| 
 | ||||
| kube::golang::setup_env | ||||
| 
 | ||||
| cd "${OPENIM_ROOT}" | ||||
| 
 | ||||
| find_files() { | ||||
|   find . -not \( \ | ||||
|       \( \ | ||||
|         -wholename './output' \ | ||||
|         -o -wholename './.git' \ | ||||
|         -o -wholename './_output' \ | ||||
|         -o -wholename './_gopath' \ | ||||
|         -o -wholename './release' \ | ||||
|         -o -wholename './target' \ | ||||
|         -o -wholename '*/vendor/*' \ | ||||
|       \) -prune \ | ||||
|     \) -name 'OWNERS*' | ||||
| } | ||||
| 
 | ||||
| export GO111MODULE=on | ||||
| find_files | xargs go run tools/yamlfmt/main.go | ||||
							
								
								
									
										34
									
								
								scripts/verify-yamlfmt.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								scripts/verify-yamlfmt.sh
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| # This script checks whether the OWNERS files need to be formatted or not by | ||||
| # `yamlfmt`. Run `scripts/update-yamlfmt.sh` to actually format sources. | ||||
| # | ||||
| # Usage: `scripts/verify-yamlfmt.sh`. | ||||
| 
 | ||||
| set -o errexit | ||||
| set -o nounset | ||||
| set -o pipefail | ||||
| 
 | ||||
| OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. | ||||
| source "${OPENIM_ROOT}/scripts/lib/init.sh" | ||||
| 
 | ||||
| openim::util::ensure_clean_working_dir | ||||
| # This sets up the environment, like GOCACHE, which keeps the worktree cleaner. | ||||
| openim::golang::setup_env | ||||
| 
 | ||||
| _tmpdir="$(openim::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")" | ||||
| git worktree add -f -q "${_tmpdir}" HEAD | ||||
| openim::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT | ||||
| cd "${_tmpdir}" | ||||
| 
 | ||||
| # Format YAML files | ||||
| hack/update-yamlfmt.sh | ||||
| 
 | ||||
| # Test for diffs | ||||
| diffs=$(git status --porcelain | wc -l) | ||||
| if [[ ${diffs} -gt 0 ]]; then | ||||
|   echo "YAML files need to be formatted" >&2 | ||||
|   git diff | ||||
|   echo "Please run 'hack/update-yamlfmt.sh'" >&2 | ||||
|   exit 1 | ||||
| fi | ||||
| @ -1,9 +1,5 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| # Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved. | ||||
| # Use of this source code is governed by a MIT style | ||||
| # license that can be found in the LICENSE file. | ||||
| 
 | ||||
| #   Use this script to test if a given TCP host/port are available | ||||
| 
 | ||||
| WAITFORIT_cmdname=${0##*/} | ||||
|  | ||||
							
								
								
									
										35
									
								
								tools/ncpu/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								tools/ncpu/main_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| // Copyright © 2023 OpenIM. All rights reserved. | ||||
| // | ||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| // you may not use this file except in compliance with the License. | ||||
| // You may obtain a copy of the License at | ||||
| // | ||||
| //     http://www.apache.org/licenses/LICENSE-2.0 | ||||
| // | ||||
| // Unless required by applicable law or agreed to in writing, software | ||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package main | ||||
| 
 | ||||
| import "testing" | ||||
| 
 | ||||
| func Test_main(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		name string | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name: "Test_main", | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "Test_main2", | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			main() | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										10
									
								
								tools/yamlfmt/OWNERS
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								tools/yamlfmt/OWNERS
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| # See the OWNERS docs at https://go.k8s.io/owners | ||||
| 
 | ||||
| reviewers: | ||||
|   - cubxxw | ||||
|   - kubbot | ||||
| approvers: | ||||
|   - cubxxw | ||||
| labels: | ||||
|   - sig/testing | ||||
|   - sig/contributor-experience | ||||
							
								
								
									
										8
									
								
								tools/yamlfmt/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								tools/yamlfmt/go.mod
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| module github.com/OpenIMSDK/Open-IM-Server/tools/yamlfmt | ||||
| 
 | ||||
| go 1.20 | ||||
| 
 | ||||
| require ( | ||||
| 	github.com/likexian/gokit v0.25.13 | ||||
| 	gopkg.in/yaml.v3 v3.0.1 | ||||
| ) | ||||
							
								
								
									
										6
									
								
								tools/yamlfmt/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								tools/yamlfmt/go.sum
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | ||||
| github.com/likexian/gokit v0.25.13 h1:p2Uw3+6fGG53CwdU2Dz0T6bOycdb2+bAFAa3ymwWVkM= | ||||
| github.com/likexian/gokit v0.25.13/go.mod h1:qQhEWFBEfqLCO3/vOEo2EDKd+EycekVtUK4tex+l2H4= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||||
							
								
								
									
										58
									
								
								tools/yamlfmt/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								tools/yamlfmt/main.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,58 @@ | ||||
| // OPENIM plan on prow tools | ||||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	"flag" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"os" | ||||
| 
 | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func main() { | ||||
| 	// Prow OWNERs file defines the default indent as 2 spaces. | ||||
| 	indent := flag.Int("indent", 2, "default indent") | ||||
| 	flag.Parse() | ||||
| 	for _, path := range flag.Args() { | ||||
| 		sourceYaml, err := os.ReadFile(path) | ||||
| 		if err != nil { | ||||
| 			fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		rootNode, err := fetchYaml(sourceYaml) | ||||
| 		if err != nil { | ||||
| 			fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) | ||||
| 		if err != nil { | ||||
| 			fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		err = streamYaml(writer, indent, rootNode) | ||||
| 		if err != nil { | ||||
| 			fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) | ||||
| 			continue | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func fetchYaml(sourceYaml []byte) (*yaml.Node, error) { | ||||
| 	rootNode := yaml.Node{} | ||||
| 	err := yaml.Unmarshal(sourceYaml, &rootNode) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return &rootNode, nil | ||||
| } | ||||
| 
 | ||||
| func streamYaml(writer io.Writer, indent *int, in *yaml.Node) error { | ||||
| 	encoder := yaml.NewEncoder(writer) | ||||
| 	encoder.SetIndent(*indent) | ||||
| 	err := encoder.Encode(in) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return encoder.Close() | ||||
| } | ||||
							
								
								
									
										144
									
								
								tools/yamlfmt/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								tools/yamlfmt/main_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,144 @@ | ||||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	"bufio" | ||||
| 	"bytes" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/likexian/gokit/assert" | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| func Test_main(t *testing.T) { | ||||
| 	sourceYaml := ` # See the OWNERS docs at https://go.k8s.io/owners | ||||
| approvers: | ||||
| - dep-approvers | ||||
| - thockin         # Network | ||||
| - liggitt | ||||
| 
 | ||||
| labels: | ||||
| - sig/architecture | ||||
| ` | ||||
| 
 | ||||
| 	outputYaml := `# See the OWNERS docs at https://go.k8s.io/owners | ||||
| approvers: | ||||
|   - dep-approvers | ||||
|   - thockin # Network | ||||
|   - liggitt | ||||
| labels: | ||||
|   - sig/architecture | ||||
| ` | ||||
| 	node, _ := fetchYaml([]byte(sourceYaml)) | ||||
| 	var output bytes.Buffer | ||||
| 	indent := 2 | ||||
| 	writer := bufio.NewWriter(&output) | ||||
| 	_ = streamYaml(writer, &indent, node) | ||||
| 	_ = writer.Flush() | ||||
| 	assert.Equal(t, outputYaml, string(output.Bytes()), "yaml was not formatted correctly") | ||||
| } | ||||
| 
 | ||||
| func Test_fetchYaml(t *testing.T) { | ||||
| 	type args struct { | ||||
| 		sourceYaml []byte | ||||
| 	} | ||||
| 	tests := []struct { | ||||
| 		name    string | ||||
| 		args    args | ||||
| 		want    *yaml.Node | ||||
| 		wantErr bool | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name: "Valid YAML", | ||||
| 			args: args{sourceYaml: []byte("key: value")}, | ||||
| 			want: &yaml.Node{ | ||||
| 				Kind:  yaml.MappingNode, | ||||
| 				Tag:   "!!map", | ||||
| 				Value: "", | ||||
| 				Content: []*yaml.Node{ | ||||
| 					&yaml.Node{ | ||||
| 						Kind:  yaml.ScalarNode, | ||||
| 						Tag:   "!!str", | ||||
| 						Value: "key", | ||||
| 					}, | ||||
| 					&yaml.Node{ | ||||
| 						Kind:  yaml.ScalarNode, | ||||
| 						Tag:   "!!str", | ||||
| 						Value: "value", | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			wantErr: false, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "Invalid YAML", | ||||
| 			args:    args{sourceYaml: []byte("key:")}, | ||||
| 			want:    nil, | ||||
| 			wantErr: true, | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			got, err := fetchYaml(tt.args.sourceYaml) | ||||
| 			if (err != nil) != tt.wantErr { | ||||
| 				t.Errorf("fetchYaml() error = %v, wantErr %v", err, tt.wantErr) | ||||
| 				return | ||||
| 			} | ||||
| 			if !reflect.DeepEqual(got, tt.want) { | ||||
| 				t.Errorf("fetchYaml() = %v, want %v", got, tt.want) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func Test_streamYaml(t *testing.T) { | ||||
| 	type args struct { | ||||
| 		indent *int | ||||
| 		in     *yaml.Node | ||||
| 	} | ||||
| 	defaultIndent := 2 | ||||
| 	tests := []struct { | ||||
| 		name       string | ||||
| 		args       args | ||||
| 		wantWriter string | ||||
| 		wantErr    bool | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name: "Valid YAML node with default indent", | ||||
| 			args: args{ | ||||
| 				indent: &defaultIndent, | ||||
| 				in: &yaml.Node{ | ||||
| 					Kind:  yaml.MappingNode, | ||||
| 					Tag:   "!!map", | ||||
| 					Value: "", | ||||
| 					Content: []*yaml.Node{ | ||||
| 						&yaml.Node{ | ||||
| 							Kind:  yaml.ScalarNode, | ||||
| 							Tag:   "!!str", | ||||
| 							Value: "key", | ||||
| 						}, | ||||
| 						&yaml.Node{ | ||||
| 							Kind:  yaml.ScalarNode, | ||||
| 							Tag:   "!!str", | ||||
| 							Value: "value", | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			wantWriter: "key: value\n", | ||||
| 			wantErr:    false, | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			writer := &bytes.Buffer{} | ||||
| 			if err := streamYaml(writer, tt.args.indent, tt.args.in); (err != nil) != tt.wantErr { | ||||
| 				t.Errorf("streamYaml() error = %v, wantErr %v", err, tt.wantErr) | ||||
| 				return | ||||
| 			} | ||||
| 			if gotWriter := writer.String(); gotWriter != tt.wantWriter { | ||||
| 				t.Errorf("streamYaml() = %v, want %v", gotWriter, tt.wantWriter) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user