You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commandline.go 1.9 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package cmdline
  2. import (
  3. "fmt"
  4. "os"
  5. "gitlink.org.cn/cloudream/common/pkgs/cmdtrie"
  6. "gitlink.org.cn/cloudream/storage/client/internal/services"
  7. )
  8. // CommandContext 命令上下文,存储与命令行相关的上下文信息。
  9. type CommandContext struct {
  10. Cmdline *Commandline // 指向当前的Commandline实例。
  11. }
  12. // commands 用于存储所有已注册的命令及其相关信息的Trie树。
  13. var commands cmdtrie.CommandTrie[CommandContext, error] = cmdtrie.NewCommandTrie[CommandContext, error]()
  14. // Commandline 命令行对象,封装了与服务交互的能力。
  15. type Commandline struct {
  16. Svc *services.Service // 指向内部服务接口。
  17. }
  18. // NewCommandline 创建一个新的Commandline实例。
  19. // svc: 指向内部服务的实例。
  20. // 返回值: 初始化好的Commandline指针及可能的错误。
  21. func NewCommandline(svc *services.Service) (*Commandline, error) {
  22. return &Commandline{
  23. Svc: svc,
  24. }, nil
  25. }
  26. // DispatchCommand 分发并执行命令。
  27. // allArgs: 命令行中所有的参数。
  28. // 功能: 根据参数执行相应的命令逻辑,出错时退出程序。
  29. func (c *Commandline) DispatchCommand(allArgs []string) {
  30. cmdCtx := CommandContext{
  31. Cmdline: c,
  32. }
  33. // 执行命令,根据命令执行结果做相应处理。
  34. cmdErr, err := commands.Execute(cmdCtx, allArgs, cmdtrie.ExecuteOption{ReplaceEmptyArrayWithNil: true})
  35. if err != nil {
  36. fmt.Printf("execute command failed, err: %s", err.Error())
  37. os.Exit(1)
  38. }
  39. if cmdErr != nil {
  40. fmt.Printf("execute command failed, err: %s", cmdErr.Error())
  41. os.Exit(1)
  42. }
  43. }
  44. // MustAddCmd 必须添加命令。
  45. // fn: 命令执行的函数。
  46. // prefixWords: 命令的前缀词。
  47. // 返回值: 无。
  48. // 功能: 向命令树中添加命令,添加失败时会抛出异常。
  49. func MustAddCmd(fn any, prefixWords ...string) any {
  50. commands.MustAdd(fn, prefixWords...)
  51. return nil
  52. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。