Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use channels to pass data to the debug tui #75

Merged
merged 15 commits into from
Oct 28, 2024
50 changes: 38 additions & 12 deletions pkg/app/execontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

const (
ofJSON = "json"
ofText = "text"
ofJSON = "json"
ofText = "text"
ofSubscription = "subscription"
)

type ExecutionContext struct {
Expand Down Expand Up @@ -122,20 +123,34 @@ func NewExecutionContext(
}

type Output struct {
CmdName string
Quiet bool
OutputFormat string
DataChannels map[string]chan interface{}
CmdName string
Quiet bool
OutputFormat string
DataChannels map[string]chan interface{}
internalDataCh chan interface{}
}

func NewOutput(cmdName string, quiet bool, outputFormat string, channels map[string]chan interface{}) *Output {
ref := &Output{
CmdName: cmdName,
Quiet: quiet,
OutputFormat: outputFormat,
DataChannels: channels,
CmdName: cmdName,
Quiet: quiet,
OutputFormat: outputFormat,
DataChannels: channels,
internalDataCh: make(chan interface{}),
}

// We want to listen to the internal channel for any data
// And dump it onto the appropriate DataChannels
go func() {
for data := range ref.internalDataCh {
if data != nil {
for _, ch := range ref.DataChannels {
ch <- data
}
}
}
}()

return ref
}

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

}

func (ref *Output) Data(channelKey string, data interface{}) {
if ch, exists := ref.DataChannels[channelKey]; exists {
ch <- data // Send data to the corresponding channel
fmt.Printf("Data sent to channel '%s': %v\n", channelKey, data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use logrus log instead of fmt.Printf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Will update / remove.

} else {
fmt.Printf("Channel for channelKey '%s' not found\n", channelKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use logrus log instead of fmt.Printf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Will update / remove.

}
}

func (ref *Output) State(state string, params ...OutVars) {
if ref.Quiet {
return
Expand Down Expand Up @@ -344,7 +368,8 @@ var (
)

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

Expand Down Expand Up @@ -379,7 +404,8 @@ func (ref *Output) Info(infoType string, params ...OutVars) {
fmt.Println(string(jsonData))
case ofText:
fmt.Printf("cmd=%s info=%s%s%s\n", ref.CmdName, itcolor(infoType), sep, data)

case ofSubscription:
ref.internalDataCh <- msg // Send data to the internal channel
default:
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.OutputFormat)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/app/master/command/cliflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ const (
FlagContainerDNSSearchUsage = "Add a dns search domain for unqualified hostnames analyzing image at runtime"
FlagMountUsage = "Mount volume analyzing image"
FlagDeleteFatImageUsage = "Delete generated fat image requires --dockerfile flag"

// TUI Related flags
FlagTUI = "tui"
FlagTUIUsage = "Enable terminal user interface mode"
)

// Container runtime command flag names and usage descriptions
Expand Down Expand Up @@ -1026,6 +1030,12 @@ var CommonFlags = map[string]cli.Flag{
Usage: FlagRuntimeUsage,
EnvVars: []string{"DSLIM_CRT_NAME"},
},
// TUI Mode
FlagTUI: &cli.BoolFlag{
Name: FlagTUI,
Usage: FlagTUIUsage,
EnvVars: []string{"DSLIM_TUI"},
},
}

//var CommonFlags
Expand Down
14 changes: 14 additions & 0 deletions pkg/app/master/command/debug/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mintoolkit/mint/pkg/app"
"github.com/mintoolkit/mint/pkg/app/master/command"
"github.com/mintoolkit/mint/pkg/app/master/tui"
"github.com/mintoolkit/mint/pkg/crt"
)

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

func ParseNameValueList(list []string) []NVPair {
Expand Down Expand Up @@ -166,9 +169,19 @@ var CLI = &cli.Command{
cflag(FlagRunPrivileged),
cflag(FlagSecurityContextFromTarget),
cflag(FlagFallbackToTargetUser),
command.Cflag(command.FlagTUI),
},
Action: func(ctx *cli.Context) error {
gcvalues := command.GlobalFlagValues(ctx)

// If we stick with this approach, the user should be communicated to
// use `--tui` as a standalone flag for `debug`
if ctx.Bool(command.FlagTUI) {
initialTUI := InitialTUI(true, gcvalues)
tui.RunTUI(initialTUI, true)
return nil
}

xc := app.NewExecutionContext(
Name,
gcvalues.QuietCLIMode,
Expand Down Expand Up @@ -209,6 +222,7 @@ var CLI = &cli.Command{
DoRunPrivileged: ctx.Bool(FlagRunPrivileged),
UseSecurityContextFromTarget: ctx.Bool(FlagSecurityContextFromTarget),
DoFallbackToTargetUser: ctx.Bool(FlagFallbackToTargetUser),
TUI: ctx.Bool(command.FlagTUI),
}

if commandParams.ActionListNamespaces &&
Expand Down
1 change: 1 addition & 0 deletions pkg/app/master/command/debug/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func OnCommand(

resolved := command.ResolveAutoRuntime(commandParams.Runtime)
logger.Tracef("runtime.handler: rt=%s resolved=%s", commandParams.Runtime, resolved)

switch resolved {
case crt.DockerRuntime:
client, err := dockerclient.New(gparams.ClientConfig)
Expand Down
Loading
Loading