client.py 3.5 KB

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