简体   繁体   English

检查 github-cli 是否已登录

[英]Check if github-cli is logged in

I'm working in a bash script using the github cli to create some repos under some conditions but for that I first need to check if the user has the cli installed (and I got that check working) and if it is logged in. gh-cli provides a function gh auth status for that, but I can't use its return to check if the user is logged in. There are two possible outputs:我在 bash 脚本中工作,使用 github cli 在某些条件下创建一些存储库,但为此我首先需要检查用户是否安装了 cli(并且我得到了检查工作)以及它是否已登录。gh -cli 为此提供了 function gh auth status ,但我不能使用它的返回来检查用户是否已登录。有两种可能的输出:

You are not logged into any GitHub hosts. Run gh auth login to authenticate.

or或者

    github.com
  ✓ Logged in to github.com as <username> (oauth_token)
  ✓ Git operations for github.com configured to use ssh protocol.
  ✓ Token: *******************

So my first try was to check if there is "not logged" inside that string with:所以我的第一次尝试是检查该字符串中是否有“未记录”:

if [[ $(gh auth status) =~ "not logged" ]] ;
then
    echo "Not logged" ;
    exit 1
fi
#run script

But I had no success.但我没有成功。 I tried also using grep -oP and sed but then I realized that somehow i can't get the output as a string我也尝试使用 grep -oP 和 sed 但后来我意识到不知何故我无法将 output 作为字符串

Anyone has any suggestions on how to check or fix this?有人对如何检查或解决此问题有任何建议吗?

Use the exit status to determine the success/failure of the installed and logged-in checks:使用退出状态来确定已安装和登录检查的成功/失败:

  1. check it's installed:检查它是否已安装:

     if; command -v gh >/dev/null 2>&1; then echo "Install gh first" exit 1 fi
  2. check auth检查身份验证

    if; gh auth status >/dev/null 2>&1: then echo "You need to login: gh auth login" exit 1 fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM