A command line tool for DigitalOcean services

command.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2016 The Doctl Authors All rights reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package doit
  14. import (
  15. "os"
  16. "os/exec"
  17. )
  18. // Command runs commands.
  19. type Command interface {
  20. Run(args ...string) ([]byte, error)
  21. Start(args ...string) error
  22. Stop() error
  23. }
  24. // LiveCommand is a live implementation of Command.
  25. type LiveCommand struct {
  26. path string
  27. cmd *exec.Cmd
  28. }
  29. // NewLiveCommand creates a LiveCommand.
  30. func NewLiveCommand(path string) *LiveCommand {
  31. return &LiveCommand{
  32. path: path,
  33. }
  34. }
  35. var _ Command = &LiveCommand{}
  36. // Run runs a LiveCommand with args and returns stdout and an error if there was one.
  37. func (c *LiveCommand) Run(args ...string) ([]byte, error) {
  38. return exec.Command(c.path, args...).Output()
  39. }
  40. // Start runs a LiveCommand with args and starts it. This would most likely block,
  41. // so you should call it in a goroutine.
  42. func (c *LiveCommand) Start(args ...string) error {
  43. c.cmd = exec.Command(c.path, args...)
  44. c.cmd.Stderr = os.Stderr
  45. return c.cmd.Start()
  46. }
  47. // Stop stops an existing LiveCommand.
  48. func (c *LiveCommand) Stop() error {
  49. return c.cmd.Process.Kill()
  50. }
  51. // MockCommand is a mock command implementation. It allows you simulate running
  52. // an external command.
  53. type MockCommand struct {
  54. path string
  55. running bool
  56. runFn func() error
  57. startFn func() error
  58. stopFn func() error
  59. }
  60. var _ Command = &MockCommand{}
  61. // NewMockCommand createsd a MockCommand.
  62. func NewMockCommand(path string) *MockCommand {
  63. return &MockCommand{
  64. path: path,
  65. runFn: func() error {
  66. return nil
  67. },
  68. startFn: func() error {
  69. return nil
  70. },
  71. stopFn: func() error {
  72. return nil
  73. },
  74. }
  75. }
  76. // Run simulates the running of a command.
  77. func (c *MockCommand) Run(args ...string) ([]byte, error) {
  78. return nil, c.runFn()
  79. }
  80. // Start simulates starting a command.
  81. func (c *MockCommand) Start(args ...string) error {
  82. c.running = true
  83. return c.startFn()
  84. }
  85. // Stop simulates stoping a command.
  86. func (c *MockCommand) Stop() error {
  87. c.running = false
  88. return c.stopFn()
  89. }