Skip to content

Commit

Permalink
cmd/internal: impl current and next
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Jun 13, 2024
1 parent db9022d commit 95cb7fe
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
30 changes: 30 additions & 0 deletions cmd/internal/current.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package internal

import (
"fmt"

"github.com/getsavvyinc/savvy-cli/display"
"github.com/getsavvyinc/savvy-cli/server/run"
"github.com/spf13/cobra"
)

// currentCmd represents the current command
var currentCmd = &cobra.Command{
Use: "current",
Short: "Get the command to run",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
cl, err := run.NewDefaultClient(ctx)
if err != nil {
display.ErrorWithSupportCTA(err)
return
}

curr := cl.CurrentCommand()
fmt.Printf("%s", curr)
},
}

func init() {
InternalCmd.AddCommand(currentCmd)
}
45 changes: 45 additions & 0 deletions cmd/internal/next.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package internal

import (
"fmt"

"github.com/getsavvyinc/savvy-cli/display"
"github.com/getsavvyinc/savvy-cli/server/run"
"github.com/spf13/cobra"
)

// nextCmd represents the next command
var nextCmd = &cobra.Command{
Use: "next",
Hidden: true,
Short: "Update runbook state to next step",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
cl, err := run.NewDefaultClient(ctx)
if err != nil {
display.ErrorWithSupportCTA(err)
return
}

curr := cl.CurrentCommand()
if curr != executedCommand {
return
}

idx, err := cl.NextCommand()
if err != nil {
display.ErrorWithSupportCTA(err)
return
}

fmt.Printf("%d", idx)
},
}

var executedCommand string

func init() {
InternalCmd.AddCommand(nextCmd)

nextCmd.Flags().StringVarP(&executedCommand, "cmd", "c", "", "previously executed command")
}
2 changes: 1 addition & 1 deletion cmd/setup/savvy.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function __savvy_runbook_runner__() {

if [[ "${SAVVY_CONTEXT}" == "run" && "${SAVVY_NEXT_STEP}" -le "${#SAVVY_COMMANDS}" ]] ; then
#next_step_idx=${SAVVY_NEXT_STEP}
cmd=$(savvy internal fetch)
cmd=$(savvy internal current)
BUFFER="${cmd}"
zle end-of-line # Accept the line for editing
fi
Expand Down
23 changes: 16 additions & 7 deletions server/run/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

type Client interface {
server.ShutdownSender
NextCommand() error
NextCommand() (int, error)
PreviousCommand() error
FetchCommand() string
CurrentCommand() string
}

func NewDefaultClient(ctx context.Context) (Client, error) {
Expand Down Expand Up @@ -45,18 +45,27 @@ func (c *client) SendShutdown() error {
return json.NewEncoder(conn).Encode(data)
}

func (c *client) NextCommand() error {
func (c *client) NextCommand() (int, error) {
conn, err := net.Dial("unix", c.socketPath)
if err != nil {
return err
return 0, err
}
defer conn.Close()

data := RunCommand{
Command: nextCommand,
}

return json.NewEncoder(conn).Encode(data)
if err := json.NewEncoder(conn).Encode(data); err != nil {
return 0, err
}

var response RunCommandIndexResponse
if err := json.NewDecoder(conn).Decode(&response); err != nil {
return 0, err
}

return response.Index, nil
}

func (c *client) PreviousCommand() error {
Expand All @@ -72,14 +81,14 @@ func (c *client) PreviousCommand() error {
return json.NewEncoder(conn).Encode(data)
}

func (c *client) FetchCommand() string {
func (c *client) CurrentCommand() string {
conn, err := net.Dial("unix", c.socketPath)
if err != nil {
return err.Error()
}

data := RunCommand{
Command: fetchCommand,
Command: currentCommand,
}

if err := json.NewEncoder(conn).Encode(data); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions server/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func WithLogger(logger *slog.Logger) Option {
// cleanupSocket is an internal function.
// It is the callers responsibility to ensure the socketPath exists.
func cleanupSocket(socketPath string) error {
cl, err := server.NewClient(context.Background(), socketPath)
cl, err := NewClient(context.Background(), socketPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,16 +140,23 @@ func (rs *RunServer) handleConnection(c net.Conn) {
rs.handleCommand(cmd, c)
}

type RunCommandIndexResponse struct {
Index int `json:"index"`
}

func (rs *RunServer) handleCommand(cmd string, c net.Conn) {
switch cmd {
case shutdownCommand:
rs.Close()
case nextCommand:
var resp RunCommandIndexResponse
rs.mu.Lock()
rs.currIndex++
if rs.currIndex >= len(rs.commands) {
rs.currIndex = len(rs.commands) - 1
}
resp.Index = rs.currIndex
json.NewEncoder(c).Encode(resp)
rs.mu.Unlock()
case previousCommand:
rs.mu.Lock()
Expand All @@ -158,7 +165,7 @@ func (rs *RunServer) handleCommand(cmd string, c net.Conn) {
rs.currIndex = 0
}
rs.mu.Unlock()
case fetchCommand:
case currentCommand:
rs.mu.Lock()
if rs.currIndex >= len(rs.commands) {
rs.currIndex = len(rs.commands) - 1
Expand All @@ -182,8 +189,8 @@ func (rs *RunServer) SocketPath() string {
const (
shutdownCommand = "savvy shutdown"
nextCommand = "savvy internal next"
previousCommand = "savvy internal prev"
fetchCommand = "savvy internal fetch"
previousCommand = "savvy internal previous"
currentCommand = "savvy internal current"
)

func (rc *RunCommand) IsShutdown() bool {
Expand Down

0 comments on commit 95cb7fe

Please sign in to comment.