From d688754862d077d8d4e03b764dcc01a46d34b005 Mon Sep 17 00:00:00 2001 From: Ted Teng Date: Thu, 27 Oct 2022 09:18:52 +0800 Subject: [PATCH] options and history tmpl --- pkg/cmd/env/templates/history.tmpl | 8 ++++---- pkg/cmd/target/history.go | 20 ++++++++------------ pkg/cmd/target/history_test.go | 7 +++---- pkg/cmd/target/target.go | 2 +- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pkg/cmd/env/templates/history.tmpl b/pkg/cmd/env/templates/history.tmpl index d94cc43d..caa86bcd 100644 --- a/pkg/cmd/env/templates/history.tmpl +++ b/pkg/cmd/env/templates/history.tmpl @@ -1,15 +1,15 @@ {{define "bash"}} -gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal | bash +{{template "eval-cmd" dict "shell" .shell "cmd" "gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal"}} {{template "usage-hint" .__meta}}{{end}} {{define "zsh"}} -gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal | zsh +{{template "eval-cmd" dict "shell" .shell "cmd" "gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal"}} {{template "usage-hint" .__meta}}{{end}} {{define "fish"}} -gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal | fish +{{template "eval-cmd" dict "shell" .shell "cmd" "gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal"}} {{template "usage-hint" .__meta}}{{end}} {{define "powershell"}} -gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal | Out-String | Invoke-Expression +{{"gardenctl target history | fzf --tac --no-sort --height 40% --layout reverse --info inline --border --preview 'echo {}' --preview-window up,1,border-horizontal | Out-String | Invoke-Expression" }} {{template "usage-hint" .__meta}}{{end}} \ No newline at end of file diff --git a/pkg/cmd/target/history.go b/pkg/cmd/target/history.go index e1ba3fdf..992c81bf 100644 --- a/pkg/cmd/target/history.go +++ b/pkg/cmd/target/history.go @@ -23,7 +23,12 @@ const ( ) // NewCmdHistory returns a new target history command -func NewCmdHistory(f util.Factory, o *HistoryOptions) *cobra.Command { +func NewCmdHistory(f util.Factory, ioStreams util.IOStreams) *cobra.Command { + o := &HistoryOptions{ + Options: base.Options{ + IOStreams: ioStreams, + }, + } cmd := &cobra.Command{ Use: "history", Short: "Print the target history", @@ -47,15 +52,6 @@ type HistoryWriteOptions struct { path string } -// NewHistoryOptions returns initialized HistoryOptions -func NewHistoryOptions(ioStreams util.IOStreams) *HistoryOptions { - return &HistoryOptions{ - Options: base.Options{ - IOStreams: ioStreams, - }, - } -} - // NewHistoryWriteOptions returns initialized HistoryWriteOptions func NewHistoryWriteOptions(ioStreams util.IOStreams) *HistoryWriteOptions { return &HistoryWriteOptions{ @@ -147,12 +143,12 @@ func (o *HistoryWriteOptions) Run(f util.Factory) error { return fmt.Errorf("failed to get current target: %w", err) } - toCommand, err := toCommand(currentTarget) + command, err := toCommand(currentTarget) if err != nil { return err } - return historyWrite(o.path, toCommand) + return historyWrite(o.path, command) } // Complete adapts from the command line args to the data required. diff --git a/pkg/cmd/target/history_test.go b/pkg/cmd/target/history_test.go index a11cfc41..f60c67af 100644 --- a/pkg/cmd/target/history_test.go +++ b/pkg/cmd/target/history_test.go @@ -73,8 +73,7 @@ var _ = Describe("history Command", func() { Describe("#ToHistoryOutput", func() { It("should print history output from command level", func() { - o := cmdtarget.NewHistoryOptions(streams) - cmd := cmdtarget.NewCmdHistory(factory, o) + cmd := cmdtarget.NewCmdHistory(factory, streams) Expect(cmd.RunE(cmd, nil)).To(Succeed()) Expect(out.String()).Should(ContainSubstring("target --garden mygarden --project myproject --shoot myshoot")) }) @@ -82,9 +81,9 @@ var _ = Describe("history Command", func() { Describe("#toCommand", func() { It("should succeed execute history parse", func() { - string, err := cmdtarget.ToCommand(currentTarget) + currentTarget, err := cmdtarget.ToCommand(currentTarget) Expect(err).NotTo(HaveOccurred()) - Expect(string).Should((ContainSubstring("target --garden mygarden --project myproject --shoot myshoot"))) + Expect(currentTarget).Should((ContainSubstring("target --garden mygarden --project myproject --shoot myshoot"))) }) }) }) diff --git a/pkg/cmd/target/target.go b/pkg/cmd/target/target.go index 56ccb26c..02bded53 100644 --- a/pkg/cmd/target/target.go +++ b/pkg/cmd/target/target.go @@ -51,7 +51,7 @@ gardenctl target value/that/matches/pattern --control-plane`, cmd.AddCommand(NewCmdUnset(f, ioStreams)) cmd.AddCommand(NewCmdView(f, ioStreams)) - cmd.AddCommand(NewCmdHistory(f, o.Options)) + cmd.AddCommand(NewCmdHistory(f, ioStreams)) o.AddFlags(cmd.Flags()) manager, err := f.Manager()