Skip to content

Commit

Permalink
Merge pull request #25 from kiyonlin/master
Browse files Browse the repository at this point in the history
🚨 Change project name
  • Loading branch information
kiyonlin authored Oct 17, 2020
2 parents 402b790 + d948898 commit 4a25503
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fiber new PROJECT [module name] [flags]

```
-h, --help help for new
-r, --repo string complex boilerplate repo name in github (default "gofiber/boilerplate")
-t, --template string basic|complex (default "basic")
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_Dev_Escort_Init(t *testing.T) {
e := getEscort()
at.Nil(e.init())

at.Contains(e.root, "fiber-cli")
at.Contains(e.root, "cli")
at.NotEmpty(e.binPath)
if runtime.GOOS != "windows" {
at.Nil(os.Remove(e.binPath))
Expand Down
114 changes: 114 additions & 0 deletions cmd/internal/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package internal

import (
"fmt"

input "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/containerd/console"
)

type errMsg error

type Prompt struct {
p *tea.Program
textInput input.Model
err error
title string
answer string
}

func NewPrompt(title string, placeholder ...string) *Prompt {
p := &Prompt{
title: title,
textInput: input.NewModel(),
}

if len(placeholder) > 0 {
p.textInput.Placeholder = placeholder[0]
}

p.p = tea.NewProgram(p)

return p
}

func (p *Prompt) YesOrNo() (bool, error) {
answer, err := p.Answer()
if err != nil {
return false, err
}

return parseBool(answer), nil
}

func parseBool(str string) bool {
switch str {
case "1", "t", "T", "true", "TRUE", "True", "y", "Y", "yes", "Yes":
return true
}
return false
}

func (p *Prompt) Answer() (result string, err error) {
if err = checkConsole(); err != nil {
return
}

if err := p.p.Start(); err != nil {
return "", err
}
return p.answer, nil
}

func (p *Prompt) Init() tea.Cmd {
p.textInput.Focus()

return input.Blink(p.textInput)
}

func (p *Prompt) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
fallthrough
case tea.KeyEsc:
fallthrough
case tea.KeyEnter:
p.answer = p.textInput.Value()
return p, tea.Quit
}

// We handle errors just like any other message
case errMsg:
p.err = msg
return p, nil
}

p.textInput, cmd = input.Update(msg, p.textInput)
return p, cmd
}

func (p *Prompt) View() string {
return fmt.Sprintf(
"%s\n\n%s\n\n%s\n\n",
p.title,
input.View(p.textInput),
"(esc to quit)",
)
}

func checkConsole() (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
}()

console.Current()

return
}
92 changes: 92 additions & 0 deletions cmd/internal/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package internal

import (
"errors"
"testing"

input "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/stretchr/testify/assert"
)

func Test_Prompt_New(t *testing.T) {
t.Parallel()

at := assert.New(t)

p := NewPrompt("title", "placeholder")

at.NotNil(p)
at.Equal("title", p.title)
at.Equal("placeholder", p.textInput.Placeholder)
at.NotNil(p.p)
}

func Test_Prompt_Answer(t *testing.T) {
t.Parallel()

p := NewPrompt("")
_, err := p.Answer()
assert.NotNil(t, err)
}

func Test_Prompt_YesOrNo(t *testing.T) {
t.Parallel()

p := NewPrompt("")
_, err := p.YesOrNo()
assert.NotNil(t, err)
}

func Test_Prompt_ParseBool(t *testing.T) {
t.Parallel()

assert.True(t, parseBool("y"))
assert.False(t, parseBool(""))
}

func Test_Prompt_Initialize(t *testing.T) {
t.Parallel()

at := assert.New(t)

p := NewPrompt("")
cmd := p.Init()

switch cmd().(type) {
case input.BlinkMsg:
default:
at.Fail("msg should be input.BlankMsg")
}
}

func Test_Prompt_Update(t *testing.T) {
t.Parallel()

at := assert.New(t)

p := NewPrompt("")

var cmd tea.Cmd
_, cmd = p.Update(nil)
at.Nil(cmd)

_, cmd = p.Update(errMsg(errors.New("fake error")))
at.NotNil(p.err)
at.Nil(cmd)

_, cmd = p.Update(tea.KeyMsg{Type: tea.KeyRune, Rune: 'a'})
at.Nil(cmd)

_, cmd = p.Update(tea.KeyMsg{Type: tea.KeyCtrlC})
at.NotNil(cmd)
}

func Test_Prompt_View(t *testing.T) {
t.Parallel()

at := assert.New(t)
p := NewPrompt("")

at.Contains(p.View(), "esc")
}
25 changes: 19 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"time"

"github.com/gofiber/cli/cmd/internal"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)

const version = "0.0.3"
const version = "0.0.4"
const configName = ".fiberconfig"

var (
Expand Down Expand Up @@ -73,8 +74,20 @@ func checkCliVersion(cmd *cobra.Command) {
}

if version != cliLatestVersion {
cmd.Println(termenv.String(fmt.Sprintf(versionWarningFormat, version, cliLatestVersion)).
Foreground(termenv.ANSIBrightYellow))
title := termenv.String(fmt.Sprintf(versionUpgradeTitleFormat, version, cliLatestVersion)).
Foreground(termenv.ANSIBrightYellow)

prompt := internal.NewPrompt(title.String())
ok, err := prompt.YesOrNo()

if err == nil && ok {
upgrade(cmd, cliLatestVersion)
}

if err != nil {
warning := fmt.Sprintf("WARNING: Failed to upgrade fiber cli: %s", err)
cmd.Println(termenv.String(warning).Foreground(termenv.ANSIBrightYellow))
}
}

updateVersionCheckedAt()
Expand All @@ -95,7 +108,7 @@ Learn more on https://gofiber.io
CLI version ` + version

