Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Lazykube
Commits
46c3a11f
Unverified
Commit
46c3a11f
authored
4 years ago
by
Elf
Committed by
GitHub
4 years ago
Browse files
Options
Download
Email Patches
Plain Diff
feat: change context (#47)
parent
a1958f93
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
pkg/app/action.go
+14
-0
pkg/app/action.go
pkg/app/handler.go
+24
-0
pkg/app/handler.go
pkg/app/keymap.go
+4
-1
pkg/app/keymap.go
pkg/app/panel.go
+1
-0
pkg/app/panel.go
pkg/app/render.go
+1
-4
pkg/app/render.go
pkg/kubecli/config/current_context.go
+26
-7
pkg/kubecli/config/current_context.go
pkg/kubecli/kubecli.go
+21
-1
pkg/kubecli/kubecli.go
with
91 additions
and
13 deletions
+91
-13
pkg/app/action.go
+
14
-
0
View file @
46c3a11f
...
...
@@ -178,6 +178,13 @@ var (
Mod
:
gocui
.
ModNone
,
}
changeContext
=
&
guilib
.
Action
{
Keys
:
keyMap
[
changeContextActionName
],
Name
:
changeContextActionName
,
Handler
:
changeContextHandler
,
Mod
:
gocui
.
ModNone
,
}
addCustomResourcePanelMoreAction
=
&
moreAction
{
NeedSelectResource
:
false
,
Action
:
*
addCustomResourcePanelAction
,
...
...
@@ -198,6 +205,12 @@ var (
Action
:
*
runPodAction
,
}
changeContextMoreAction
=
&
moreAction
{
NeedSelectResource
:
false
,
ShowAction
:
nil
,
Action
:
*
changeContext
,
}
commonResourceMoreActions
=
[]
*
moreAction
{
addCustomResourcePanelMoreAction
,
editResourceMoreAction
,
...
...
@@ -206,6 +219,7 @@ var (
moreActionsMap
=
map
[
string
][]
*
moreAction
{
clusterInfoViewName
:
{
addCustomResourcePanelMoreAction
,
changeContextMoreAction
,
},
namespaceViewName
:
append
(
commonResourceMoreActions
,
...
...
This diff is collapsed.
Click to expand it.
pkg/app/handler.go
+
24
-
0
View file @
46c3a11f
...
...
@@ -651,3 +651,27 @@ func runPodCommandInput(gui *guilib.Gui, namespace, podName, image string) error
}
return
nil
}
func
changeContextHandler
(
gui
*
guilib
.
Gui
,
view
*
guilib
.
View
)
error
{
if
err
:=
showFilterDialog
(
gui
,
"Selected a context to swicth."
,
func
(
confirmed
string
)
error
{
kubecli
.
Cli
.
SetCurrentContext
(
confirmed
)
gui
.
ReRenderAll
()
if
err
:=
gui
.
FocusView
(
clusterInfoViewName
,
false
);
err
!=
nil
{
return
err
}
return
nil
},
func
(
inputted
string
)
([]
string
,
error
)
{
return
kubecli
.
Cli
.
ListContexts
(),
nil
},
"No contexts."
,
false
,
);
err
!=
nil
{
return
err
}
return
nil
}
This diff is collapsed.
Click to expand it.
pkg/app/keymap.go
+
4
-
1
View file @
46c3a11f
...
...
@@ -48,6 +48,7 @@ const (
containerExecCommandActionName
=
"Execute the command"
changePodLogsContainerActionName
=
"Change pod logs container"
runPodActionName
=
"Run a pod with an image"
changeContextActionName
=
"Change context"
)
var
(
...
...
@@ -77,7 +78,8 @@ var (
moreActionsName
:
{
gocui
.
KeyF3
,
'm'
},
toFilteredViewAction
:
{
gocui
.
KeyTab
,
gocui
.
KeyArrowDown
},
toFilterInputAction
:
{
gocui
.
KeyTab
},
filteredNextLineAction
:
{
gocui
.
KeyArrowDown
},
filteredNextLineAction
:
{
gocui
.
KeyArrowDown
,
'j'
},
filteredPreviousLineAction
:
{
gocui
.
KeyArrowUp
,
'h'
},
confirmFilterInputAction
:
{
gocui
.
KeyEnter
},
switchConfirmDialogOpt
:
{
gocui
.
KeyTab
,
gocui
.
KeyArrowRight
,
gocui
.
KeyArrowLeft
,
'k'
,
'l'
},
confirmDialogEnter
:
{
gocui
.
KeyEnter
},
...
...
@@ -88,6 +90,7 @@ var (
inputDialogEnter
:
{
gocui
.
KeyEnter
},
changePodLogsContainerActionName
:
{
'c'
},
runPodActionName
:
{
'r'
},
changeContextActionName
:
{
'~'
},
}
)
...
...
This diff is collapsed.
Click to expand it.
pkg/app/panel.go
+
1
-
0
View file @
46c3a11f
...
...
@@ -34,6 +34,7 @@ var (
Actions
:
guilib
.
ToActionInterfaceArr
([]
*
guilib
.
Action
{
toNavigation
,
nextCyclicView
,
changeContext
,
newMoreActions
(
moreActionsMap
[
clusterInfoViewName
]),
}),
OnFocus
:
func
(
gui
*
guilib
.
Gui
,
view
*
guilib
.
View
)
error
{
...
...
This diff is collapsed.
Click to expand it.
pkg/app/render.go
+
1
-
4
View file @
46c3a11f
...
...
@@ -258,10 +258,7 @@ func navigationOnClick(gui *guilib.Gui, view *guilib.View) error {
func
renderClusterInfo
(
_
*
guilib
.
Gui
,
view
*
guilib
.
View
)
error
{
view
.
Clear
()
currentContext
,
err
:=
kubecli
.
Cli
.
CurrentContext
()
if
err
!=
nil
{
return
nil
}
currentContext
:=
kubecli
.
Cli
.
CurrentContext
()
if
_
,
err
:=
fmt
.
Fprintf
(
view
,
"Current Context: %s"
,
color
.
Green
.
Sprint
(
currentContext
));
err
!=
nil
{
return
err
...
...
This diff is collapsed.
Click to expand it.
pkg/kubecli/config/current_context.go
+
26
-
7
View file @
46c3a11f
package
config
import
"k8s.io/client-go/tools/clientcmd"
import
(
"k8s.io/client-go/tools/clientcmd"
clientcmdapi
"k8s.io/client-go/tools/clientcmd/api"
)
func
CurrentContext
()
(
string
,
error
)
{
var
(
config
*
clientcmdapi
.
Config
)
func
init
()
{
var
err
error
pathOptions
:=
clientcmd
.
NewDefaultPathOptions
()
config
,
err
:
=
pathOptions
.
GetStartingConfig
()
config
,
err
=
pathOptions
.
GetStartingConfig
()
if
err
!=
nil
{
return
""
,
err
panic
(
err
)
}
}
func
CurrentContext
()
string
{
return
config
.
CurrentContext
}
func
SetCurrentContext
(
context
string
)
{
config
.
CurrentContext
=
context
}
if
config
.
CurrentContext
==
""
{
return
"current-context is not set"
,
nil
func
ListContexts
()
[]
string
{
contexts
:=
make
([]
string
,
0
)
for
name
,
_
:=
range
config
.
Contexts
{
contexts
=
append
(
contexts
,
name
)
}
return
con
fig
.
CurrentContext
,
nil
return
con
texts
}
This diff is collapsed.
Click to expand it.
pkg/kubecli/kubecli.go
+
21
-
1
View file @
46c3a11f
...
...
@@ -31,6 +31,7 @@ func init() {
type
KubeCLI
struct
{
factory
util
.
Factory
namespace
*
string
context
*
string
}
type
Cmd
struct
{
...
...
@@ -63,14 +64,17 @@ func (c *Cmd) SetFlag(name, value string) *Cmd {
func
NewKubeCLI
()
*
KubeCLI
{
namespace
:=
""
context
:=
config
.
CurrentContext
()
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
(
true
)
.
WithDeprecatedPasswordFlag
()
kubeConfigFlags
.
Namespace
=
&
namespace
kubeConfigFlags
.
Context
=
&
context
matchVersionKubeConfigFlags
:=
util
.
NewMatchVersionFlags
(
kubeConfigFlags
)
k
:=
&
KubeCLI
{
factory
:
util
.
NewFactory
(
matchVersionKubeConfigFlags
),
namespace
:
&
namespace
,
context
:
&
context
,
}
return
k
}
...
...
@@ -78,12 +82,24 @@ func NewKubeCLI() *KubeCLI {
func
(
cli
*
KubeCLI
)
SetNamespace
(
namespace
string
)
{
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
(
true
)
.
WithDeprecatedPasswordFlag
()
kubeConfigFlags
.
Namespace
=
&
namespace
kubeConfigFlags
.
Context
=
cli
.
context
matchVersionKubeConfigFlags
:=
util
.
NewMatchVersionFlags
(
kubeConfigFlags
)
cli
.
factory
=
util
.
NewFactory
(
matchVersionKubeConfigFlags
)
cli
.
namespace
=
&
namespace
}
func
(
cli
*
KubeCLI
)
SetCurrentContext
(
context
string
)
{
config
.
SetCurrentContext
(
context
)
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
(
true
)
.
WithDeprecatedPasswordFlag
()
kubeConfigFlags
.
Namespace
=
cli
.
namespace
kubeConfigFlags
.
Context
=
&
context
matchVersionKubeConfigFlags
:=
util
.
NewMatchVersionFlags
(
kubeConfigFlags
)
cli
.
factory
=
util
.
NewFactory
(
matchVersionKubeConfigFlags
)
cli
.
context
=
&
context
}
func
(
cli
*
KubeCLI
)
WithNamespace
(
namespace
string
)
*
KubeCLI
{
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
(
true
)
.
WithDeprecatedPasswordFlag
()
kubeConfigFlags
.
Namespace
=
&
namespace
...
...
@@ -101,10 +117,14 @@ func (cli *KubeCLI) Namespace() string {
return
*
cli
.
namespace
}
func
(
cli
*
KubeCLI
)
CurrentContext
()
(
string
,
error
)
{
func
(
cli
*
KubeCLI
)
CurrentContext
()
string
{
return
config
.
CurrentContext
()
}
func
(
cli
*
KubeCLI
)
ListContexts
()
[]
string
{
return
config
.
ListContexts
()
}
func
(
cli
*
KubeCLI
)
ClusterInfo
()
(
string
,
error
)
{
return
clusterinfo
.
ClusterInfo
(
cli
.
factory
)
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment