-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cli version comparison and improve setup command (#3518)
- Loading branch information
Showing
11 changed files
with
154 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,26 @@ | ||
package ui | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/huh" | ||
) | ||
|
||
type askModel struct { | ||
prompt string | ||
required bool | ||
textInput textinput.Model | ||
err error | ||
} | ||
|
||
func (m askModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m askModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEnter: | ||
if !m.required || (m.required && strings.TrimSpace(m.textInput.Value()) != "") { | ||
return m, tea.Quit | ||
} | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
return m, tea.Quit | ||
} | ||
default: | ||
return m, cmd | ||
} | ||
|
||
m.textInput, cmd = m.textInput.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m askModel) View() string { | ||
return fmt.Sprintf( | ||
"%s\n\n%s\n\n%s", | ||
m.prompt, | ||
m.textInput.View(), | ||
"(esc to quit)", | ||
) + "\n" | ||
} | ||
|
||
func Ask(prompt, placeholder string, required bool) (string, error) { | ||
ti := textinput.New() | ||
ti.Placeholder = placeholder | ||
ti.Focus() | ||
ti.CharLimit = 156 | ||
ti.Width = 40 | ||
|
||
p := tea.NewProgram(askModel{ | ||
prompt: prompt, | ||
textInput: ti, | ||
required: required, | ||
err: nil, | ||
}) | ||
|
||
_m, err := p.Run() | ||
var input string | ||
err := huh.NewInput(). | ||
Title(prompt). | ||
Value(&input). | ||
Placeholder(placeholder).Validate(func(s string) error { | ||
if required && strings.TrimSpace(s) == "" { | ||
return errors.New("required") | ||
} | ||
return nil | ||
}).Run() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
m, ok := _m.(askModel) | ||
if !ok { | ||
return "", fmt.Errorf("unexpected model: %T", _m) | ||
} | ||
|
||
text := strings.TrimSpace(m.textInput.Value()) | ||
|
||
return text, nil | ||
return strings.TrimSpace(input), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,19 @@ | ||
package ui | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/huh" | ||
) | ||
|
||
type confirmModel struct { | ||
confirmed bool | ||
prompt string | ||
err error | ||
} | ||
|
||
func (m confirmModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m confirmModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
if msg.Runes != nil { | ||
switch msg.Runes[0] { | ||
case 'y': | ||
m.confirmed = true | ||
return m, tea.Quit | ||
case 'n': | ||
m.confirmed = false | ||
return m, tea.Quit | ||
} | ||
} | ||
|
||
switch msg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
return m, tea.Quit | ||
} | ||
default: | ||
return m, nil | ||
} | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m confirmModel) View() string { | ||
return fmt.Sprintf( | ||
"%s y / n (esc to quit)", | ||
m.prompt, | ||
) + "\n" | ||
} | ||
|
||
func Confirm(prompt string) (bool, error) { | ||
p := tea.NewProgram(confirmModel{ | ||
prompt: prompt, | ||
err: nil, | ||
}) | ||
|
||
_m, err := p.Run() | ||
var confirm bool | ||
err := huh.NewConfirm(). | ||
Title(prompt). | ||
Affirmative("Yes!"). | ||
Negative("No."). | ||
Value(&confirm).Run() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
m, ok := _m.(confirmModel) | ||
if !ok { | ||
return false, fmt.Errorf("unexpected model: %T", _m) | ||
} | ||
|
||
return m.confirmed, nil | ||
return confirm, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package update | ||
|
||
type GithubRelease struct { | ||
TagName string `json:"tag_name"` | ||
Assets []struct { | ||
Name string `json:"name"` | ||
BrowserDownloadURL string `json:"browser_download_url"` | ||
} `json:"assets"` | ||
type VersionData struct { | ||
Latest string `json:"latest"` | ||
Next string `json:"next"` | ||
RC string `json:"rc"` | ||
} | ||
|
||
type NewVersion struct { | ||
Version string | ||
AssetURL string | ||
} | ||
|
||
const githubReleaseURL = "https://api.github.com/repos/woodpecker-ci/woodpecker/releases/latest" | ||
const ( | ||
woodpeckerVersionURL = "https://woodpecker-ci.org/version.json" | ||
githubBinaryURL = "https://github.com/woodpecker-ci/woodpecker/releases/download/v%s/woodpecker-cli_%s_%s.tar.gz" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.