Skip to content

Commit bcb4e34

Browse files
authored
Use channels to pass data to the debug tui (#75)
* Move data channel out of handler Signed-off-by: Evan Harris <echarris@smcm.edu> * Mark TODO Signed-off-by: Evan Harris <echarris@smcm.edu> * Add debug key bindings Signed-off-by: Evan Harris <echarris@smcm.edu> * Add TUI flags Signed-off-by: Evan Harris <echarris@smcm.edu> * Add TUI flags to debug cli Signed-off-by: Evan Harris <echarris@smcm.edu> * Add debug event Signed-off-by: Evan Harris <echarris@smcm.edu> * Implement debug tui with channel based data flow Signed-off-by: Evan Harris <echarris@smcm.edu> * Open debug tui from home tui Signed-off-by: Evan Harris <echarris@smcm.edu> * Rm uused flags Signed-off-by: Evan Harris <echarris@smcm.edu> * Allow debug --tui invocation with no other flags Signed-off-by: Evan Harris <echarris@smcm.edu> * Support use of debug --tui to render debuggable containers Signed-off-by: Evan Harris <echarris@smcm.edu> * Fix spelling Signed-off-by: Evan Harris <echarris@smcm.edu> * Change name from data -> subscription Signed-off-by: Evan Harris <echarris@smcm.edu> * Improve comment and fix typo Signed-off-by: Evan Harris <echarris@smcm.edu> --------- Signed-off-by: Evan Harris <echarris@smcm.edu>
1 parent 7081b05 commit bcb4e34

File tree

11 files changed

+351
-88
lines changed

11 files changed

+351
-88
lines changed

pkg/app/execontext.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616
)
1717

1818
const (
19-
ofJSON = "json"
20-
ofText = "text"
19+
ofJSON = "json"
20+
ofText = "text"
21+
ofSubscription = "subscription"
2122
)
2223

