Skip to content

Commit

Permalink
Refactor s3hub tui status
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Feb 3, 2024
1 parent 187b128 commit 87dfab0
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 256 deletions.
23 changes: 21 additions & 2 deletions ui/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ func ErrorMessage(err error) string {

// Choice represents a choice.
type Choice struct {
// Choice is the currently selected menu item.
Choice int
Max int
Min int
// Max is the maximum choice number.
Max int
// Min is the minimum choice number.
Min int
}

// NewChoice returns a new choice.
Expand Down Expand Up @@ -146,3 +149,19 @@ func NewToggleSets(n int) ToggleSets {
}
return ts
}

// Window represents the window size.
type Window struct {
// Width is the window width.
Width int
// Height is the window height.
Height int
}

// NewWindow returns a new window.
func NewWindow(width, height int) *Window {
return &Window{
Width: width,
Height: height,
}
}
22 changes: 22 additions & 0 deletions ui/s3hub/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ import (
"golang.org/x/sync/semaphore"
)

// createMsg is the message that is sent when the user wants to create the S3 bucket.
type createMsg struct{}

// createS3BucketCmd creates the S3 bucket command.
func (m *s3hubCreateBucketModel) createS3BucketCmd() tea.Cmd {
return tea.Cmd(func() tea.Msg {
if m.app == nil {
return ui.ErrMsg(fmt.Errorf("not initialized s3 application. please restart the application"))
}
input := &usecase.S3BucketCreatorInput{
Bucket: m.bucket,
Region: m.region,
}
m.status = statusBucketCreating

if _, err := m.app.S3BucketCreator.CreateS3Bucket(m.ctx, input); err != nil {
return ui.ErrMsg(err)
}
return createMsg{}
})
}

// fetchS3BucketMsg is the message that is sent when the user wants to fetch the list of the S3 buckets.
type fetchS3BucketMsg struct {
buckets model.BucketSets
Expand Down
50 changes: 12 additions & 38 deletions ui/s3hub/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/nao1215/rainbow/app/di"
"github.com/nao1215/rainbow/app/domain/model"
"github.com/nao1215/rainbow/app/usecase"
"github.com/nao1215/rainbow/ui"
)

Expand All @@ -26,8 +25,8 @@ type s3hubCreateBucketModel struct {
err error
// bucket is the name of the S3 bucket that the user wants to create.
bucket model.Bucket
// state is the state of the create bucket operation.
state s3hubCreateBucketState
// state is the status of the create bucket operation.
status status
// awsConfig is the AWS configuration.
awsConfig *model.AWSConfig
// awsProfile is the AWS profile.
Expand All @@ -42,17 +41,7 @@ type s3hubCreateBucketModel struct {
ctx context.Context
}

// createMsg is the message that is sent when the user wants to create the S3 bucket.
type createMsg struct{}

type s3hubCreateBucketState int

const (
s3hubCreateBucketStateNone s3hubCreateBucketState = 0
s3hubCreateBucketStateCreating s3hubCreateBucketState = 1
s3hubCreateBucketStateCreated s3hubCreateBucketState = 2
)

// newS3hubCreateBucketModel creates a new s3hubCreateBucketModel.
func newS3hubCreateBucketModel() (*s3hubCreateBucketModel, error) {
ti := textinput.New()
ti.Placeholder = fmt.Sprintf("Write the S3 bucket name here (min: %d, max: %d)", model.MinBucketNameLength, model.MaxBucketNameLength)
Expand All @@ -77,10 +66,12 @@ func newS3hubCreateBucketModel() (*s3hubCreateBucketModel, error) {
}, nil
}

// Init initializes the model.
func (m *s3hubCreateBucketModel) Init() tea.Cmd {
return textinput.Blink
}

// Update updates the model.
func (m *s3hubCreateBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.err != nil {
return m, tea.Quit
Expand Down Expand Up @@ -108,7 +99,7 @@ func (m *s3hubCreateBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.region = m.region.Next()
}
case "enter":
if m.state == s3hubCreateBucketStateCreated {
if m.status == statusBucketCreated {
return newRootModel(), nil
}
if m.bucketNameInput.Value() == "" || len(m.bucketNameInput.Value()) < model.MinBucketNameLength {
Expand All @@ -131,18 +122,19 @@ func (m *s3hubCreateBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.err = msg
return m, nil
case createMsg:
m.state = s3hubCreateBucketStateCreated
m.status = statusBucketCreated
return m, nil
}

if m.state != s3hubCreateBucketStateCreated && m.choice == s3hubCreateBucketBucketNameChoice {
if m.status != statusBucketCreated && m.choice == s3hubCreateBucketBucketNameChoice {
var cmd tea.Cmd
m.bucketNameInput, cmd = m.bucketNameInput.Update(msg)
return m, cmd
}
return m, nil
}

// View renders the application's UI.
func (m *s3hubCreateBucketModel) View() string {
if m.err != nil {
message := fmt.Sprintf("[ AWS Profile ] %s\n[ Region ] %s\n[ S3 Name ]%s\n\n%s\n\n%s\n%s\n\n",
Expand All @@ -157,7 +149,7 @@ func (m *s3hubCreateBucketModel) View() string {
return message
}

if m.state == s3hubCreateBucketStateCreated {
if m.status == statusBucketCreated {
return fmt.Sprintf("[ AWS Profile ] %s\n[ Region ] %s\n[ S3 Name ]%s\n\n%s\n\nCreated S3 bucket: %s\n%s\n",
m.awsProfile.String(),
m.region.String(),
Expand All @@ -167,7 +159,7 @@ func (m *s3hubCreateBucketModel) View() string {
ui.Subtle("<enter>: return to the top"))
}

if m.state == s3hubCreateBucketStateCreating {
if m.status == statusBucketCreating {
return fmt.Sprintf("[ AWS Profile ] %s\n[ Region ] %s\n[ %s ]%s\n\n%s\n\n%s\n%s\n\n%s\n",
m.awsProfile.String(),
m.region.String(),
Expand Down Expand Up @@ -207,7 +199,7 @@ func (m *s3hubCreateBucketModel) View() string {

// bucketNameWithColor returns the bucket name with color.
func (m *s3hubCreateBucketModel) bucketNameWithColor() string {
if m.state == s3hubCreateBucketStateCreating || m.state == s3hubCreateBucketStateCreated {
if m.status == statusBucketCreating || m.status == statusBucketCreated {
return m.bucketNameInput.View()
}

Expand All @@ -230,21 +222,3 @@ func (m *s3hubCreateBucketModel) bucketNameLengthString() string {
}
return lengthStr
}

func (m *s3hubCreateBucketModel) createS3BucketCmd() tea.Cmd {
return tea.Cmd(func() tea.Msg {
if m.app == nil {
return ui.ErrMsg(fmt.Errorf("not initialized s3 application. please restart the application"))
}
input := &usecase.S3BucketCreatorInput{
Bucket: m.bucket,
Region: m.region,
}
m.state = s3hubCreateBucketStateCreating

if _, err := m.app.S3BucketCreator.CreateS3Bucket(m.ctx, input); err != nil {
return ui.ErrMsg(err)
}
return createMsg{}
})
}
Loading

0 comments on commit 87dfab0

Please sign in to comment.