Skip to content

Commit

Permalink
feat: add tail logs (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
elfgzp committed Jan 5, 2021
1 parent 0b4048c commit 1fea136
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/app/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ var (
Mod: gocui.ModNone,
}

tailLogsAction = &guilib.Action{
Keys: keyMap[tailLogsActionName],
Name: tailLogsActionName,
Handler: tailLogsHandler,
Mod: gocui.ModNone,
}

scrollLogsAction = &guilib.Action{
Keys: keyMap[scrollLogsActionName],
Name: scrollLogsActionName,
Handler: scrollLogsHandler,
Mod: gocui.ModNone,
}

runPodAction = &guilib.Action{
Keys: keyMap[runPodActionName],
Name: runPodActionName,
Expand Down Expand Up @@ -255,6 +269,28 @@ var (
},
Action: *changePodLogsContainerAction,
},
&moreAction{
NeedSelectResource: false,
ShowAction: func(gui *guilib.Gui, view *guilib.View) bool {
resourceName := resourceViewName(getViewResourceName(activeView.Name))
if resourceName == "" {
return false
}
return resourceLogAble(resourceName)
},
Action: *tailLogsAction,
},
&moreAction{
NeedSelectResource: false,
ShowAction: func(gui *guilib.Gui, view *guilib.View) bool {
resourceName := resourceViewName(getViewResourceName(activeView.Name))
if resourceName == "" {
return false
}
return resourceLogAble(resourceName)
},
Action: *scrollLogsAction,
},
&moreAction{
NeedSelectResource: false,
ShowAction: func(gui *guilib.Gui, view *guilib.View) bool {
Expand Down
20 changes: 20 additions & 0 deletions pkg/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,26 @@ func changePodLogsContainerHandler(gui *guilib.Gui, view *guilib.View) error {
return nil
}

func tailLogsHandler(gui *guilib.Gui, view *guilib.View) error {
if err := view.SetState(ScrollingLogsStateKey, false, false); err != nil {
return err
}
if err := gui.FocusView(detailViewName, false); err != nil {
return err
}
return nil
}

func scrollLogsHandler(gui *guilib.Gui, view *guilib.View) error {
if err := view.SetState(ScrollingLogsStateKey, true, false); err != nil {
return err
}
if err := gui.FocusView(detailViewName, false); err != nil {
return err
}
return nil
}

func runPodHandler(gui *guilib.Gui, _ *guilib.View) error {
if err := showFilterDialog(
gui,
Expand Down
4 changes: 4 additions & 0 deletions pkg/app/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
deleteCustomResourcePanelActionName = "Delete custom resource panel"
containerExecCommandActionName = "Execute the command"
changePodLogsContainerActionName = "Change pod logs container"
tailLogsActionName = "Tail logs"
scrollLogsActionName = "Scroll logs"
runPodActionName = "Run a pod with an image"
changeContextActionName = "Change context"
)
Expand Down Expand Up @@ -89,6 +91,8 @@ var (
optionsDialogEnter: {gocui.KeyEnter},
inputDialogEnter: {gocui.KeyEnter},
changePodLogsContainerActionName: {'c'},
tailLogsActionName: {'t'},
scrollLogsActionName: {'s'},
runPodActionName: {'r'},
changeContextActionName: {'~'},
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/app/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ var (
Mod: gocui.ModNone,
},
changePodLogsContainerAction,
tailLogsAction,
scrollLogsAction,
newMoreActions(moreActionsMap[detailViewName]),
}),
}
Expand Down Expand Up @@ -317,6 +319,8 @@ var (
}

restartableResource = []string{"deployments", "statefulsets", "daemonsets"}

logAbleResource = []string{"deployment", "statefulset", "daemonset", "service", "pod"}
)

func getViewResourceName(viewName string) string {
Expand Down Expand Up @@ -517,3 +521,12 @@ func resourceRestartable(resource string) bool {
}
return false
}

func resourceLogAble(resource string) bool {
for _, logAble := range logAbleResource {
if resource == logAble {
return true
}
}
return false
}
13 changes: 13 additions & 0 deletions pkg/app/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ func onFocusClearSelected(gui *guilib.Gui, view *guilib.View) error {

func podLogsRender(gui *guilib.Gui, view *guilib.View) error {
// Todo: Fix chinese character of logs.
scrollLogs := true
if val, _ := view.GetState(ScrollingLogsStateKey); val != nil {
var ok bool
scrollLogs, ok = val.(bool)
if !ok {
scrollLogs = true
}
}

if !scrollLogs {
return nil
}

podView, err := gui.GetView(podViewName)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/app/statekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
filterInputValueStateKey = "filterInputValue" // value type: string
confirmValueStateKey = "confirmValue" // value type: string
logSinceTimeStateKey = "logSinceTime" // value type: time.Time
ScrollingLogsStateKey = "scrollingLogs" // value type: boolean
podContainersStateKey = "podContainers" // value type: []string
logContainerStateKey = "logContainer" // value type: string
)

0 comments on commit 1fea136

Please sign in to comment.