Shell Utils

  • shell-dsr/utils.sh
#!/usr/bin/bash

# echo message with color
########################################################
function echoError {
  echo -e "\033[31m $1 \033[0m"
}

function echoSuccess {
  echo -e "\033[32m $1 \033[0m"
}

function echoWarning {
  echo -e "\033[33m $1 \033[0m"
}

function echoNotice {
  echo -e "\033[36m $1 \033[0m"
}

function hideCursor {
  echo -e "\033[?25l"
}

function showCursor {
  echo -e "\033[?25h"
}

# set cursor position
function setCursor {
  local line=$1
  local col=$2
  tput cup $line $col
}
# directory and File
########################################################
# check directory
function isDirExist {
  local dirs=$@
  for dir in $dirs
  do
    if [ -d $dir ]
    then
      continue
    else
      echo $dir
      break
    fi
  done
}

# check file exist
function isFileExist {
  local files=$@
  echo $#
  for file in $files
  do
    if [ -f "$file" ]
    then
      continue
    else
      echo $file
      break
    fi
  done
}

# upper the first char
function upperFirstChar {
  local words=$@
  local newWords=""
  for word in $words
  do
    local firstChar=${word:0:1}
    local upperChar=${firstChar^^}
    local newWord=$upperChar${word:1}
    newWords="$newWords $newWord"
  done
  echo $newWords
}
  • shell-dsr/fileUtils.sh
#!/usr/bin/bash

# load utils 
. ./util/utils.sh

# switch directory 
function goDirectory 
{
  local repoPath=$1
  local invalidPath=$(isDirExist "$repoPath")
  if [ -n "$invalidPath" ]
  then
    echoError "=== Error: please check '$invalidPath', it is not exist! ==="
    exit
  else 
    cd $repoPath
  fi
}
  • shell-dsr/gitUtils.sh
#!/usr/bin/bash

# load utils 
. ./util/utils.sh

# clone repo use the repo name
function gclone {
  local repo=$1
  git clone "gitPath/${repo}.git"
}

# restore the branch
function restoreBranch {
  local currentPwd=$1
  local currentBranch=$2
  cd $currentPwd
  git checkout $currentBranch
}

# get current branch
function getCurrentBranch {
  echo $(git symbolic-ref --short HEAD)
}

# check branch is exist
function isBranchExist {
  local branch=$(git branch | grep "$1"$)
  if [ -z "$branch" ] 
  then
    echoWarning "=== Error: Branch '$1' is not exist! ==="
    return 1
  fi
}

# check work directory clean status
function checkClean {
  local repo=$1
  local isClean=$(git diff)
  if [ -z "$isClean" ]
  then
    echoSuccess "=== Cool! current repo $repo is clean ==="
  else
    echoError "=== Error: Please make the repo '$repo' to be clean status ==="
    exit
  fi
}

# check merge conflict
function checkConflict {
  local isConflict=$(git diff)
  if [ -z $isConflict ]
  then
    echoWarning "=== Warning: no conflict!==="
  else
    echoError "=== Error: merge Branch has conflict! ==="
    exit
  fi
}

# pull new code from remote repo
function pullCode {
  local releaseBranch=$1
  local noCheck=$2

  # check releaseBranch is exist 
  if [ -z "$noCheck" ]
  then
    isBranchExist "$releaseBranch" 
    if [ $? -ne 0 ]
    then
      exit
    fi
  fi

  # if current branch is not releaseBranch then check out
  local currentBranch=$(getCurrentBranch)
  if [ $currentBranch != $releaseBranch ]
  then
    echoWarning "=== warning: switch to branch: $releaseBranch ==="
    git checkout "$releaseBranch"
  fi

  echoWarning "=== warning: began to pull code  from $releaseBranch ... ==="
  git pull
}

# push code to remote repo
function pushCode {
  local branch=$1
  local file=$2
  local comments=$3
  git add -- "$file"
  git commit --no-verify -m "$comments"
  git push --set-upstream origin $branch
  if [ $? -eq 0 ]
  then
    echoSuccess "=== Success: push code Success! ==="
  else
    echoError "=== Error: push code Failed! ==="
    exit
  fi
}


# check my branch and merge release branch
function mergeBranch {
  local releaseBranch=$1
  local myBranch=$2
  isBranchExist "$myBranch"
  if [ $? -eq 0 ]
  then
    echoWarning "=== warning: switch to branch: $myBranch ==="
    git checkout "$myBranch"
    echoWarning "=== warning: began to merge branch: $releaseBranch to $myBranch ... ==="
    git merge "$releaseBranch" --commit "merge $releaseBranch to $myBranch"
  else 
    echoError "=== Error: Please check the myBranch correctly! ==="
    exit
  fi
}


# create new Branch and switch to it 
function createNewBranch {
  local baseBranch=$1
  local newBranch=$2
  isBranchExist "$newBranch"
  if [ $? -eq 0 ]
  then
    echoWarning "=== warning: switch to branch: $newBranch ==="
    git checkout "$newBranch"
    echoWarning "=== Warning: $newBranch has exist! ==="
  else
    git checkout "$baseBranch"
    echoWarning "=== warning: switch to branch: $newBranch ==="
    git checkout -b "$newBranch"
    echoSuccess "=== Success: $newBranch create Success! ==="
  fi
}

# switch branch
function switchBranch {
  local branch=$1
  isBranchExist "$branch"
  if [ $? -eq 0 ]
  then
    echoWarning "=== warning: switch to branch: $branch ==="
    git checkout "$branch"
  else
    echoError"=== Error: $branch is not Exist! ==="
    exit
  fi
}

posted @   箫笛  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2022-02-14 shell编程 - 条件测试
2022-02-14 shell编程 - 条件语句
点击右上角即可分享
微信分享提示