WIP app run command

This commit is contained in:
2021-08-29 18:21:30 +02:00
parent bef2c862bf
commit 8cc691ab52
7 changed files with 795 additions and 2 deletions

View File

@ -1,6 +1,16 @@
package app
import (
"context"
"errors"
"fmt"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -25,4 +35,97 @@ var appRunCommand = &cli.Command{
userFlag,
},
ArgsUsage: "<service> <args>...",
Usage: "run a command in a service container",
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
}
if c.Args().Len() < 2 {
internal.ShowSubcommandHelpAndError(c, errors.New("no <service> provided"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
host := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
serviceName := c.Args().Get(1)
filters := filters.NewArgs()
filters.Add("name", fmt.Sprintf("%s_%s", appEnv.StackName(), serviceName))
containers, err := cl.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
if len(containers) > 1 {
logrus.Fatalf("expected 1 container but got %d", len(containers))
}
cmd := c.Args().Slice()[2:]
execCreateOpts := types.ExecConfig{
//AttachStderr: true,
AttachStdin: true,
//AttachStdout: true,
Cmd: cmd,
Detach: false,
Tty: true,
}
if user != "" {
execCreateOpts.User = user
}
if noTTY {
execCreateOpts.Tty = false
}
// container := containers[0]
// idResp, err := cl.ContainerExecCreate(ctx, container.ID, execCreateOpts)
// if err != nil {
// logrus.Fatal(err)
// }
// execAttachOpts := types.ExecStartCheck{Detach: false, Tty: true}
// hResp, err := cl.ContainerExecAttach(ctx, idResp.ID, execAttachOpts)
// if err != nil {
// logrus.Fatal(err)
// }
// defer hResp.Close()
// var outBuf, errBuf bytes.Buffer
// outputDone := make(chan error)
// go func() {
// _, err = stdcopy.StdCopy(&outBuf, &errBuf, hResp.Reader)
// outputDone <- err
// }()
// select {
// case err := <-outputDone:
// if err != nil {
// logrus.Fatal(err)
// }
// break
// case <-ctx.Done():
// break
// }
// iresp, err := cl.ContainerExecInspect(ctx, idResp.ID)
// if err != nil {
// logrus.Fatal(err)
// }
return nil
},
}