Skip to content

Commit

Permalink
Merge pull request aziontech#489 from aziontech/minor-fixes-2
Browse files Browse the repository at this point in the history
feat: azion root command becomes an alias for azion init
  • Loading branch information
PatrickMenoti authored Sep 11, 2023
2 parents a964ddd + 81e42ec commit 8fb6dd9
Show file tree
Hide file tree
Showing 25 changed files with 203 additions and 123 deletions.
2 changes: 1 addition & 1 deletion cmd/gen_docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func run(args []string) error {
return fmt.Errorf("error: --doc-path not set")
}

rootCmd := cmd.NewRootCmd(&cmdutil.Factory{
rootCmd := cmd.NewCmd(&cmdutil.Factory{
IOStreams: iostreams.System(),
})
rootCmd.InitDefaultHelpCmd()
Expand Down
1 change: 1 addition & 0 deletions messages/build/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import "errors"

var (
ErrorBuilding = errors.New("Failed to build your resource. Azion configuration not found. Make sure you are in the root directory of your local repository and have already initialized or linked your resource with the commands 'azion init' or 'azion link'")
ErrorVulcanExecute = errors.New("Error executing Vulcan: %s")
EdgeApplicationsOutputErr = errors.New("This output-ctrl option is not available. Read the readme files found in the repository https://github.com/aziontech/azioncli-template and try again")
ErrFailedToRunBuildCommand = errors.New("Failed to run the build command. Verify if the command is correct and check the output above for more details. Run the 'azion build' command again or contact Azion's support")
Expand Down
1 change: 1 addition & 0 deletions messages/general/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var (
ApiListFlagPage = "Returns a page of the list according to its number"
ApiListFlagPageSize = "Defines how many items should be returned per page"
ApiListFlagFilter = "Filters items by their name"
CliVersion = "Azion CLI %s"
)
1 change: 1 addition & 0 deletions messages/init/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ var (
ErrorFailedCreatingWorkerDirectory = errors.New("Failed to create the worker directory. The worker's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrorFailedCreatingAzionDirectory = errors.New("Failed to create the azion directory. The public's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrorDeps = errors.New("Failed to install project dependencies")
ErrorWorkingDir = errors.New("Failed to change current working directory")
)
5 changes: 3 additions & 2 deletions messages/init/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package init
var (

//init cmd
EdgeApplicationsInitUsage = `init [flags]`
EdgeApplicationsInitUsage = `init [flags]`
EdgeApplicationsInitShortDescription = "Initializes an Edge Application"
EdgeApplicationsInitLongDescription = "Defines primary parameters based on a given name and application preset to start an Edge Application on the Azion’s platform"
EdgeApplicationsInitRunningCmd = "Running init step command:\n\n"
Expand All @@ -14,7 +14,8 @@ var (
EdgeApplicationsInitFlagNo = "Answers all yes/no interactions automatically with no"
WebAppInitContentOverridden = "This project was already configured. Do you want to override the previous configuration? <yes | no> (default: no) "
WebAppInitCmdSuccess = "Template successfully fetched and configured\n\n"
InitGettingTemplates = "Getting modes available"
InitGettingTemplates = "Getting modes available\n"
InitGettingVulcan = "Getting templates available\n"
InitProjectQuestion = "(Hit enter to accept the suggested name in parenthesis) Your project's name: "
EdgeApplicationsInitFlagHelp = "Displays more information about the init command"
EdgeApplicationsInitSuccessful = "Your project %s was initialized successfully\n"
Expand Down
1 change: 1 addition & 0 deletions messages/link/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ var (
ErrorFailedCreatingWorkerDirectory = errors.New("Failed to create the worker directory. The worker's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrorFailedCreatingAzionDirectory = errors.New("Failed to create the azion directory. The public's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrReadEnvFile = errors.New("Failed to read the webdev.env file. Verify if the file is corrupted or changed or run the 'azioncli edge_applications publish' command again")
ErrorDeps = errors.New("Failed to install project dependencies")
)
2 changes: 1 addition & 1 deletion messages/link/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
EdgeApplicationsLinkFlagTemplate = "The Edge Application's preset"
EdgeApplicationsLinkFlagMode = "The Edge Application's mode"
WebAppLinkCmdSuccess = "Template successfully fetched and configured\n\n"
LinkGettingTemplates = "Getting templates available"
LinkGettingTemplates = "Getting templates available\n"
LinkProjectQuestion = "(Hit enter to accept the suggested name in parenthesis) Your project's name: "
EdgeApplicationsLinkFlagHelp = "Displays more information about the link command"
EdgeApplicationsLinkSuccessful = "Your project %s was linked successfully\n"
Expand Down
4 changes: 2 additions & 2 deletions messages/root/messages.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package root

var (
RootUsage = "azion <command> <subcommand> [flags]\n azion [flags]\n\t"
RootDescription = "Azion CLI %s"
RootUsage = "azion <command> <subcommand> [flags]"
RootDescription = "The Azion Command Line Interface is a unified tool to manage your Azion projects and resources"
RootHelpFlag = "Displays more information about the Azion CLI"
RootDoNotUpdate = "Do not receive update notification"
RootLogDebug = "Displays log at a debug level"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Client) Upload(ctx context.Context, fileOps *contracts.FileOps) error {
req := c.apiClient.DefaultApi.StorageVersionIdPost(ctx, fileOps.VersionID).XAzionStaticPath(fileOps.Path).Body(fileOps.FileContent).ContentType(fileOps.MimeType)
_, httpResp, err := req.Execute()
if err != nil {
logger.Debug("Error while uploading file to storage api", zap.Error(err))
logger.Debug("Error while uploading file <"+fileOps.Path+"> to storage api", zap.Error(err))
return utils.ErrorPerStatusCode(httpResp, err)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/build/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func RunBuildCmdLine(cmd *BuildCmd) error {

conf, err := cmd.GetAzionJsonContent()
if err != nil {
return err
logger.Debug("Error while building your project", zap.Error(err))
return msg.ErrorBuilding
}

if Preset != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDeployCmd(t *testing.T) {

err := cmd.Execute()

require.EqualError(t, err, "Failed to open the azion.json file. The file doesn't exist, is corrupted, or has an invalid JSON format. Verify if the file format is JSON or fix its content according to the JSON format specification at https://www.json.org/json-en.html")
require.EqualError(t, err, "Failed to build your resource. Azion configuration not found. Make sure you are in the root directory of your local repository and have already initialized or linked your resource with the commands 'azion init' or 'azion link'")
})

t.Run("failed to create application", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (cmd *DeployCmd) uploadFiles(f *cmdutil.Factory, pathStatic string, version

// Create worker goroutines
for i := 1; i <= noOfWorkers; i++ {
go worker(i, jobs, results, &currentFile, clientUpload)
go worker(jobs, results, &currentFile, clientUpload)
}

bar := progressbar.NewOptions(
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// worker reads the range of jobs and uploads the file, if there is an error during upload, we returning it through the results channel
func worker(wID int, jobs <-chan contracts.FileOps, results chan<- error, currentFile *int64, clientUpload *storage.Client) {
func worker(jobs <-chan contracts.FileOps, results chan<- error, currentFile *int64, clientUpload *storage.Client) {

for job := range jobs {
if err := clientUpload.Upload(context.Background(), &job); err != nil {
Expand Down
47 changes: 25 additions & 22 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/aziontech/azion-cli/pkg/cmd/deploy"
"github.com/aziontech/azion-cli/pkg/cmd/dev"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/utils"
Expand Down Expand Up @@ -90,7 +89,6 @@ func NewInitCmd(f *cmdutil.Factory) *InitCmd {
}

func NewCobraCmd(init *InitCmd, f *cmdutil.Factory) *cobra.Command {
options := &contracts.AzionApplicationOptions{}
info := &InitInfo{}
cobraCmd := &cobra.Command{
Use: msg.EdgeApplicationsInitUsage,
Expand All @@ -108,7 +106,7 @@ func NewCobraCmd(init *InitCmd, f *cmdutil.Factory) *cobra.Command {
`),
RunE: func(cmd *cobra.Command, args []string) error {
info.GlobalFlagAll = f.GlobalFlagAll
return init.run(info, options, cmd)
return init.Run(info)
},
}

Expand All @@ -122,7 +120,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewInitCmd(f), f)
}

func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptions, c *cobra.Command) error {
func (cmd *InitCmd) Run(info *InitInfo) error {
logger.Debug("Running init command")

path, err := cmd.GetWorkDir()
Expand All @@ -134,17 +132,17 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio

switch info.Template {
case "simple":
return initSimple(cmd, path, info, c)
return initSimple(cmd, path, info)
case "static":
return initStatic(cmd, info, options, c)
return initStatic(cmd, info)
}

// Checks for global --yes flag and that name flag was not sent
if info.GlobalFlagAll && !c.Flags().Changed("name") {
if info.GlobalFlagAll && info.Name == "" {
info.Name = thoth.GenerateName()
} else {
// if name was not sent we ask for input, otherwise info.Name already has the value
if !c.Flags().Changed("name") {
if info.Name == "" {
projName, err := askForInput(msg.InitProjectQuestion, thoth.GenerateName())
if err != nil {
return err
Expand All @@ -154,11 +152,9 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio
}
}

if !c.Flags().Changed("template") || !c.Flags().Changed("mode") {
err = cmd.selectVulcanTemplates(info)
if err != nil {
return err
}
err = cmd.selectVulcanTemplates(info)
if err != nil {
return err
}

info.PathWorkingDir = info.PathWorkingDir + "/" + info.Name
Expand All @@ -172,26 +168,29 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio
err = cmd.ChangeDir(info.PathWorkingDir)
if err != nil {
logger.Debug("Error while changing to new working directory", zap.Error(err))
return msg.ErrorDeps
return msg.ErrorWorkingDir
}

shouldDev, err := cmd.ShouldDevDeploy(info, "Do you want to start a local development server?")
if err != err {
return err
}
if shouldDev {

shouldYarn, err := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to start local development server")
shouldDeps, err := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to start local development server")
if err != err {
return err
}

if shouldYarn {
err = yarnInstall(cmd)
if shouldDeps {
answer, err := utils.GetPackageManager()
if err != nil {
logger.Debug("Failed to install project dependencies")
return err
}
err = depsInstall(cmd, answer)
if err != nil {
logger.Debug("Error while installing project dependencies", zap.Error(err))
return msg.ErrorDeps
}
}

logger.Debug("Running dev command from init command")
Expand All @@ -210,13 +209,17 @@ func (cmd *InitCmd) run(info *InitInfo, options *contracts.AzionApplicationOptio
return err
}
if shouldDeploy {
shouldYarn, err := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to deploy your project")
shouldDeps, err := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to deploy your project")
if err != err {
return err
}

if shouldYarn {
err = yarnInstall(cmd)
if shouldDeps {
answer, err := utils.GetPackageManager()
if err != nil {
return err
}
err = depsInstall(cmd, answer)
if err != nil {
logger.Debug("Failed to install project dependencies")
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/init/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

func initSimple(cmd *InitCmd, path string, info *InitInfo, c *cobra.Command) error {
func initSimple(cmd *InitCmd, path string, info *InitInfo) error {
var err error
var shouldFetchTemplates bool
options := &contracts.AzionApplicationSimple{}
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/init/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"fmt"

msg "github.com/aziontech/azion-cli/messages/init"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"
thoth "github.com/aziontech/go-thoth"
"github.com/spf13/cobra"
)

func initStatic(cmd *InitCmd, info *InitInfo, options *contracts.AzionApplicationOptions, c *cobra.Command) error {
func initStatic(cmd *InitCmd, info *InitInfo) error {
shouldFetchTemplates, err := shouldFetch(cmd, info)
if err != nil {
return err
Expand All @@ -20,7 +18,7 @@ func initStatic(cmd *InitCmd, info *InitInfo, options *contracts.AzionApplicatio
if info.GlobalFlagAll {
info.Name = thoth.GenerateName()
} else {
if !c.Flags().Changed("name") {
if info.Name == "" {
projName, err := askForInput(msg.InitProjectQuestion, thoth.GenerateName())
if err != nil {
return err
Expand Down
56 changes: 30 additions & 26 deletions pkg/cmd/init/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package init

import (
"fmt"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -68,47 +69,50 @@ func askForInput(msg string, defaultIn string) (string, error) {
}

func (cmd *InitCmd) selectVulcanTemplates(info *InitInfo) error {
logger.FInfo(cmd.Io.Out, msg.InitGettingTemplates)
logger.FInfo(cmd.Io.Out, msg.InitGettingVulcan)

err := cmd.CommandRunInteractive(cmd.F, "npx --yes [email protected] init --name "+info.Name)
if err != nil {
return err
}

output, _, err := cmd.CommandRunner("npx --yes [email protected] presets ls", []string{"CLEAN_OUTPUT_MODE=true"})
if err != nil {
return err
}
if info.Template == "" || info.Mode == "" {
output, _, err := cmd.CommandRunner("npx --yes [email protected] presets ls", []string{"CLEAN_OUTPUT_MODE=true"})
if err != nil {
return err
}

newLineSplit := strings.Split(output, "\n")
newLineSplit[len(newLineSplit)-1] = "static (azion)"
newLineSplit := strings.Split(output, "\n")
newLineSplit[len(newLineSplit)-1] = "static (azion)"

answer := ""
template := ""
mode := ""
prompt := &survey.Select{
Message: "Choose a mode:",
Options: newLineSplit,
}
err = survey.AskOne(prompt, &answer)
if err != nil {
return err
}
answer := ""
template := ""
mode := ""
prompt := &survey.Select{
Message: "Choose a mode:",
Options: newLineSplit,
}
err = survey.AskOne(prompt, &answer)
if err != nil {
return err
}

modeSplit := strings.Split(answer, " ")
template = modeSplit[0]
mode = strings.Replace(strings.Replace(modeSplit[1], "(", "", -1), ")", "", -1)
modeSplit := strings.Split(answer, " ")
template = modeSplit[0]
mode = strings.Replace(strings.Replace(modeSplit[1], "(", "", -1), ")", "", -1)

info.Template = template
info.Mode = mode
info.Template = template
info.Mode = mode

}

return nil
}

func yarnInstall(cmd *InitCmd) error {
func depsInstall(cmd *InitCmd, packageManager string) error {
logger.FInfo(cmd.Io.Out, msg.InitInstallDeps)

err := cmd.CommandRunInteractive(cmd.F, "yarn install")
command := fmt.Sprintf("%s install", packageManager)
err := cmd.CommandRunInteractive(cmd.F, command)
if err != nil {
logger.Debug("Error while running command with simultaneous output", zap.Error(err))
return msg.ErrorDeps
Expand Down
Loading

0 comments on commit 8fb6dd9

Please sign in to comment.