client.py 3.4 KB

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