Skip to content

Commit

Permalink
feat: Display notifications for failed and succesful downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
eslam-allam committed Apr 10, 2024
1 parent 21bde07 commit 48a797f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/spring-initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var logger *log.Logger = log.Default()

func main() {
tmpDir := os.TempDir()
f, err := tea.LogToFile(path.Join(tmpDir, constants.LogFileName), "Main loop")
f, err := tea.LogToFile(path.Join(tmpDir, constants.LogFileName), "Main loop")
if err != nil {
fmt.Printf("Failed to start logger: %v", err)
os.Exit(1)
Expand Down
17 changes: 13 additions & 4 deletions models/buttons/buttons.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package buttons

import (
"log"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -9,6 +11,8 @@ import (
"github.com/eslam-allam/spring-initializer-go/models/notification"
)

var logger *log.Logger = log.Default()

type Action int

const (
Expand All @@ -25,6 +29,11 @@ const (
ACTION_RESET
)

type ActionStateMessage struct {
Message string
State ActionState
}

var (
downloadCmd tea.Cmd = func() tea.Msg {
return DOWNLOAD
Expand Down Expand Up @@ -105,20 +114,20 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {

case ActionState:
case ActionStateMessage:
m.inAction = false
switch msg {
switch msg.State {
case ACTION_SUCCESS:
cmd = func() tea.Msg {
return notification.NotificationMsg{
Message: "Download Successful!",
Message: msg.Message,
Level: notification.INFO,
}
}
case ACTION_FAILED:
cmd = func() tea.Msg {
return notification.NotificationMsg{
Message: "Download Failed!",
Message: msg.Message,
Level: notification.ERROR,
}
}
Expand Down
34 changes: 26 additions & 8 deletions models/mainModel/mainModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (m model) View() string {
h, v := lipgloss.Size(body)
notification := m.notification.View()
hn, vn := lipgloss.Size(notification)
verticalPos := math.Floor(float64(v) * 0.5 - float64(vn) * 0.5)
verticalPos := math.Floor(float64(v)*0.5 - float64(vn)*0.5)
body = overlay.PlaceOverlay(h/2-hn/2, int(verticalPos), notification, body)
}

Expand Down Expand Up @@ -376,7 +376,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.buttons, cmd = m.buttons.Update(msg)
}

case buttons.ActionState:
case buttons.ActionStateMessage:
m.buttons, cmd = m.buttons.Update(msg)

case buttons.Action:
Expand All @@ -386,30 +386,48 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
_, _, err := m.generateProject()
if err != nil {
logger.Printf("%v", err)
return buttons.ACTION_FAILED
return buttons.ActionStateMessage{
State: buttons.ACTION_FAILED,
Message: fmt.Sprintf("Failed to Download file: %s", err),
}
}
return buttons.ActionStateMessage{
State: buttons.ACTION_SUCCESS,
Message: "File Downloaded Successfully!",
}
return buttons.ACTION_SUCCESS
}
case buttons.DOWNLOAD_EXTRACT:
cmd = func() tea.Msg {
fullPath, isZip, err := m.generateProject()
if err != nil {
logger.Printf("%v", err)
return buttons.ACTION_FAILED
return buttons.ActionStateMessage{
State: buttons.ACTION_FAILED,
Message: fmt.Sprintf("Failed to Download file: %s", err),
}
}
if isZip {
err = files.UnzipFile(fullPath, m.targetDirectory)
if err != nil {
logger.Printf("Error unzipping file: %v", err)
return buttons.ACTION_FAILED
return buttons.ActionStateMessage{
State: buttons.ACTION_FAILED,
Message: fmt.Sprintf("Failed to extract project: %s", err),
}
}
err = os.Remove(fullPath)
if err != nil {
logger.Printf("Error deleting zip file: %v", err)
return buttons.ActionStateMessage{
State: buttons.ACTION_SUCCESS,
Message: fmt.Sprintf("Project extracted but could not delete zip: %s", err),
}
}
}

return buttons.ACTION_SUCCESS
return buttons.ActionStateMessage{
State: buttons.ACTION_SUCCESS,
Message: "Project Generated Successfully!",
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions service/springio/springio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
"github.com/eslam-allam/spring-initializer-go/models/metadata"
)


type metaFieldType string

var client http.Client = http.Client{
Timeout: constants.DownloadTimeoutSeconds * time.Second,
Timeout: constants.DownloadTimeoutSeconds * time.Second,
}

const (
Expand Down

0 comments on commit 48a797f

Please sign in to comment.