Skip to content

Commit

Permalink
feat: added input and select type interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed May 19, 2023
1 parent 376e182 commit 99becd7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /go/src/app
COPY . .

# Execute o comando make build quando o container for iniciado
CMD ["make", "build"]
CMD ["make", "build", "lint"]

# Defina a variável de ambiente PATH com o diretório bin do Go
ENV PATH="/go/bin:${PATH}"
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/fatih/color v1.13.0
github.com/go-faker/faker/v4 v4.1.0
github.com/go-git/go-git/v5 v5.4.2
github.com/manifoldco/promptui v0.9.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.14.0
github.com/tidwall/gjson v1.14.3
Expand All @@ -30,6 +31,7 @@ require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
Expand Down Expand Up @@ -459,6 +462,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down Expand Up @@ -763,6 +768,7 @@ golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
19 changes: 19 additions & 0 deletions pkg/cli_interactive/choose/choose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package choose

import (
"github.com/manifoldco/promptui"
)

func Choose() (string, error) {
prompt := promptui.Select{
Label: "Select Type",
Items: []string{"nextjs", "static", "cdn"},
}

_, result, err := prompt.Run()
if err != nil {
return "", err
}

return result, nil
}
18 changes: 18 additions & 0 deletions pkg/cli_interactive/insert/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package insert

import (
"github.com/manifoldco/promptui"
)

func Insert() (string, error) {
prompt := promptui.Prompt{
Label: "Project name",
}

result, err := prompt.Run()
if err != nil {
return "", err
}

return result, err
}
85 changes: 59 additions & 26 deletions pkg/cmd/edge_applications/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package init
import (
"encoding/json"
"fmt"
"github.com/aziontech/azion-cli/pkg/cli_interactive/choose"
"github.com/aziontech/azion-cli/pkg/cli_interactive/insert"
"io/fs"
"os"
"os/exec"
Expand Down Expand Up @@ -124,22 +126,15 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio
}
info.PathWorkingDir = path