2324
type ExecutionContext struct {
@@ -122,20 +123,34 @@ func NewExecutionContext(
122123
}
123124

124125
type Output struct {
125-
CmdName string
126-
Quiet bool
127-
OutputFormat string
128-
DataChannels map[string]chan interface{}
126+
CmdName string
127+
Quiet bool
128+
OutputFormat string
129+
DataChannels map[string]chan interface{}
130+
internalDataCh chan interface{}
129131
}
130132

131133
func NewOutput(cmdName string, quiet bool, outputFormat string, channels map[string]chan interface{}) *Output {
132134
ref := &Output{
133-
CmdName: cmdName,
134-
Quiet: quiet,
135-
OutputFormat: outputFormat,
136-
DataChannels: channels,
135+
CmdName: cmdName,
136+
Quiet: quiet,
137+
OutputFormat: outputFormat,
138+
DataChannels: channels,
139+
internalDataCh: make(chan interface{}),
137140
}
138141

142+
// We want to listen to the internal channel for any data
143+
// And dump it onto the appropriate DataChannels
144+
go func() {
145+
for data := range ref.internalDataCh {
146+
if data != nil {
147+
for _, ch := range ref.DataChannels {
148+
ch <- data
149+
}
150+
}
151+
}
152+
}()
153+
139154
return ref
140155
}
141156

@@ -272,6 +287,15 @@ func (ref *Output) Message(data string) {
272287

273288
}
274289

290+
func (ref *Output) Data(channelKey string, data interface{}) {
291+
if ch, exists := ref.DataChannels[channelKey]; exists {
292+
ch <- data // Send data to the corresponding channel
293+
fmt.Printf("Data sent to channel '%s': %v\n", channelKey, data)
294+
} else {
295+
fmt.Printf("Channel for channelKey '%s' not found\n", channelKey)
296+
}
297+
}
298+
275299
func (ref *Output) State(state string, params ...OutVars) {
276300
if ref.Quiet {
277301
return
@@ -344,7 +368,8 @@ var (
344368
)
345369

346370
func (ref *Output) Info(infoType string, params ...OutVars) {
347-
if ref.Quiet {
371+
// TODO - carry this pattern to other Output methods
372+
if ref.Quiet && ref.OutputFormat != ofSubscription {
348373
return
349374
}
350375

@@ -379,7 +404,8 @@ func (ref *Output) Info(infoType string, params ...OutVars) {
379404
fmt.Println(string(jsonData))
380405
case ofText:
381406
fmt.Printf("cmd=%s info=%s%s%s\n", ref.CmdName, itcolor(infoType), sep, data)
382-
407+
case ofSubscription:
408+
ref.internalDataCh <- msg // Send data to the internal channel
383409
default:
384410
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.OutputFormat)
385411
}

pkg/app/master/command/cliflags.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ const (
312312
FlagContainerDNSSearchUsage = "Add a dns search domain for unqualified hostnames analyzing image at runtime"
313313
FlagMountUsage = "Mount volume analyzing image"
314314
FlagDeleteFatImageUsage = "Delete generated fat image requires --dockerfile flag"
315+
316+
// TUI Related flags
317+
FlagTUI = "tui"
318+
FlagTUIUsage = "Enable terminal user interface mode"
315319
)
316320

317321
// Container runtime command flag names and usage descriptions
@@ -1026,6 +1030,12 @@ var CommonFlags = map[string]cli.Flag{
10261030
Usage: FlagRuntimeUsage,
10271031
EnvVars: []string{"DSLIM_CRT_NAME"},
10281032
},
1033+
// TUI Mode
1034+
FlagTUI: &cli.BoolFlag{
1035+
Name: FlagTUI,
1036+
Usage: FlagTUIUsage,
1037+
EnvVars: []string{"DSLIM_TUI"},
1038+
},
10291039
}
10301040

10311041
//var CommonFlags

pkg/app/master/command/debug/cli.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/mintoolkit/mint/pkg/app"
1010
"github.com/mintoolkit/mint/pkg/app/master/command"
11+
"github.com/mintoolkit/mint/pkg/app/master/tui"
1112
"github.com/mintoolkit/mint/pkg/crt"
1213
)
1314

@@ -86,6 +87,8 @@ type CommandParams struct {
8687
UseSecurityContextFromTarget bool
8788
/// fallback to using target container user if it's non-root (mostly for kubernetes)
8889
DoFallbackToTargetUser bool
90+
// `debug --tui` use mode`
91+
TUI bool
8992
}
9093

9194
func ParseNameValueList(list []string) []NVPair {
@@ -166,9 +169,19 @@ var CLI = &cli.Command{
166169
cflag(FlagRunPrivileged),
167170
cflag(FlagSecurityContextFromTarget),
168171
cflag(FlagFallbackToTargetUser),
172+
command.Cflag(command.FlagTUI),
169173
},
170174
Action: func(ctx *cli.Context) error {
171175
gcvalues := command.GlobalFlagValues(ctx)
176+
177+
// If we stick with this approach, the user should be communicated to
178+
// use `--tui` as a standalone flag for `debug`
179+
if ctx.Bool(command.FlagTUI) {
180+
initialTUI := InitialTUI(true, gcvalues)
181+
tui.RunTUI(initialTUI, true)
182+
return nil
183+
}
184+
172185
xc := app.NewExecutionContext(
173186
Name,
174187
gcvalues.QuietCLIMode,
@@ -209,6 +222,7 @@ var CLI = &cli.Command{
209222
DoRunPrivileged: ctx.Bool(FlagRunPrivileged),
210223
UseSecurityContextFromTarget: ctx.Bool(FlagSecurityContextFromTarget),
211224
DoFallbackToTargetUser: ctx.Bool(FlagFallbackToTargetUser),
225+
TUI: ctx.Bool(command.FlagTUI),
212226
}
213227

214228
if commandParams.ActionListNamespaces &&

pkg/app/master/command/debug/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func OnCommand(
8282

8383
resolved := command.ResolveAutoRuntime(commandParams.Runtime)
8484
logger.Tracef("runtime.handler: rt=%s resolved=%s", commandParams.Runtime, resolved)
85+
8586
switch resolved {
8687
case crt.DockerRuntime:
8788
client, err := dockerclient.New(gparams.ClientConfig)

0 commit comments

Comments
 (0)