Skip to content

Commit

Permalink
labctl play start --ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Mar 7, 2024
1 parent 3b02d55 commit e68fcdc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/playground/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *listPrinter) printOne(play *api.Play) {
}

var link string
if play.Active {
if play.Active || play.TutorialName+play.ChallengeName+play.CourseName != "" {
link = play.PageURL
}

Expand Down
43 changes: 39 additions & 4 deletions cmd/playground/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"

"github.com/iximiuz/labctl/cmd/ssh"
"github.com/iximiuz/labctl/internal/api"
"github.com/iximiuz/labctl/internal/labcli"
)

type startOptions struct {
playground string

open bool
open bool

ssh bool
machine string

quiet bool
}

Expand Down Expand Up @@ -42,6 +47,18 @@ func newStartCommand(cli labcli.CLI) *cobra.Command {
false,
`Open the playground page in a browser`,
)
flags.BoolVar(
&opts.ssh,
"ssh",
false,
`SSH into the playground immediately after it's created`,
)
flags.StringVar(
&opts.machine,
"machine",
"",
`SSH into the machine with the given name (requires --ssh flag, default to the first machine)`,
)
flags.BoolVarP(
&opts.quiet,
"quiet",
Expand All @@ -61,10 +78,28 @@ func runStartPlayground(ctx context.Context, cli labcli.CLI, opts *startOptions)
return fmt.Errorf("couldn't create a new playground: %w", err)
}

cli.PrintAux("Opening %s in your browser...\n", play.PageURL)
cli.PrintAux("Playground %q has been created.\n", play.ID)

if opts.open {
cli.PrintAux("Opening %s in your browser...\n", play.PageURL)

if err := open.Run(play.PageURL); err != nil {
cli.PrintAux("Couldn't open the browser. Copy the above URL into a browser manually to access the playground.\n")
}
}

if opts.ssh {
if opts.machine == "" {
opts.machine = play.Machines[0].Name
} else {
if play.GetMachine(opts.machine) == nil {
return fmt.Errorf("machine %q not found in the playground", opts.machine)
}
}

cli.PrintAux("SSH-ing into %s machine...\n", opts.machine)

if err := open.Run(play.PageURL); err != nil {
cli.PrintAux("Couldn't open the browser. Copy the above URL into a browser manually to access the playground.\n")
return ssh.RunSSHSession(ctx, cli, play.ID, opts.machine, nil)
}

cli.PrintOut("%s\n", play.ID)
Expand Down
16 changes: 13 additions & 3 deletions cmd/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,19 @@ func runSSHSession(ctx context.Context, cli labcli.CLI, opts *options) error {
}
}

return RunSSHSession(ctx, cli, opts.playID, opts.machine, opts.command)
}

func RunSSHSession(
ctx context.Context,
cli labcli.CLI,
playID string,
machine string,
command []string,
) error {
tunnel, err := portforward.StartTunnel(ctx, cli.Client(), portforward.TunnelOptions{
PlayID: opts.playID,
Machine: opts.machine,
PlayID: playID,
Machine: machine,
PlaysDir: cli.Config().PlaysDir,
SSHDir: cli.Config().SSHDir,
})
Expand Down Expand Up @@ -133,7 +143,7 @@ func runSSHSession(ctx context.Context, cli labcli.CLI, opts *options) error {
return fmt.Errorf("couldn't create SSH session: %w", err)
}

if err := sess.Run(ctx, cli, strings.Join(opts.command, " ")); err != nil {
if err := sess.Run(ctx, cli, strings.Join(command, " ")); err != nil {
return fmt.Errorf("SSH session error: %w", err)
}

Expand Down
39 changes: 34 additions & 5 deletions internal/api/plays.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,40 @@ type Play struct {

Playground Playground `json:"playground" yaml:"playground"`

PageURL string `json:"pageUrl" yaml:"pageUrl"`
Active bool `json:"active" yaml:"active"`
Running bool `json:"running" yaml:"running"`
Destroyed bool `json:"destroyed" yaml:"destroyed"`
Failed bool `json:"failed" yaml:"failed"`
TutorialName string `json:"tutorialName,omitempty" yaml:"tutorialName"`

Tutorial *struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
} `json:"tutorial,omitempty" yaml:"tutorial"`

ChallengeName string `json:"challengeName,omitempty" yaml:"challengeName"`

Challenge *struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
} `json:"challenge,omitempty" yaml:"challenge"`

CourseName string `json:"courseName,omitempty" yaml:"courseName"`

Course *struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
} `json:"course,omitempty" yaml:"course"`

LessonPath string `json:"lessonPath,omitempty" yaml:"lessonPath"`

Lesson *struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
} `json:"lesson,omitempty" yaml:"lesson"`

PageURL string `json:"pageUrl" yaml:"pageUrl"`

Active bool `json:"active" yaml:"active"`
Running bool `json:"running" yaml:"running"`
Destroyed bool `json:"destroyed" yaml:"destroyed"`
Failed bool `json:"failed" yaml:"failed"`

Machines []Machine `json:"machines" yaml:"machines"`
}
Expand Down

0 comments on commit e68fcdc

Please sign in to comment.