/
exec.go
50 lines (41 loc) · 1.12 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package command
import (
"fmt"
"os/exec"
"strings"
"github.com/local-deploy/dl/project"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func execCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "exec [command]",
Short: "Executing a command in a PHP container",
Long: `Running bash command in PHP container as user www-data`,
Example: "dl exec composer install\ndl exec \"ls -la\"",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runExec(args)
},
}
return cmd
}
func runExec(args []string) {
project.LoadEnv()
docker, err := exec.LookPath("docker")
if err != nil {
fmt.Println(err)
return
}
command := []string{docker, "exec", "-t", "-w", "/var/www/html"}
site := project.Env.GetString("HOST_NAME")
command = append(command, site+"_php")
command = append(command, args...)
logrus.Infof("Use container name %s", site+"_php")
logrus.Infof("Command execution %s", strings.Join(args, " "))
out, err := exec.Command("bash", "-c", strings.Join(command, " ")).CombinedOutput()
if err != nil {
logrus.Error(err)
}
fmt.Println(string(out))
}