github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/internal/apiimpl/intercept.go (about)

     1  package apiimpl
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/api"
     8  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/intercept"
     9  )
    10  
    11  func toInterceptCmd(rq *api.InterceptRequest, ih api.InterceptHandler) *intercept.Command {
    12  	cmd := &intercept.Command{
    13  		Name:           rq.Name,
    14  		AgentName:      rq.WorkloadName,
    15  		Port:           rq.Port,
    16  		ServiceName:    rq.ServiceName,
    17  		LocalMountPort: rq.LocalMountPort,
    18  		Replace:        rq.Replace,
    19  		EnvFile:        rq.EnvFile,
    20  		EnvJSON:        rq.EnvJSON,
    21  		Silent:         rq.Silent,
    22  		ToPod:          toStrings(rq.ToPod),
    23  		Mechanism:      "tcp",
    24  	}
    25  	if cmd.Name == "" {
    26  		cmd.Name = cmd.AgentName
    27  	}
    28  
    29  	if rq.Address.IsValid() {
    30  		cmd.Address = rq.Address.String()
    31  	} else {
    32  		cmd.Address = "127.0.0.1"
    33  	}
    34  	switch ih := ih.(type) {
    35  	case nil:
    36  	case api.CmdHandler:
    37  		cmd.Cmdline = ih.Cmdline
    38  		cmd.Mount, cmd.MountSet = toCmdMount(ih.MountPoint)
    39  	case api.DockerRunInterceptHandler:
    40  		cmd.DockerRun = true
    41  		cmd.Cmdline = appendOptions(ih.Options, cmd.Cmdline)
    42  		cmd.Cmdline = append(cmd.Cmdline, ih.Image)
    43  		cmd.Cmdline = append(cmd.Cmdline, ih.Arguments...)
    44  		cmd.Mount = strconv.FormatBool(ih.Mount)
    45  		cmd.MountSet = true
    46  		cmd.WaitMessage = "type <ctrl>-C to end..."
    47  	case api.DockerBuildInterceptHandler:
    48  		cmd.DockerRun = true
    49  		if ih.Debug {
    50  			cmd.DockerDebug = ih.Context
    51  			cmd.WaitMessage = "waiting for a debugger frontend to attach..."
    52  		} else {
    53  			cmd.DockerBuild = ih.Context
    54  			cmd.WaitMessage = "type <ctrl>-C to end..."
    55  		}
    56  		cmd.DockerBuildOptions = ih.BuildOptions
    57  		cmd.Cmdline = appendOptions(ih.Options, cmd.Cmdline)
    58  		cmd.Cmdline = append(cmd.Cmdline, "IMAGE")
    59  		cmd.Cmdline = append(cmd.Cmdline, ih.Arguments...)
    60  		cmd.Mount = strconv.FormatBool(ih.Mount)
    61  		cmd.MountSet = true
    62  	}
    63  	return cmd
    64  }
    65  
    66  func toCmdMount(mountPoint string) (string, bool) {
    67  	switch mountPoint {
    68  	case "", "false":
    69  		return "false", true
    70  	case "true":
    71  		return "true", false
    72  	default:
    73  		return mountPoint, true
    74  	}
    75  }
    76  
    77  func appendOptions(opts []string, flags []string) []string {
    78  	for _, opt := range opts {
    79  		if n := strings.IndexByte(opt, '='); n > 0 {
    80  			flags = append(flags, "--"+opt[:n], opt[n+1:])
    81  		} else {
    82  			flags = append(flags, "--"+opt)
    83  		}
    84  	}
    85  	return flags
    86  }