Skip to content
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
14 changes: 10 additions & 4 deletions app/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"sort"

"github.com/iskandervdh/spinup/common"
"github.com/iskandervdh/spinup/core"
Expand All @@ -24,6 +25,11 @@ func (a *App) GetCommands() []core.Command {
return nil
}

// Sort commands by name
sort.Slice(commands, func(i, j int) bool {
return commands[i].Name < commands[j].Name
})

return commands
}

Expand All @@ -44,14 +50,14 @@ func (a *App) AddCommand(name string, command string) error {
return nil
}

func (a *App) UpdateCommand(name string, command string) error {
func (a *App) UpdateCommand(id int64, name string, command string) error {
err := a.core.FetchCommands()

if err != nil {
return fmt.Errorf("error getting commands config: %s", err)
}

msg := a.core.UpdateCommand(name, command)
msg := a.core.UpdateCommandById(id, name, command)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
Expand All @@ -61,14 +67,14 @@ func (a *App) UpdateCommand(name string, command string) error {
return nil
}

func (a *App) RemoveCommand(name string) error {
func (a *App) RemoveCommand(id int64) error {
err := a.core.FetchCommands()

if err != nil {
return fmt.Errorf("error getting commands config: %s", err)
}

msg := a.core.RemoveCommand(name)
msg := a.core.RemoveCommandById(id)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
Expand Down
64 changes: 58 additions & 6 deletions app/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"
"os"
"sort"

"github.com/iskandervdh/spinup/common"
"github.com/iskandervdh/spinup/core"
Expand Down Expand Up @@ -30,10 +31,15 @@ func (a *App) GetProjects() []core.Project {
return nil
}

// Sort projects by name
sort.Slice(projects, func(i, j int) bool {
return projects[i].Name < projects[j].Name
})

return projects
}

func (a *App) AddProject(name string, port int64, commandNames []string) error {
func (a *App) AddProject(name string, port int64, commandNames []string, projectDir string) error {
err := a.core.FetchCommands()

if err != nil {
Expand All @@ -53,10 +59,19 @@ func (a *App) AddProject(name string, port int64, commandNames []string) error {
return fmt.Errorf("%s", msg.GetText())
}

if projectDir != "" {
msg = a.core.SetProjectDir(name, &projectDir)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
return fmt.Errorf("%s", msg.GetText())
}
}

return nil
}

func (a *App) UpdateProject(name string, port int64, commandNames []string) error {
func (a *App) UpdateProject(id int64, name string, port int64, commandNames []string, projectDir string) error {
err := a.core.FetchCommands()

if err != nil {
Expand All @@ -69,17 +84,26 @@ func (a *App) UpdateProject(name string, port int64, commandNames []string) erro
return fmt.Errorf("error getting projects config: %s", err)
}

msg := a.core.UpdateProject(name, port, commandNames)
msg := a.core.UpdateProjectByID(id, name, port, commandNames)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
return fmt.Errorf("%s", msg.GetText())
}

if projectDir != "" {
msg = a.core.SetProjectDir(name, &projectDir)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
return fmt.Errorf("%s", msg.GetText())
}
}

return nil
}

func (a *App) RemoveProject(name string) error {
func (a *App) RemoveProject(id int64) error {
err := a.core.FetchCommands()

if err != nil {
Expand All @@ -92,7 +116,7 @@ func (a *App) RemoveProject(name string) error {
return fmt.Errorf("error getting projects config: %s", err)
}

msg := a.core.RemoveProject(name)
msg := a.core.RemoveProjectById(id)

if _, ok := msg.(*common.ErrMsg); ok {
fmt.Println(msg.GetText())
Expand All @@ -102,7 +126,7 @@ func (a *App) RemoveProject(name string) error {
return nil
}

func (a *App) SelectProjectDirectory(projectName string, defaultDir string) error {
func (a *App) UpdateProjectDirectory(projectName string, defaultDir string) error {
if defaultDir == "" {
d, err := os.UserHomeDir()

Expand Down Expand Up @@ -136,3 +160,31 @@ func (a *App) SelectProjectDirectory(projectName string, defaultDir string) erro

return nil
}

func (a *App) SelectProjectDirectory(projectName string, defaultDir string) (string, error) {
if defaultDir == "" {
d, err := os.UserHomeDir()

if err != nil {
defaultDir = ""
} else {
defaultDir = d
}
}

dir, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a directory for project " + projectName,
DefaultDirectory: defaultDir,
})

if err != nil {
fmt.Println("Error selecting directory:", err)
return "", err
}

if dir == "" {
return "", fmt.Errorf("no directory selected")
}

return dir, nil
}
28 changes: 28 additions & 0 deletions core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ func (c *Core) RemoveCommand(name string) common.Msg {
return common.NewSuccessMsg("Removed command '%s'", name)
}

func (c *Core) RemoveCommandById(id int64) common.Msg {
if c.commands == nil {
return common.NewErrMsg("No commands found")
}

err := c.dbQueries.DeleteCommandById(c.dbContext, id)

if err != nil {
return common.NewErrMsg("Error deleting command: %s", err)
}

return common.NewSuccessMsg("Removed command")
}

// Update the command with the given name to the given command string.
func (c *Core) UpdateCommand(name string, command string) common.Msg {
if c.commands == nil {
Expand All @@ -109,6 +123,20 @@ func (c *Core) UpdateCommand(name string, command string) common.Msg {
return common.NewSuccessMsg("Updated command '%s': %s", name, command)
}

func (c *Core) UpdateCommandById(id int64, name string, command string) common.Msg {
err := c.dbQueries.UpdateCommandById(c.dbContext, sqlc.UpdateCommandByIdParams{
ID: id,
Name: name,
Command: command,
})

if err != nil {
return common.NewErrMsg("Error updating command: %s", err)
}

return common.NewSuccessMsg("Updated command '%s': %s", name, command)
}

// Rename the command with the given old name to the given new name.
func (c *Core) RenameCommand(oldName string, newName string) common.Msg {
if c.commands == nil {
Expand Down
Loading