refactor: split project manager

This commit is contained in:
raj 2023-10-22 16:50:53 +05:30
parent 9184093a1f
commit 5d19efea47
3 changed files with 146 additions and 129 deletions

92
project-manager/init.go Normal file
View File

@ -0,0 +1,92 @@
package projectmanager
import (
"fmt"
"os"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
projectconfig "github.com/xrehpicx/pee/config"
"github.com/xrehpicx/pee/ui/filepicker"
"github.com/xrehpicx/pee/utils"
)
var InitCmd = &cobra.Command{
Use: "init",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectName := args[0]
projectExists := projectconfig.ProjectExists(projectName)
if projectExists {
log.Warn("Project already exists", "name", projectName)
return
}
selected, err := filepicker.FilePicker("Select your project dir", "Selected Dir: ")
if selected == "" {
log.Warn("No dir selected, aborting")
return
}
if err != nil {
log.Error(err)
return
}
log.Debug("Selected", "work_dir", selected)
// Define the session configuration
workingDir := selected
windows := []projectconfig.Window{
{
WindowName: "editor",
Layout: "8070,202x58,0,0[202x46,0,0,89,202x11,0,47,92]",
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your editor'"},
},
{
ShellCommand: []string{"echo 'run dev server'"},
},
},
},
{
WindowName: "ssh windows",
ShellCommandBefore: []string{
"ls -lr",
},
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your ssh windows'"},
},
{
ShellCommand: []string{"echo 'command to open your ssh windows'"},
},
},
},
{
WindowName: "git",
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your git client'"},
},
},
},
}
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: false,
ReportTimestamp: false,
})
ppath, err := projectconfig.CreateProject(projectName, workingDir, windows)
if err != nil {
logger.Error(err)
} else {
editorCommand, err := projectconfig.GetEditorCommand(projectName)
if err != nil {
editorCommand = ""
}
utils.EditFile(ppath, editorCommand)
fmt.Println("Created Project, config is at:", ppath)
}
},
}

54
project-manager/ls.go Normal file
View File

@ -0,0 +1,54 @@
package projectmanager
import (
"fmt"
btable "github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
projectconfig "github.com/xrehpicx/pee/config"
"github.com/xrehpicx/pee/ui/table"
"github.com/xrehpicx/pee/utils"
)
var ListProjects = &cobra.Command{
Use: "ls",
Short: "List all projects",
Run: func(cmd *cobra.Command, args []string) {
projects, err := projectconfig.ListProjects()
if err != nil {
fmt.Println(err)
return
}
columns := []btable.Column{
{Title: "Name", Width: 20},
{Title: "Session Name", Width: 20},
{Title: "Working Dir", Width: 50},
{Title: "Last Opened", Width: 20},
}
var rows []btable.Row
for projectName, config := range projects {
row := []string{
projectName,
config.SessionName,
config.WorkingDir,
config.LastOpened.Format("2006-01-02 15:04:05"),
}
rows = append(rows, row)
}
selectedRow, action := table.Table(columns, rows)
if action == "edit" {
// print a vim command to open the config file
editorCommand, err := projectconfig.GetEditorCommand(selectedRow[0])
if err != nil {
editorCommand = ""
}
utils.EditFile(projectconfig.ProjectConfigFilePath(selectedRow[0]), editorCommand)
log.Debug("Opened config file", "file", projectconfig.ProjectConfigFilePath(selectedRow[0]))
}
if action == "open" {
ExecuteProjectEnv(selectedRow[0])
}
},
}

View File

@ -1,62 +1,13 @@
package projectmanager package projectmanager
import ( import (
"fmt"
"os"
projectconfig "github.com/xrehpicx/pee/config" projectconfig "github.com/xrehpicx/pee/config"
"github.com/xrehpicx/pee/controller" "github.com/xrehpicx/pee/controller"
"github.com/xrehpicx/pee/ui/filepicker"
"github.com/xrehpicx/pee/ui/table"
"github.com/xrehpicx/pee/utils"
btable "github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ListProjects = &cobra.Command{
Use: "ls",
Short: "List all projects",
Run: func(cmd *cobra.Command, args []string) {
projects, err := projectconfig.ListProjects()
if err != nil {
fmt.Println(err)
return
}
columns := []btable.Column{
{Title: "Name", Width: 20},
{Title: "Session Name", Width: 20},
{Title: "Working Dir", Width: 50},
{Title: "Last Opened", Width: 20},
}
var rows []btable.Row
for projectName, config := range projects {
row := []string{
projectName,
config.SessionName,
config.WorkingDir,
config.LastOpened.Format("2006-01-02 15:04:05"),
}
rows = append(rows, row)
}
selectedRow, action := table.Table(columns, rows)
if action == "edit" {
// print a vim command to open the config file
editorCommand, err := projectconfig.GetEditorCommand(selectedRow[0])
if err != nil {
editorCommand = ""
}
utils.EditFile(projectconfig.ProjectConfigFilePath(selectedRow[0]), editorCommand)
log.Debug("Opened config file", "file", projectconfig.ProjectConfigFilePath(selectedRow[0]))
}
if action == "open" {
ExecuteProjectEnv(selectedRow[0])
}
},
}
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "pee", Use: "pee",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
@ -79,83 +30,3 @@ func ExecuteProjectEnv(projectName string) {
projectconfig.UpdateLastOpened(projectName) projectconfig.UpdateLastOpened(projectName)
log.Debug("Created tmux session", "name", config.SessionName) log.Debug("Created tmux session", "name", config.SessionName)
} }
var InitCmd = &cobra.Command{
Use: "init",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectName := args[0]
projectExists := projectconfig.ProjectExists(projectName)
if projectExists {
log.Warn("Project already exists", "name", projectName)
return
}
selected, err := filepicker.FilePicker("Select your project dir", "Selected Dir: ")
if selected == "" {
log.Warn("No dir selected, aborting")
return
}
if err != nil {
log.Error(err)
return
}
log.Debug("Selected", "work_dir", selected)
// Define the session configuration
workingDir := selected
windows := []projectconfig.Window{
{
WindowName: "editor",
Layout: "8070,202x58,0,0[202x46,0,0,89,202x11,0,47,92]",
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your editor'"},
},
{
ShellCommand: []string{"echo 'run dev server'"},
},
},
},
{
WindowName: "ssh windows",
ShellCommandBefore: []string{
"ls -lr",
},
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your ssh windows'"},
},
{
ShellCommand: []string{"echo 'command to open your ssh windows'"},
},
},
},
{
WindowName: "git",
Panes: []projectconfig.Pane{
{
ShellCommand: []string{"echo 'command to open your git client'"},
},
},
},
}
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: false,
ReportTimestamp: false,
})
ppath, err := projectconfig.CreateProject(projectName, workingDir, windows)
if err != nil {
logger.Error(err)
} else {
editorCommand, err := projectconfig.GetEditorCommand(projectName)
if err != nil {
editorCommand = ""
}
utils.EditFile(ppath, editorCommand)
fmt.Println("Created Project, config is at:", ppath)
}
},
}