command.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 command
  15. import (
  16. "os/exec"
  17. "yunion.io/x/log"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/webconsole/recorder"
  20. )
  21. const (
  22. PROTOCOL_TTY string = "tty"
  23. //PROTOCOL_VNC string = "vnc"
  24. )
  25. type ICommand interface {
  26. GetProtocol() string
  27. GetCommand() *exec.Cmd
  28. GetSafeCommandString() string
  29. Cleanup() error
  30. Scan(d byte, send func(msg string))
  31. GetClientSession() *mcclient.ClientSession
  32. GetRecordObject() *recorder.Object
  33. }
  34. type BaseCommand struct {
  35. s *mcclient.ClientSession
  36. name string
  37. args []string
  38. }
  39. func NewBaseCommand(s *mcclient.ClientSession, name string, args ...string) *BaseCommand {
  40. return &BaseCommand{
  41. s: s,
  42. name: name,
  43. args: args,
  44. }
  45. }
  46. func (c *BaseCommand) GetClientSession() *mcclient.ClientSession {
  47. return c.s
  48. }
  49. func (c *BaseCommand) AppendArgs(args ...string) *BaseCommand {
  50. c.args = append(c.args, args...)
  51. return c
  52. }
  53. func (c BaseCommand) GetCommand() *exec.Cmd {
  54. return exec.Command(c.name, c.args...)
  55. }
  56. func (c BaseCommand) GetSafeCommandString() string {
  57. return c.GetCommand().String()
  58. }
  59. func (c BaseCommand) Scan(byte, func(msg string)) {
  60. }
  61. func (c BaseCommand) Cleanup() error {
  62. log.Infof("BaseCommand Cleanup do nothing")
  63. return nil
  64. }
  65. func (c BaseCommand) GetRecordObject() *recorder.Object {
  66. return nil
  67. }