versionWarningFormat = `
WARNING: You are using fiber-cli version %s; however, version %s is available.
You should consider upgrading via the 'fiber upgrade' command.`
versionUpgradeTitleFormat = `
You are using fiber cli version %s; however, version %s is available.
Would you like to upgrade now? (y/N)`
)
4 changes: 2 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func setupRootCmd(t *testing.T) (*assert.Assertions, *bytes.Buffer) {
return at, b
}

var latestCliVersionUrl = "https://api.github.com/repos/gofiber/fiber-cli/releases/latest"
var latestCliVersionUrl = "https://api.github.com/repos/gofiber/cli/releases/latest"

var fakeCliVersionResponse = func(version ...string) []byte {
v := "99.99.99"
if len(version) > 0 {
v = version[0]
}
return []byte(fmt.Sprintf(`{ "assets": [], "assets_url": "https://api.github.com/repos/gofiber/fiber-cli/releases/32630724/assets", "author": { "avatar_url": "https://avatars1.githubusercontent.com/u/1214670?v=4", "events_url": "https://api.github.com/users/kiyonlin/events{/privacy}", "followers_url": "https://api.github.com/users/kiyonlin/followers", "following_url": "https://api.github.com/users/kiyonlin/following{/other_user}", "gists_url": "https://api.github.com/users/kiyonlin/gists{/gist_id}", "gravatar_id": "", "html_url": "https://github.com/kiyonlin", "id": 1214670, "login": "kiyonlin", "node_id": "MDQ6VXNlcjEyMTQ2NzA=", "organizations_url": "https://api.github.com/users/kiyonlin/orgs", "received_events_url": "https://api.github.com/users/kiyonlin/received_events", "repos_url": "https://api.github.com/users/kiyonlin/repos", "site_admin": false, "starred_url": "https://api.github.com/users/kiyonlin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/kiyonlin/subscriptions", "type": "User", "url": "https://api.github.com/users/kiyonlin" }, "created_at": "2020-10-15T15:58:55Z", "draft": false, "html_url": "https://github.com/gofiber/fiber-cli/releases/tag/v99.99.99", "id": 32630724, "name": "v%s", "node_id": "MDc6UmVsZWFzZTMyNjMwNzI0", "prerelease": false, "published_at": "2020-10-15T16:09:05Z", "tag_name": "v99.99.99", "tarball_url": "https://api.github.com/repos/gofiber/fiber-cli/tarball/v99.99.99", "target_commitish": "master", "upload_url": "https://uploads.github.com/repos/gofiber/fiber-cli/releases/32630724/assets{?name,label}", "url": "https://api.github.com/repos/gofiber/fiber-cli/releases/32630724", "zipball_url": "https://api.github.com/repos/gofiber/fiber-cli/zipball/v99.99.99"}`, v))
return []byte(fmt.Sprintf(`{ "assets": [], "assets_url": "https://api.github.com/repos/gofiber/cli/releases/32630724/assets", "author": { "avatar_url": "https://avatars1.githubusercontent.com/u/1214670?v=4", "events_url": "https://api.github.com/users/kiyonlin/events{/privacy}", "followers_url": "https://api.github.com/users/kiyonlin/followers", "following_url": "https://api.github.com/users/kiyonlin/following{/other_user}", "gists_url": "https://api.github.com/users/kiyonlin/gists{/gist_id}", "gravatar_id": "", "html_url": "https://github.com/kiyonlin", "id": 1214670, "login": "kiyonlin", "node_id": "MDQ6VXNlcjEyMTQ2NzA=", "organizations_url": "https://api.github.com/users/kiyonlin/orgs", "received_events_url": "https://api.github.com/users/kiyonlin/received_events", "repos_url": "https://api.github.com/users/kiyonlin/repos", "site_admin": false, "starred_url": "https://api.github.com/users/kiyonlin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/kiyonlin/subscriptions", "type": "User", "url": "https://api.github.com/users/kiyonlin" }, "created_at": "2020-10-15T15:58:55Z", "draft": false, "html_url": "https://github.com/gofiber/cli/releases/tag/v99.99.99", "id": 32630724, "name": "v%s", "node_id": "MDc6UmVsZWFzZTMyNjMwNzI0", "prerelease": false, "published_at": "2020-10-15T16:09:05Z", "tag_name": "v99.99.99", "tarball_url": "https://api.github.com/repos/gofiber/cli/tarball/v99.99.99", "target_commitish": "master", "upload_url": "https://uploads.github.com/repos/gofiber/cli/releases/32630724/assets{?name,label}", "url": "https://api.github.com/repos/gofiber/cli/releases/32630724", "zipball_url": "https://api.github.com/repos/gofiber/cli/zipball/v99.99.99"}`, v))
}
5 changes: 2 additions & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/muesli/termenv"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -33,14 +32,14 @@ func upgradeRunE(cmd *cobra.Command, _ []string) error {
}

