Adding EaltEdge CLI Command For Deployment
[ealt-edge.git] / ocd / cli / ealt / cmd / setup / common.go
1 /*
2 Copyright 2020 Huawei Technologies Co., Ltd.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // runCommandWithShell executes the given command with "sh -c".
18 // It returns an error if the command outputs anything on the stderr.
19 package setup
20
21
22 import (
23         "bytes"
24         "fmt"
25         "io"
26         "os"
27         "os/exec"
28         "strings"
29         "sync"
30 )
31
32 //Command defines commands to be executed and captures std out and std error
33 type Command struct {
34         Cmd    *exec.Cmd
35         StdOut []byte
36         StdErr []byte
37 }
38
39 func (cm Command) GetStdOutput() string {
40         if len(cm.StdOut) != 0 {
41                 return strings.TrimRight(string(cm.StdOut), "\n")
42         }
43         return ""
44 }
45
46 //GetStdErr gets StdErr field
47 func (cm Command) GetStdErr() string {
48         if len(cm.StdErr) != 0 {
49                 return strings.TrimRight(string(cm.StdErr), "\n")
50         }
51         return ""
52 }
53
54 //It helps in the commands where it takes some time for execution.
55 func (cm Command) ExecuteCmdShowOutput() error {
56         var stdoutBuf, stderrBuf bytes.Buffer
57         stdoutIn, _ := cm.Cmd.StdoutPipe()
58         stderrIn, _ := cm.Cmd.StderrPipe()
59
60         var errStdout, errStderr error
61         stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
62         stderr := io.MultiWriter(os.Stderr, &stderrBuf)
63         err := cm.Cmd.Start()
64         if err != nil {
65                 return fmt.Errorf("failed to start '%s' because of error : %s", strings.Join(cm.Cmd.Args, " "), err.Error())
66         }
67
68         var wg sync.WaitGroup
69         wg.Add(1)
70
71         go func() {
72                 _, errStdout = io.Copy(stdout, stdoutIn)
73                 wg.Done()
74         }()
75
76         _, errStderr = io.Copy(stderr, stderrIn)
77         wg.Wait()
78
79         err = cm.Cmd.Wait()
80         if err != nil {
81                 return fmt.Errorf("failed to run '%s' because of error : %s", strings.Join(cm.Cmd.Args, " "), err.Error())
82         }
83         if errStdout != nil || errStderr != nil {
84                 return fmt.Errorf("failed to capture stdout or stderr")
85         }
86
87         cm.StdOut, cm.StdErr = stdoutBuf.Bytes(), stderrBuf.Bytes()
88         return nil
89 }
90
91 func runCommandAtShell(command string) (string, error) {
92     cmd := &Command{Cmd: exec.Command("sh", "-c", command)}
93     err := cmd.ExecuteCmdShowOutput()
94     if err != nil {
95             return "", err
96     }
97     errout := cmd.GetStdErr()
98     if errout != "" {
99             return "", fmt.Errorf("%s", errout)
100     }
101     return cmd.GetStdOutput(), nil
102 }