if info.TypeLang == "cdn" {
if !hasThisFlag(c, "name") {
dir := filepath.Dir(info.PathWorkingDir)
parent := filepath.Base(dir)
info.Name = parent
fmt.Fprintf(cmd.Io.Out, "%s\n", msg.EdgeApplicationsInitNameNotSentCdn)
}
return initCdn(cmd, path, info)
projectName, projectSettings, err := DetectedProjectJS(info, cmd, path)
if err != nil {
return err
}

if info.TypeLang == "static" {
if !hasThisFlag(c, "name") {
info.Name = filepath.Base(path)
fmt.Fprintf(cmd.Io.Out, "%s\n", msg.EdgeApplicationsInitNameNotSentStatic)
}

switch info.TypeLang {
case "cdn":
return initCdn(cmd, path, info)
case "static":
return initStatic(cmd, info)
}

Expand All @@ -148,11 +143,6 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio
return err
}

projectName, projectSettings, err := DetectedProjectJS(bytePackageJson)
if err != nil {
return err
}

fmt.Fprintf(cmd.Io.Out, msg.EdgeApplicationsAutoDetectec, projectSettings) // nolint:all

if !hasThisFlag(c, "type") {
Expand Down Expand Up @@ -215,18 +205,61 @@ func ReadPackageJson(cmd *InitCmd, path string) ([]byte, string, error) {
return bytePackageJson, pathPackageJson, nil
}

func DetectedProjectJS(bytePackageJson []byte) (projectName string, projectSettings string, err error) {
func DetectedProjectJS(info *InitInfo, cmd *InitCmd, path string) (projectName string, projectSettings string, err error) {
var packageJson PackageJson
err = json.Unmarshal(bytePackageJson, &packageJson)
path = path + "/package.json"
_, err = cmd.Stat(path)
if err != nil {
return "", "", utils.ErrorUnmarshalReader
if len(info.Name) > 0 {
projectName = info.Name
} else {

projectName, err = insert.Insert()
if err != nil {
return "", "", err
}
info.Name = projectName

projectSettings, err = choose.Choose()
if err != nil {
return "", "", err
}
info.TypeLang = projectSettings
return projectName, projectSettings, nil
}
} else {
pathPackageJson := path
bytePackageJson, errReadFile := cmd.FileReader(pathPackageJson)
if errReadFile != nil {
return "", "", msg.ErrorPackageJsonNotFound
}

if len(info.Name) == 0 {
projectName = packageJson.Name
info.Name = projectName
}

err = json.Unmarshal(bytePackageJson, &packageJson)
if err != nil {
return "", "", utils.ErrorUnmarshalReader
}
}

if len(packageJson.Dependencies.Next) > 0 {
projectName = packageJson.Name
switch {
case len(packageJson.Dependencies.Next) > 0:
projectSettings = "nextjs"
} else {
return "", "", utils.ErrorUnsupportedType
case info.TypeLang == "nextjs":
projectSettings = info.TypeLang
case info.TypeLang == "static":
projectSettings = info.TypeLang
case info.TypeLang == "cdn":
projectSettings = info.TypeLang
default:
projectSettings, err = choose.Choose()
if err != nil {
return "", "", err
}
info.TypeLang = projectSettings
}

return projectName, projectSettings, nil
Expand Down
64 changes: 2 additions & 62 deletions pkg/cmd/edge_applications/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,8 @@ func TestCobraCmd(t *testing.T) {

cmd := NewCobraCmd(initCmd)

cmd.SetArgs([]string{"--name", "SUUPA_DOOPA", "--type", "javascript"})

err := cmd.Execute()

require.ErrorIs(t, err, msg.ErrorPackageJsonNotFound)
})

t.Run("with unsupported type", func(t *testing.T) {
f, _, _ := testutils.NewFactory(nil)

initCmd := NewInitCmd(f)

initCmd.FileReader = func(path string) ([]byte, error) {
return []byte("{\n \"name\": \"vanillajs-app\",\n \"version\": \"1.0.0\",\n \"main\": \"index.js\",\n \"scripts\": {\n \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\",\n \"build\": \"azioncli edge_applications build\",\n \"deploy\": \"azioncli edge_applications publish\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/aziontech/azioncli-template.git\"\n },\n \"author\": \"\",\n \"license\": \"ISC\",\n \"bugs\": {\n \"url\": \"https://github.com/aziontech/azioncli-template/issues\"\n },\n \"homepage\": \"https://github.com/aziontech/azioncli-template#readme\",\n \"description\": \"\",\n \"devDependencies\": {\n \"clean-webpack-plugin\": \"^4.0.0\",\n \"webpack-cli\": \"^4.9.2\"\n }\n}\n"), nil
}

cmd := NewCobraCmd(initCmd)

cmd.SetArgs([]string{"--name", "BLEBLEBLE", "--type", "demeuamor"})

err := cmd.Execute()

require.ErrorIs(t, err, utils.ErrorUnsupportedType)
cmd.SetArgs([]string{"--name", "SUUPA_DOOPA", "--type", "static", "-y"})
require.NoError(t, cmd.Execute())
})

t.Run("with -y and -n flags", func(t *testing.T) {
Expand All @@ -72,45 +51,6 @@ func TestCobraCmd(t *testing.T) {
require.ErrorIs(t, err, msg.ErrorYesAndNoOptions)
})

t.Run("did not send name", func(t *testing.T) {
mock := &httpmock.Registry{}
f, stdout, _ := testutils.NewFactory(mock)
initCmd := NewInitCmd(f)

initCmd.LookPath = func(bin string) (string, error) {
return "", nil
}

initCmd.CommandRunner = func(cmd string, envs []string) (string, int, error) {
return "", 0, nil
}
initCmd.FileReader = func(path string) ([]byte, error) {
return []byte(`{"init": {"cmd": "ls", "output-ctrl": "on-error"}, "type":"static" }`), nil
}
initCmd.WriteFile = func(filename string, data []byte, perm fs.FileMode) error {
return nil
}
initCmd.Rename = func(oldpath string, newpath string) error {
return nil
}
initCmd.Mkdir = func(path string, perm os.FileMode) error {
return nil
}
initCmd.GitPlainClone = func(path string, isBare bool, o *git.CloneOptions) (*git.Repository, error) {
return &git.Repository{}, nil
}

cmd := NewCobraCmd(initCmd)

cmd.SetArgs([]string{"--type", "static"})

err := cmd.Execute()

require.NoError(t, err)

require.Contains(t, stdout.String(), fmt.Sprintf(msg.EdgeApplicationsInitSuccessful+"\n", "init"))
})

t.Run("success with static", func(t *testing.T) {
mock := &httpmock.Registry{}
f, stdout, _ := testutils.NewFactory(mock)
Expand Down

0 comments on commit 99becd7

Please sign in to comment.