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" ) var user string var userFlag = &cli.StringFlag{ Name: "user", Value: "", Destination: &user, } var noTTY bool var noTTYFlag = &cli.BoolFlag{ Name: "no-tty", Value: false, Destination: &noTTY, } var appRunCommand = &cli.Command{ Name: "run", Flags: []cli.Flag{ noTTYFlag, userFlag, }, ArgsUsage: " ...", 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 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 }, }