diff --git a/.gitignore b/.gitignore index a54220d..9ad5af7 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ __debug* .vscode/ cattle-drive dist/ + +# tui log file +cattle-drive.log + diff --git a/cli/cmds/interactive/interactive.go b/cli/cmds/interactive/interactive.go index 57e3067..68acecf 100644 --- a/cli/cmds/interactive/interactive.go +++ b/cli/cmds/interactive/interactive.go @@ -18,6 +18,7 @@ import ( var ( source string target string + logFilePath string interactiveFlags = []cli.Flag{ &cli.StringFlag{ Name: "source", @@ -31,6 +32,13 @@ var ( Destination: &target, Aliases: []string{"t"}, }, + &cli.StringFlag{ + Name: "log-file", + Usage: "log file path", + Destination: &logFilePath, + Aliases: []string{"l"}, + Value: "cattle-drive.log", + }, } ) @@ -111,5 +119,5 @@ func migrate(clx *cli.Context) error { return err } cmds.Spinner.Stop() - return tui.StartTea(sc, tc, cl) + return tui.StartTea(sc, tc, cl, logFilePath) } diff --git a/cli/cmds/status/status.go b/cli/cmds/status/status.go index 0112b4b..0a0911e 100644 --- a/cli/cmds/status/status.go +++ b/cli/cmds/status/status.go @@ -36,7 +36,7 @@ var ( func NewCommand() *cli.Command { return &cli.Command{ Name: "status", - Usage: "status command", + Usage: "Status command", Action: status, Flags: append(cmds.CommonFlags, statusFlags...), } diff --git a/pkg/cluster/tui/cluster.go b/pkg/cluster/tui/cluster.go index 8b56677..eb1d401 100644 --- a/pkg/cluster/tui/cluster.go +++ b/pkg/cluster/tui/cluster.go @@ -1,8 +1,8 @@ package tui import ( - "bytes" "context" + "fmt" "galal-hussein/cattle-drive/pkg/cluster/tui/constants" "strings" "time" @@ -140,11 +140,13 @@ func (m Model) View() string { } func (m *Model) migrateCluster(ctx context.Context) { - var buf bytes.Buffer - if err := constants.SC.Migrate(ctx, constants.Lclient, constants.TC, &buf); err != nil { + fmt.Fprintf(&constants.LogFile, "[%s] initiating cluster objects migrate:\n", time.Now().String()) + if err := constants.SC.Migrate(ctx, constants.Lclient, constants.TC, &constants.LogFile); err != nil { + fmt.Fprintf(&constants.LogFile, "[%s] [error] %v\n", time.Now().String(), err) m.Update(tea.Quit()) } if err := updateClusters(ctx); err != nil { + fmt.Fprintf(&constants.LogFile, "[%s] [error] %v\n", time.Now().String(), err) m.Update(tea.Quit()) } constants.Migratedch <- true diff --git a/pkg/cluster/tui/constants/constants.go b/pkg/cluster/tui/constants/constants.go index a9f30ee..e0d69a2 100644 --- a/pkg/cluster/tui/constants/constants.go +++ b/pkg/cluster/tui/constants/constants.go @@ -3,6 +3,7 @@ package constants import ( "galal-hussein/cattle-drive/pkg/client" "galal-hussein/cattle-drive/pkg/cluster" + "os" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -44,7 +45,10 @@ var ( Lclient *client.Clients // WindowSize store the size of the terminal window WindowSize tea.WindowSizeMsg + // Migratedch all object chan Migratedch = make(chan bool) + // logFile + LogFile os.File ) /* STYLING */ diff --git a/pkg/cluster/tui/objects.go b/pkg/cluster/tui/objects.go index 2a5a378..6aff999 100644 --- a/pkg/cluster/tui/objects.go +++ b/pkg/cluster/tui/objects.go @@ -2,6 +2,7 @@ package tui import ( "context" + "fmt" "galal-hussein/cattle-drive/pkg/cluster" "galal-hussein/cattle-drive/pkg/cluster/tui/constants" "strings" @@ -184,7 +185,11 @@ func status(name string, migrated, diff bool) (string, constants.MigrationStatus } func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { - var msg string + var ( + objectMigrated bool + msg string + ) + switch i.objType { case constants.ProjectType: if i.status == constants.NotMigratedStatus { @@ -193,6 +198,7 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { if err := constants.Lclient.Projects.Create(ctx, constants.TC.Obj.Name, p.Obj, nil, v1.CreateOptions{}); err != nil { return nil, err } + objectMigrated = true if err := updateClusters(ctx); err != nil { return nil, err } @@ -205,6 +211,7 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { if _, err := constants.TC.Client.Namespace.Create(ns.Obj); err != nil { return nil, err } + objectMigrated = true if err := updateClusters(ctx); err != nil { return nil, err } @@ -218,6 +225,7 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { if err := constants.Lclient.ProjectRoleTemplateBindings.Create(ctx, prtb.ProjectName, prtb.Obj, nil, v1.CreateOptions{}); err != nil { return nil, err } + objectMigrated = true if err := updateClusters(ctx); err != nil { return nil, err } @@ -230,6 +238,7 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { if err := constants.Lclient.ClusterRoleTemplateBindings.Create(ctx, constants.TC.Obj.Name, crtb.Obj, nil, v1.CreateOptions{}); err != nil { return nil, err } + objectMigrated = true if err := updateClusters(ctx); err != nil { return nil, err } @@ -243,6 +252,7 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { if err := constants.TC.Client.ClusterRepos.Create(ctx, constants.TC.Obj.Name, repo.Obj, nil, v1.CreateOptions{}); err != nil { return nil, err } + objectMigrated = true if err := updateClusters(ctx); err != nil { return nil, err } @@ -251,6 +261,9 @@ func (m *Objects) migrateObject(ctx context.Context, i item) (tea.Msg, error) { } m.mode = migrated + if objectMigrated { + fmt.Fprintf(&constants.LogFile, "[%s] migrated object [%s/%s]\n", time.Now().String(), i.objType, i.title) + } return msg, nil } @@ -264,6 +277,7 @@ func updateClusters(ctx context.Context) error { if err := constants.SC.Compare(ctx, constants.Lclient, constants.TC); err != nil { return err } + fmt.Fprintf(&constants.LogFile, "[%s] successfully updated cluster [%s]\n", time.Now().String(), constants.SC.Obj.Spec.DisplayName) return nil } diff --git a/pkg/cluster/tui/tui.go b/pkg/cluster/tui/tui.go index c65571f..395bb0c 100644 --- a/pkg/cluster/tui/tui.go +++ b/pkg/cluster/tui/tui.go @@ -11,13 +11,14 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -func StartTea(sc, tc *cluster.Cluster, client *client.Clients) error { - if f, err := tea.LogToFile("debug.log", "help"); err != nil { +func StartTea(sc, tc *cluster.Cluster, client *client.Clients, logFilePath string) error { + if f, err := tea.LogToFile(logFilePath, "help"); err != nil { fmt.Println("Couldn't open a file for logging:", err) os.Exit(1) } else { + constants.LogFile = *f defer func() { - err = f.Close() + err = constants.LogFile.Close() if err != nil { log.Fatal(err) }