Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --json flag back to list #158

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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