Skip to content

Commit

Permalink
feat: add json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski committed Sep 6, 2024
1 parent 9380ad3 commit 296206e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
29 changes: 29 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package json

import (
"encoding/json"
"fmt"

"github.com/joshmedeski/sesh/model"
)

type Json interface {
EncodeSessions(sessions []model.SeshSession) string
}

type RealJson struct{}

func NewJson() Json {
return &RealJson{}
}

func (j *RealJson) EncodeSessions(sessions []model.SeshSession) string {
jsonSessions, err := json.Marshal(sessions)
if err != nil {
fmt.Printf(
"Couldn't list sessions as json: %s\n",
err,
)
}
return string(jsonSessions)
}
81 changes: 81 additions & 0 deletions json/mock_Json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion seshcli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"

"github.com/joshmedeski/sesh/icon"
"github.com/joshmedeski/sesh/json"
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/model"
cli "github.com/urfave/cli/v2"
)

func List(icon icon.Icon, list lister.Lister) *cli.Command {
func List(icon icon.Icon, json json.Json, list lister.Lister) *cli.Command {
return &cli.Command{
Name: "list",
Aliases: []string{"l"},
Expand Down Expand Up @@ -59,6 +61,15 @@ func List(icon icon.Icon, list lister.Lister) *cli.Command {
return fmt.Errorf("couldn't list sessions: %q", err)
}

if cCtx.Bool("json") {
var sessionsArray []model.SeshSession
for _, i := range sessions.OrderedIndex {
sessionsArray = append(sessionsArray, sessions.Directory[i])
}
fmt.Println(json.EncodeSessions(sessionsArray))
return nil
}

for _, i := range sessions.OrderedIndex {
name := sessions.Directory[i].Name
if cCtx.Bool("icons") {
Expand Down
4 changes: 3 additions & 1 deletion seshcli/seshcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/joshmedeski/sesh/git"
"github.com/joshmedeski/sesh/home"
"github.com/joshmedeski/sesh/icon"
"github.com/joshmedeski/sesh/json"
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/oswrap"
Expand All @@ -31,6 +32,7 @@ func App(version string) cli.App {
dir := dir.NewDir(os, path)
shell := shell.NewShell(exec)
home := home.NewHome(os)
json := json.NewJson()

// resource dependencies
git := git.NewGit(shell)
Expand All @@ -56,7 +58,7 @@ func App(version string) cli.App {
Version: version,
Usage: "Smart session manager for the terminal",
Commands: []*cli.Command{
List(icon, lister),
List(icon, json, lister),
Connect(connector, icon),
Clone(),
},
Expand Down

0 comments on commit 296206e

Please sign in to comment.