Skip to content

Commit

Permalink
chore: read azion.json
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Oct 15, 2024
1 parent d6f8577 commit f312876
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
38 changes: 22 additions & 16 deletions pkg/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DeployCmd struct {
VersionID func() string
CallScript func(token string, id string, secret string, prefix string, name string, cmd *DeployCmd) (string, error)
OpenBrowser func(f *cmdutil.Factory, urlConsoleDeploy string, cmd *DeployCmd) error
CaptureLogs func(execId string, token string, cmd *DeployCmd) (string, error)
CaptureLogs func(execId string, token string, cmd *DeployCmd) error
CheckToken func(f *cmdutil.Factory) error
ReadSettings func() (token.Settings, error)
UploadFiles func(f *cmdutil.Factory, conf *contracts.AzionApplicationOptions, msgs *[]string, pathStatic, bucket string, cmd *DeployCmd) error
Expand Down Expand Up @@ -221,15 +221,21 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
return err
}

url, err := cmd.CaptureLogs(id, settings.Token, cmd)
err = cmd.CaptureLogs(id, settings.Token, cmd)
if err != nil {
return err
}

conf, err = cmd.GetAzionJsonContent(ProjectConf)
if err != nil {
logger.Debug("Failed to get Azion JSON content", zap.Error(err))
return err
}

logger.FInfoFlags(cmd.F.IOStreams.Out, msg.DeploySuccessful, f.Format, f.Out)
msgs = append(msgs, msg.DeploySuccessful)

msgfOutputDomainSuccess := fmt.Sprintf(msg.DeployOutputDomainSuccess, url)
msgfOutputDomainSuccess := fmt.Sprintf(msg.DeployOutputDomainSuccess, conf.Domain.Url)
logger.FInfoFlags(cmd.F.IOStreams.Out, msgfOutputDomainSuccess, f.Format, f.Out)
msgs = append(msgs, msgfOutputDomainSuccess)

Expand All @@ -239,7 +245,7 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
return nil
}

func captureLogs(execId, token string, cmd *DeployCmd) (string, error) {
func captureLogs(execId, token string, cmd *DeployCmd) error {
logsURL := fmt.Sprintf("%s/api/script-runner/executions/%s/logs", DeployURL, execId)
resultsURL := fmt.Sprintf("%s/api/script-runner/executions/%s/results", DeployURL, execId)

Expand All @@ -253,7 +259,7 @@ func captureLogs(execId, token string, cmd *DeployCmd) (string, error) {
req, err := http.NewRequest("GET", logsURL, bytes.NewBuffer([]byte{}))
if err != nil {
logger.Debug("Error creating request", zap.Error(err))
return "", err
return err
}

// Set headers
Expand All @@ -268,19 +274,19 @@ func captureLogs(execId, token string, cmd *DeployCmd) (string, error) {
resp, err := client.Do(req)
if err != nil {
logger.Debug("Error sending request", zap.Error(err))
return "", err
return err
}
defer resp.Body.Close()

// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return err
}

if err := cmd.Unmarshal(body, &Logs); err != nil {
logger.Debug("Error unmarshalling response", zap.Error(err))
return "", err
return err
}

switch Logs.Status {
Expand All @@ -292,7 +298,7 @@ func captureLogs(execId, token string, cmd *DeployCmd) (string, error) {
requestResults, err := http.NewRequest("GET", resultsURL, bytes.NewBuffer([]byte{}))
if err != nil {
logger.Debug("Error creating request", zap.Error(err))
return "", err
return err
}

// Set headers
Expand All @@ -306,36 +312,36 @@ func captureLogs(execId, token string, cmd *DeployCmd) (string, error) {
respResults, err := clientResults.Do(requestResults)
if err != nil {
logger.Debug("Error sending request", zap.Error(err))
return "", err
return err
}
defer respResults.Body.Close()

// Read the response
body, err := io.ReadAll(respResults.Body)
if err != nil {
return "", err
return err
}

if err := cmd.Unmarshal(body, &Result); err != nil {
logger.Debug("Error unmarshalling response", zap.Error(err))
return "", err
return err
}

if Result.Result.Errors != nil {
return "", errors.New(Result.Result.Errors.Stack) //TODO: add mensagem que deu ruim e é para verificar se criou algo na conta
return errors.New(Result.Result.Errors.Stack) //TODO: add mensagem que deu ruim e é para verificar se criou algo na conta
}

err = cmd.WriteAzionJsonContent(Result.Result.Azion, ProjectConf)
if err != nil {
return "", err
return err
}
default:
s.Stop()
return "", msg.ErrorDeployRemote
return msg.ErrorDeployRemote
}
s.Stop()
break
}

return Result.Result.Azion.Domain.Url, nil
return nil
}
6 changes: 3 additions & 3 deletions pkg/cmd/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func MockOpenBrowser(f *cmdutil.Factory, urlConsoleDeploy string, cmd *DeployCmd
}

// MockCaptureLogs mocks captureLogs behavior
func MockCaptureLogs(execId string, token string, cmd *DeployCmd) (string, error) {
return "", nil
func MockCaptureLogs(execId string, token string, cmd *DeployCmd) error {
return nil
}

func TestDeploy_Run(t *testing.T) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestCaptureLogs(t *testing.T) {
cmd.WriteAzionJsonContent = func(conf *contracts.AzionApplicationOptions, confConf string) error {
return nil
}
_, err := cmd.CaptureLogs(execID, token, cmd)
err := cmd.CaptureLogs(execID, token, cmd)

// Check for expected error
if tt.expectError {
Expand Down

0 comments on commit f312876

Please sign in to comment.