client.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2024/04/09 12:56:07
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : client enter point
  8. """
  9. import os,sys,re
  10. import argparse
  11. class Client:
  12. help_str ="""
  13. # clone repo
  14. fgh clone https://github.com/xx/yy.git
  15. fgh push https://github.com/xx/yy.git
  16. # download file
  17. fgh wget https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
  18. fgh wget https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md
  19. fgh curl -O https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
  20. fgh curl -O https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md
  21. """
  22. def __init__(self):
  23. self.fgit_host = ''
  24. self.token = ''
  25. self.read_config()
  26. self.args = self.parse_args()
  27. def read_config(self):
  28. import dotenv
  29. current_dir = os.path.dirname(os.path.realpath(__file__))
  30. dotenv.load_dotenv(os.path.join(current_dir, '.env'))
  31. fgit_host = os.getenv('FGH_HOST')
  32. try:
  33. if fgit_host[-1] == '/':
  34. self.fgit_host = fgit_host[:-1]
  35. self.token = os.getenv('FGh_TOKEN')
  36. except Exception as e:
  37. print(".env配置错误"+e)
  38. @staticmethod
  39. def parse_args():
  40. parser = argparse.ArgumentParser(description='fgit client')
  41. parser.add_argument('command', type=str, help='fgit command',
  42. choices=['git','wget','curl','clone', 'push', 'pull', 'commit', 'add', 'status', 'log', 'diff', 'branch', 'checkout', 'merge', 'rebase', 'reset', 'tag', 'fetch', 'remote', 'init', 'config', 'help'])
  43. # all args behind command
  44. parser.add_argument('args', nargs=argparse.REMAINDER, help='git command args')
  45. return parser.parse_args()
  46. def choose_host(self):
  47. """choose host"""
  48. pass
  49. def run(self):
  50. args = self.parse_args()
  51. command = args.command
  52. if len(args.args) == 0 and command != 'help':
  53. print('Usage: fgh <command> [<args>]')
  54. sys.exit(1)
  55. if command == "wget":
  56. if len(args.args) > 0:
  57. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  58. # print('wget ' + ' '.join(args.args))
  59. os.system('wget ' + ' '.join(args.args))
  60. elif command =="curl":
  61. if len(args.args) > 0:
  62. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  63. print('curl ' + ' '.join(args.args))
  64. os.system('curl ' + ' '.join(args.args))
  65. elif command == "help":
  66. print(Client.help_str)
  67. else:
  68. if len(args.args) > 0:
  69. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  70. cmd_str = 'git ' + command + ' ' + ' '.join(args.args)
  71. try:
  72. if command == 'git':
  73. cmd_str = command + ' ' + ' '.join(args.args)
  74. os.system(cmd_str)
  75. # print(cmd_str)
  76. except Exception as e:
  77. print(cmd_str + e)
  78. if __name__=='__main__':
  79. client = Client()
  80. client.run()