diff --git a/Dockerfile b/Dockerfile index 35eb23a5..b17dfecc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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}" diff --git a/go.mod b/go.mod index 9c6f654e..7421ef66 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index c6288d0f..e6f4f6cb 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/cli_interactive/choose/choose.go b/pkg/cli_interactive/choose/choose.go new file mode 100644 index 00000000..dd85fddf --- /dev/null +++ b/pkg/cli_interactive/choose/choose.go @@ -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 +} diff --git a/pkg/cli_interactive/insert/insert.go b/pkg/cli_interactive/insert/insert.go new file mode 100644 index 00000000..05d19525 --- /dev/null +++ b/pkg/cli_interactive/insert/insert.go @@ -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 +} diff --git a/pkg/cmd/edge_applications/init/init.go b/pkg/cmd/edge_applications/init/init.go index 50938e9f..82ebdd0a 100644 --- a/pkg/cmd/edge_applications/init/init.go +++ b/pkg/cmd/edge_applications/init/init.go @@ -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" @@ -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) } @@ -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") { @@ -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 diff --git a/pkg/cmd/edge_applications/init/init_test.go b/pkg/cmd/edge_applications/init/init_test.go index 4ffafc5f..4eeaeee4 100644 --- a/pkg/cmd/edge_applications/init/init_test.go +++ b/pkg/cmd/edge_applications/init/init_test.go @@ -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) { @@ -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)