Skip to content

Commit

Permalink
add install flags to workspace create cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Nima Kaviani <[email protected]>
  • Loading branch information
cppforlife authored and nimakaviani committed Jan 18, 2019
1 parent 4214b0d commit e3c5faf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
5 changes: 5 additions & 0 deletions docs/cmd/kwt_workspace_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ kwt workspace create [flags]
--image-command strings Set command (can be set multiple times)
--image-command-arg strings Set command args (can be set multiple times)
-i, --input string Set inputs (format: 'name=local-dir-path' or 'name=local-dir-path:remote-dir-path') (example: knctl=.)
--install-chrome Install Google Chrome
--install-desktop Configure X11 and VNC access
--install-firefox Install Firefox
--install-go1x Install Go 1.x
--install-sublime Install Sublime Text
-n, --namespace string Specified namespace ($KWT_NAMESPACE or default from kubeconfig)
-o, --output string Set outputs (format: 'name=local-dir-path' or 'name=local-dir-path:remote-dir-path') (example: knctl=.)
--port ints Set port (multiple can be specified)
Expand Down
7 changes: 7 additions & 0 deletions pkg/kwt/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CreateOptions struct {

WorkspaceFlags WorkspaceFlags
CreateFlags CreateFlags
InstallFlags InstallFlags
RunFlags RunFlags
DeleteFlags DeleteFlags
}
Expand All @@ -34,6 +35,7 @@ func NewCreateCmd(o *CreateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co
}
o.WorkspaceFlags.SetNonRequired(cmd, flagsFactory)
o.CreateFlags.Set(cmd, flagsFactory)
o.InstallFlags.Set("install", cmd, flagsFactory)
o.RunFlags.Set(cmd, flagsFactory)
o.DeleteFlags.Set("delete", cmd, flagsFactory)
return cmd
Expand Down Expand Up @@ -84,6 +86,11 @@ func (o *CreateOptions) Run() error {
return err
}

err = InstallOperation{workspace, o.InstallFlags, o.ui, restConfig}.Run()
if err != nil {
return err
}

return RunOperation{workspace, o.RunFlags, o.ui, restConfig}.Run()
}

Expand Down
54 changes: 27 additions & 27 deletions pkg/kwt/cmd/workspace/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@ type InstallOptions struct {
ui ui.UI

WorkspaceFlags WorkspaceFlags

Desktop bool
Firefox bool
SublimeText bool
GoogleChrome bool
Go1x bool
}

type installer struct {
Enabled bool
Title string
InstallFunc func(*rest.Config) error
InstallFlags InstallFlags
}

func NewInstallOptions(depsFactory cmdcore.DepsFactory, configFactory cmdcore.ConfigFactory, ui ui.UI) *InstallOptions {
Expand All @@ -42,13 +31,7 @@ func NewInstallCmd(o *InstallOptions, flagsFactory cmdcore.FlagsFactory) *cobra.
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}
o.WorkspaceFlags.Set(cmd, flagsFactory)

cmd.Flags().BoolVar(&o.Desktop, "desktop", false, "Configure X11 and VNC access")
cmd.Flags().BoolVar(&o.Firefox, "firefox", false, "Install Firefox")
cmd.Flags().BoolVar(&o.SublimeText, "sublime", false, "Install Sublime Text")
cmd.Flags().BoolVar(&o.GoogleChrome, "chrome", false, "Install Google Chrome")
cmd.Flags().BoolVar(&o.Go1x, "go1x", false, "Install Go 1.x")

o.InstallFlags.Set("", cmd, flagsFactory)
return cmd
}

Expand Down Expand Up @@ -79,21 +62,38 @@ func (o *InstallOptions) Run() error {
return err
}

desktop := ctlwork.WorkspaceDesktop{workspace}
return InstallOperation{workspace, o.InstallFlags, o.ui, restConfig}.Run()
}

type InstallOperation struct {
Workspace ctlwork.Workspace
InstallFlags InstallFlags
UI ui.UI
RestConfig *rest.Config
}

type installer struct {
Enabled bool
Title string
InstallFunc func(*rest.Config) error
}

func (o InstallOperation) Run() error {
desktop := ctlwork.WorkspaceDesktop{o.Workspace}

installers := []installer{
{Enabled: o.Desktop, Title: "desktop", InstallFunc: desktop.Install},
{Enabled: o.Firefox, Title: "Mozilla Firefox", InstallFunc: desktop.AddFirefox},
{Enabled: o.SublimeText, Title: "Sublime Text 3", InstallFunc: desktop.AddSublimeText},
{Enabled: o.GoogleChrome, Title: "Google Chrome", InstallFunc: desktop.AddChrome},
{Enabled: o.Go1x, Title: "Go 1.x", InstallFunc: desktop.AddGo1x},
{Enabled: o.InstallFlags.Desktop, Title: "desktop", InstallFunc: desktop.Install},
{Enabled: o.InstallFlags.Firefox, Title: "Mozilla Firefox", InstallFunc: desktop.AddFirefox},
{Enabled: o.InstallFlags.SublimeText, Title: "Sublime Text 3", InstallFunc: desktop.AddSublimeText},
{Enabled: o.InstallFlags.GoogleChrome, Title: "Google Chrome", InstallFunc: desktop.AddChrome},
{Enabled: o.InstallFlags.Go1x, Title: "Go 1.x", InstallFunc: desktop.AddGo1x},
}

for _, installer := range installers {
if installer.Enabled {
o.ui.PrintLinef("[%s] Installing %s...", time.Now().Format(time.RFC3339), installer.Title)
o.UI.PrintLinef("[%s] Installing %s...", time.Now().Format(time.RFC3339), installer.Title)

err := installer.InstallFunc(restConfig)
err := installer.InstallFunc(o.RestConfig)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/kwt/cmd/workspace/install_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package workspace

import (
cmdcore "github.com/cppforlife/kwt/pkg/kwt/cmd/core"
"github.com/spf13/cobra"
)

type InstallFlags struct {
Desktop bool
Firefox bool
SublimeText bool
GoogleChrome bool
Go1x bool
}

func (s *InstallFlags) Set(prefix string, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory) {
if len(prefix) > 0 {
prefix += "-"
}

cmd.Flags().BoolVar(&s.Desktop, prefix+"desktop", false, "Configure X11 and VNC access")
cmd.Flags().BoolVar(&s.Firefox, prefix+"firefox", false, "Install Firefox")
cmd.Flags().BoolVar(&s.SublimeText, prefix+"sublime", false, "Install Sublime Text")
cmd.Flags().BoolVar(&s.GoogleChrome, prefix+"chrome", false, "Install Google Chrome")
cmd.Flags().BoolVar(&s.Go1x, prefix+"go1x", false, "Install Go 1.x")
}

0 comments on commit e3c5faf

Please sign in to comment.