client.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. dotenv.load_dotenv()
  30. fgit_host = os.getenv('FGH_HOST')
  31. try:
  32. if fgit_host[-1] == '/':
  33. self.fgit_host = fgit_host[:-1]
  34. self.token = os.getenv('FGh_TOKEN')
  35. except Exception as e:
  36. print(".env配置错误"+e)
  37. @staticmethod
  38. def parse_args():
  39. parser = argparse.ArgumentParser(description='fgit client')
  40. parser.add_argument('command', type=str, help='fgit command',
  41. choices=['git','wget','curl','clone', 'push', 'pull', 'commit', 'add', 'status', 'log', 'diff', 'branch', 'checkout', 'merge', 'rebase', 'reset', 'tag', 'fetch', 'remote', 'init', 'config', 'help'])
  42. # all args behind command
  43. parser.add_argument('args', nargs=argparse.REMAINDER, help='git command args')
  44. return parser.parse_args()
  45. def choose_host(self):
  46. """choose host"""
  47. pass
  48. def run(self):
  49. args = self.parse_args()
  50. command = args.command
  51. if len(args.args) == 0 and command != 'help':
  52. print('Usage: fgh <command> [<args>]')
  53. sys.exit(1)
  54. if command == "wget":
  55. if len(args.args) > 0:
  56. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  57. # print('wget ' + ' '.join(args.args))
  58. os.system('wget ' + ' '.join(args.args))
  59. elif command =="curl":
  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('curl ' + ' '.join(args.args))
  63. os.system('curl ' + ' '.join(args.args))
  64. elif command == "help":
  65. print(Client.help_str)
  66. else:
  67. if len(args.args) > 0:
  68. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  69. cmd_str = 'git ' + command + ' ' + ' '.join(args.args)
  70. try:
  71. if command == 'git':
  72. cmd_str = command + ' ' + ' '.join(args.args)
  73. os.system(cmd_str)
  74. # print(cmd_str)
  75. except Exception as e:
  76. print(cmd_str + e)
  77. if __name__=='__main__':
  78. client = Client()
  79. client.run()