executor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package promputils
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "os/exec"
  21. "regexp"
  22. "strings"
  23. "yunion.io/x/pkg/utils"
  24. "yunion.io/x/structarg"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. var (
  28. parser *structarg.ArgumentParser
  29. session *mcclient.ClientSession
  30. )
  31. func InitEnv(_parser *structarg.ArgumentParser, _session *mcclient.ClientSession) {
  32. parser = _parser
  33. session = _session
  34. }
  35. func escaper(str string) string {
  36. var re = regexp.MustCompile(`(?i)--filter\s+[a-z0-9]+[^\s]+( |$)`)
  37. for _, match := range re.FindAllString(str, -1) {
  38. rep := strings.Replace(match, "--filter", "", -1)
  39. rep = strings.TrimSpace(rep)
  40. rep = strings.Replace(rep, `\'`, "efWpvXpY6lH5", -1)
  41. rep = strings.Replace(rep, "'", "", -1)
  42. rep = strings.Replace(rep, `\"`, "GsVHUhkj68Be", -1)
  43. rep = strings.Replace(rep, `"`, "", -1)
  44. rep = strings.Replace(rep, `GsVHUhkj68Be`, `\"`, -1)
  45. rep = strings.Replace(rep, `efWpvXpY6lH5`, `\'`, -1)
  46. rep = `--filter ` + rep
  47. str = strings.Replace(str, match, rep, -1)
  48. }
  49. return str
  50. }
  51. func Executor(s string) {
  52. s = strings.TrimSpace(s)
  53. if s == "" {
  54. return
  55. } else if s == "quit" || s == "exit" || s == "x" || s == "q" {
  56. fmt.Println("Bye!")
  57. os.Exit(0)
  58. return
  59. }
  60. s = escaper(s)
  61. args := utils.ArgsStringToArray(s)
  62. e := parser.ParseArgs(utils.ArgsStringToArray(s), false)
  63. subcmd := parser.GetSubcommand()
  64. subparser := subcmd.GetSubParser()
  65. if args[0] == "--debug" {
  66. session.GetClient().SetDebug(true)
  67. } else {
  68. session.GetClient().SetDebug(false)
  69. }
  70. if subparser != nil && subparser.IsHelpSet() {
  71. fmt.Print(subparser.HelpString())
  72. return
  73. }
  74. if e != nil {
  75. if subparser != nil {
  76. fmt.Print(subparser.Usage())
  77. } else {
  78. fmt.Print(parser.Usage())
  79. }
  80. fmt.Println(e)
  81. } else {
  82. suboptions := subparser.Options()
  83. e = subcmd.Invoke(session, suboptions)
  84. if e != nil {
  85. fmt.Println(e)
  86. }
  87. }
  88. return
  89. }
  90. func ExecuteAndGetResult(s string) (string, error) {
  91. s = strings.TrimSpace(s)
  92. if s == "" {
  93. return "", errors.New("you need to pass the something arguments")
  94. }
  95. out := &bytes.Buffer{}
  96. cmd := exec.Command("/bin/sh", "-c", "source ~/.RC_ADMIN &&/home/yunion/git/yunioncloud/_output/bin/climc "+s)
  97. cmd.Stdin = os.Stdin
  98. cmd.Stdout = out
  99. if err := cmd.Run(); err != nil {
  100. return "", err
  101. }
  102. r := string(out.Bytes())
  103. return r, nil
  104. }