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

Add tui log file #6

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ __debug*
.vscode/
cattle-drive
dist/

# tui log file
cattle-drive.log

10 changes: 9 additions & 1 deletion cli/cmds/interactive/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var (
source string
target string
logFilePath string
interactiveFlags = []cli.Flag{
&cli.StringFlag{
Name: "source",
Expand All @@ -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",
},
}
)

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion cli/cmds/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cluster/tui/cluster.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package tui

import (
"bytes"
"context"
"fmt"
"galal-hussein/cattle-drive/pkg/cluster/tui/constants"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/tui/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package constants
import (
"galal-hussein/cattle-drive/pkg/client"
"galal-hussein/cattle-drive/pkg/cluster"
"os"

Choose a reason for hiding this comment

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

Looks like this file needs to be go fmt'd or run goimports.


tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -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 */
Expand Down
16 changes: 15 additions & 1 deletion pkg/cluster/tui/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"context"
"fmt"

Choose a reason for hiding this comment

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

Looks like this file needs to have go fmt / goimport ran on it.

"galal-hussein/cattle-drive/pkg/cluster"
"galal-hussein/cattle-drive/pkg/cluster/tui/constants"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/cluster/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down