Skip to content

Commit

Permalink
yoke: atc command creates a debug file if -debug-file is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Jan 11, 2025
1 parent d28f75d commit d0e58d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
31 changes: 28 additions & 3 deletions cmd/yoke/cmd_atc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"fmt"
"runtime"
"sync"
Expand All @@ -17,17 +18,41 @@ import (
"k8s.io/client-go/dynamic"

"github.com/yokecd/yoke/internal/atc/tower"
"github.com/yokecd/yoke/internal/home"
"github.com/yokecd/yoke/internal/k8s"
"github.com/yokecd/yoke/pkg/apis/airway/v1alpha1"
)

func ATC(ctx context.Context) error {
client, err := k8s.NewClientFromKubeConfig(home.Kubeconfig)
type ATCParams struct {
GlobalSettings
Debug string
}

func GetAtcParams(settings GlobalSettings, args []string) ATCParams {
flagset := flag.NewFlagSet("atc", flag.ExitOnError)

params := ATCParams{GlobalSettings: settings}

RegisterGlobalFlags(flagset, &params.GlobalSettings)

flagset.StringVar(&params.Debug, "debug-file", "", "debug file")

flagset.Parse(args)

return params
}

func ATC(ctx context.Context, params ATCParams) error {
client, err := k8s.NewClientFromKubeConfig(params.KubeConfigPath)
if err != nil {
return fmt.Errorf("failed to initialize kube client: %w", err)
}

if params.Debug != "" {
if err := tower.SetupDebugFile(params.Debug); err != nil {
return fmt.Errorf("failed to setup debug file: %w", err)
}
}

app := tea.NewProgram(
tower.ATCDashboard{
Content: tower.MakeAirwayListView(tea.WindowSizeMsg{}),
Expand Down
2 changes: 1 addition & 1 deletion cmd/yoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func run() error {

switch cmd := flag.Arg(0); cmd {
case "atc":
return ATC(ctx)
return ATC(ctx, GetAtcParams(settings, subcmdArgs))
case "takeoff", "up", "apply":
{
var source io.Reader
Expand Down
29 changes: 21 additions & 8 deletions internal/atc/tower/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tower
import (
"fmt"
"os"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -18,12 +19,20 @@ var banner = cyanSyle.Render(` _ _ _____

var debugFile *os.File

func init() {
_ = os.Remove("./debug.txt")
debugFile, _ = os.OpenFile("./debug.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o644)
if _, err := debugFile.WriteString("DEBUG.txt\n\n"); err != nil {
panic(err)
func debugf(format string, args ...any) {
if debugFile == nil {
return
}
_, _ = fmt.Fprintf(debugFile, format+"\n", args...)
}

func SetupDebugFile(dst string) (err error) {
debugFile, err = os.OpenFile(dst, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o644)
if err != nil {
return err
}
_, err = fmt.Fprintf(debugFile, "debug session start: %s\n\n", time.Now().Format(time.RFC3339Nano))
return err
}

type Commands struct {
Expand All @@ -43,18 +52,22 @@ func (dashboard ATCDashboard) Init() tea.Cmd {
}

func (dashboard ATCDashboard) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
fmt.Fprintf(debugFile, "%#v\n\n", msg)
debugFile.Sync()
debugf("%v", func() any {
if value, ok := msg.(fmt.Stringer); ok {
return value.String()
}
return msg
}())

switch msg := msg.(type) {

case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
return dashboard, tea.Quit
case tea.KeyCtrlZ:
return dashboard, tea.Suspend
}

case ExecMsg:
return dashboard, msg(dashboard.Commands)
}
Expand Down

0 comments on commit d0e58d8

Please sign in to comment.