func upgrade(cmd *cobra.Command, cliLatestVersion string) {
upgrader := execCommand("go", "get", "-u", "github.com/gofiber/fiber-cli/fiber")
upgrader := execCommand("go", "get", "-u", "github.com/gofiber/cli/fiber")
upgrader.Env = append(upgrader.Env, os.Environ()...)
upgrader.Env = append(upgrader.Env, "GO111MODULE=off")
if err := runCmd(upgrader); err != nil {
cmd.Printf("fiber: failed to upgrade: %v", err)
return
}

success := fmt.Sprintf("Done! Fiber-cli is now at v%s!", cliLatestVersion)
success := fmt.Sprintf("Done! Fiber cli is now at v%s!", cliLatestVersion)
cmd.Println(termenv.String(success).Foreground(termenv.ANSIBrightGreen))
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func latestVersion(isCli bool) (v string, err error) {
)

if isCli {
res, err = http.Get("https://api.github.com/repos/gofiber/fiber-cli/releases/latest")
res, err = http.Get("https://api.github.com/repos/gofiber/cli/releases/latest")
} else {
res, err = http.Get("https://api.github.com/repos/gofiber/fiber/releases/latest")
}
Expand Down
2 changes: 1 addition & 1 deletion fiber/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/gofiber/fiber-cli/cmd"
import "github.com/gofiber/cli/cmd"

func main() {
cmd.Execute()
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module github.com/gofiber/fiber-cli
module github.com/gofiber/cli

go 1.14

require (
github.com/charmbracelet/bubbles v0.7.0
github.com/charmbracelet/bubbletea v0.12.0
github.com/containerd/console v1.0.1
github.com/fsnotify/fsnotify v1.4.7
github.com/jarcoal/httpmock v1.0.6
github.com/kr/text v0.2.0 // indirect
github.com/muesli/termenv v0.7.4
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.4.0
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
)
Loading

0 comments on commit 4a25503

Please sign in to comment.