Skip to content

Commit

Permalink
feat: --yes/-y flag, prompt keybindings help,
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinoKader committed Feb 24, 2023
1 parent 68826ab commit 5c5b207
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
9 changes: 5 additions & 4 deletions cmd/portal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func init() {
}

var configCmd = &cobra.Command{
Use: "config",
Short: "View and configure options",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {},
Use: "config",
Short: "View and configure options",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
ValidArgs: []string{configPathCmd.Name(), configViewCmd.Name(), configEditCmd.Name(), configResetCmd.Name()},
Run: func(cmd *cobra.Command, args []string) {},
}

var configPathCmd = &cobra.Command{
Expand Down
18 changes: 15 additions & 3 deletions cmd/portal/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
- somedomain.com
`
receiveCmd.Flags().StringP("relay", "r", "", desc)
// Add subcommand flags (dummy default values as default values are handled through viper).
receiveCmd.Flags().BoolP("yes", "y", false, "Overwrite existing files without [Y/n] prompts")
}

// ------------------------------------------------------ Command ------------------------------------------------------
Expand All @@ -38,16 +38,28 @@ var receiveCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
ValidArgsFunction: passwordCompletion,
PreRunE: func(cmd *cobra.Command, args []string) error {
// Bind flags to viper
// Bind flags to viper.
if err := viper.BindPFlag("relay", cmd.Flags().Lookup("relay")); err != nil {
return fmt.Errorf("binding relay flag: %w", err)
}

// Reverse the --yes/-y flag value as it has an inverse relationship
// with the configuration value 'prompt_overwrite_files'.
overwriteFlag := cmd.Flags().Lookup("yes")
if overwriteFlag.Changed {
shouldOverwrite, _ := strconv.ParseBool(overwriteFlag.Value.String())
_ = overwriteFlag.Value.Set(strconv.FormatBool(!shouldOverwrite))
}

if err := viper.BindPFlag("prompt_overwrite_files", overwriteFlag); err != nil {
return fmt.Errorf("binding yes flag: %w", err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
file.RemoveTemporaryFiles(file.RECEIVE_TEMP_FILE_NAME_PREFIX)
if err := validateRelayInViper(); err != nil {
return err
return fmt.Errorf("%w (%s) is not a valid address", err, viper.GetString("relay"))
}
logFile, err := setupLoggingFromViper("receive")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/portal/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var sendCmd = &cobra.Command{
file.RemoveTemporaryFiles(file.SEND_TEMP_FILE_NAME_PREFIX)

if err := validateRelayInViper(); err != nil {
return err
return fmt.Errorf("%w (%s) is not a valid address", err, viper.GetString("relay"))
}

logFile, err := setupLoggingFromViper("send")
Expand Down
15 changes: 7 additions & 8 deletions cmd/portal/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import (
"github.com/spf13/cobra"
)

// NOTE: The `port` flag is required and not managed through viper.
func init() {
serveCmd.Flags().IntP("port", "p", 0, "port to run the portal relay server on")
_ = serveCmd.MarkFlagRequired("port")
}

// serveCmd is the cobra command for `portal serve`
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve the relay server",
Long: "The serve command serves the relay server locally.",
Args: cobra.NoArgs,
Args: cobra.MatchAll(cobra.ExactArgs(0), cobra.NoArgs),
RunE: func(cmd *cobra.Command, args []string) error {
port, _ := cmd.Flags().GetInt("port")
ver, err := semver.Parse(version)
Expand All @@ -25,10 +31,3 @@ var serveCmd = &cobra.Command{
return nil
},
}

// Add `port` flag.
// NOTE: The `port` flag is required and not managed through viper.
func init() {
serveCmd.Flags().IntP("port", "p", 0, "port to run the portal rendezvous server on")
_ = serveCmd.MarkFlagRequired("port")
}
22 changes: 15 additions & 7 deletions ui/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ const (
)

type KeyMap struct {
Quit key.Binding
CopyPassword key.Binding
FileListUp key.Binding
FileListDown key.Binding
OverwritePromptYes key.Binding
OverwritePromptNo key.Binding
Quit key.Binding
CopyPassword key.Binding
FileListUp key.Binding
FileListDown key.Binding
OverwritePromptYes key.Binding
OverwritePromptNo key.Binding
OverwritePromptConfirm key.Binding
}

func (k KeyMap) ShortHelp() []key.Binding {
Expand All @@ -42,6 +43,7 @@ func (k KeyMap) ShortHelp() []key.Binding {
k.FileListDown,
k.OverwritePromptYes,
k.OverwritePromptNo,
k.OverwritePromptConfirm,
}
}

Expand All @@ -54,6 +56,7 @@ func (k KeyMap) FullHelp() [][]key.Binding {
k.FileListDown,
k.OverwritePromptYes,
k.OverwritePromptNo,
k.OverwritePromptConfirm,
},
}
}
Expand Down Expand Up @@ -86,14 +89,19 @@ var Keys = KeyMap{
),
OverwritePromptYes: key.NewBinding(
key.WithKeys("y", "Y"),
key.WithHelp("(Y/y)", "confirm overwrite"),
key.WithHelp("(Y/y)", "accept overwrite"),
key.WithDisabled(),
),
OverwritePromptNo: key.NewBinding(
key.WithKeys("n", "N"),
key.WithHelp("(N/n)", "deny overwrite"),
key.WithDisabled(),
),
OverwritePromptConfirm: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("(⏎ )", "confirm choice"),
key.WithDisabled(),
),
}

var PadText = strings.Repeat(" ", MARGIN)
Expand Down
34 changes: 19 additions & 15 deletions ui/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, ui.TaskCmd(message, tea.Batch(cmds...))

case overwritePromptRequestMsg:
var cmds []tea.Cmd
m.state = showOverwritePrompt
m.resetSpinner()
m.keys.OverwritePromptYes.SetEnabled(true)
m.keys.OverwritePromptNo.SetEnabled(true)
m.resetSpinner()
cmds = append(cmds, m.spinner.Tick)

prompt := confirmation.New(fmt.Sprintf("Overwrite file '%s'?", msg.fileName), confirmation.Yes)
m.overwritePrompt = *confirmation.NewModel(prompt)
m.overwritePrompt.MaxWidth = m.width
m.overwritePrompt.WrapMode = promptkit.HardWrap
m.overwritePrompt.Template = confirmation.TemplateYN
m.overwritePrompt.ResultTemplate = confirmation.ResultTemplateYN
m.overwritePrompt.KeyMap.Abort = []string{}
m.overwritePrompt.KeyMap.Toggle = []string{}
cmds = append(cmds, m.overwritePrompt.Init())
m.keys.OverwritePromptConfirm.SetEnabled(true)

return m, tea.Batch(cmds...)
return m, tea.Batch(m.spinner.Tick, m.newOverwritePrompt(msg.fileName))

case decompressionDoneMsg:
m.state = showFinished
Expand Down Expand Up @@ -253,10 +242,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "left", "right":
cmds = append(cmds, promptCmd)
case "y", "Y", "n", "N", "enter":
}
switch {
case key.Matches(msg, m.keys.OverwritePromptYes, m.keys.OverwritePromptNo, m.keys.OverwritePromptConfirm):
m.state = showDecompressing
m.keys.OverwritePromptYes.SetEnabled(false)
m.keys.OverwritePromptNo.SetEnabled(false)
m.keys.OverwritePromptConfirm.SetEnabled(false)
shouldOverwrite, _ := m.overwritePrompt.Value()
m.overwritePromptResponses <- overwritePromptResponseMsg{shouldOverwrite}
cmds = append(cmds, m.listenOverwritePromptRequestsCmd())
Expand Down Expand Up @@ -430,6 +422,18 @@ func (m *model) decompressCmd(temp *os.File) tea.Cmd {

// -------------------- HELPER METHODS -------------------------

func (m *model) newOverwritePrompt(fileName string) tea.Cmd {
prompt := confirmation.New(fmt.Sprintf("Overwrite file '%s'?", fileName), confirmation.Yes)
m.overwritePrompt = *confirmation.NewModel(prompt)
m.overwritePrompt.MaxWidth = m.width
m.overwritePrompt.WrapMode = promptkit.HardWrap
m.overwritePrompt.Template = confirmation.TemplateYN
m.overwritePrompt.ResultTemplate = confirmation.ResultTemplateYN
m.overwritePrompt.KeyMap.Abort = []string{}
m.overwritePrompt.KeyMap.Toggle = []string{}
return m.overwritePrompt.Init()
}

func (m *model) resetSpinner() {
m.spinner = spinner.New()
m.spinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color(ui.ELEMENT_COLOR))
Expand Down

0 comments on commit 5c5b207

Please sign in to comment.