Skip to content

Commit

Permalink
Merge pull request #952 from aziontech/read-info-json
Browse files Browse the repository at this point in the history
chore: read info.json instead of .vulcan
  • Loading branch information
PatrickMenoti authored Sep 17, 2024
2 parents a4afad5 + 89213c9 commit cad0af3
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 47 deletions.
1 change: 1 addition & 0 deletions messages/init/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ var (
ErrorModeNotFound = errors.New("No mode was found for the selected template. For more information, run the command again using the '--debug' flag. If the problem persists, contact Azion’s support")
ErrorReadingGitignore = errors.New("Failed to read your .gitignore file")
ErrorWritingGitignore = errors.New("Failed to write to your .gitignore file")
ErrorGetProjectInfo = errors.New("Failed to get project info (preset and mode)")
)
9 changes: 4 additions & 5 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/aziontech/azion-cli/pkg/output"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"github.com/aziontech/azion-cli/utils"
helpers "github.com/aziontech/azion-cli/utils"
thoth "github.com/aziontech/go-thoth"
"github.com/go-git/go-git/v5"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -266,10 +265,10 @@ func (cmd *initCmd) Run(c *cobra.Command, _ []string) error {
vul := vulcanPkg.NewVulcan()
err = cmd.selectVulcanTemplates(vul)
if err != nil {
return err
return msg.ErrorGetProjectInfo
}

if cmd.auto || !helpers.Confirm(cmd.globalFlagAll, msg.AskLocalDev, false) {
if cmd.auto || !utils.Confirm(cmd.globalFlagAll, msg.AskLocalDev, false) {
logger.FInfoFlags(cmd.f.IOStreams.Out, msg.InitDevCommand, cmd.f.Format, cmd.f.Out)
msgs = append(msgs, msg.InitDevCommand)
} else {
Expand All @@ -285,7 +284,7 @@ func (cmd *initCmd) Run(c *cobra.Command, _ []string) error {
}
}

if cmd.auto || !helpers.Confirm(cmd.globalFlagAll, msg.AskDeploy, false) {
if cmd.auto || !utils.Confirm(cmd.globalFlagAll, msg.AskDeploy, false) {
logger.FInfoFlags(cmd.f.IOStreams.Out, msg.InitDeployCommand, cmd.f.Format, cmd.f.Out)
msgs = append(msgs, msg.InitDeployCommand)
msgEdgeAppInitSuccessFull := fmt.Sprintf(msg.EdgeApplicationsInitSuccessful, cmd.name)
Expand Down Expand Up @@ -318,7 +317,7 @@ func (cmd *initCmd) Run(c *cobra.Command, _ []string) error {

func (cmd *initCmd) deps(c *cobra.Command, m string, msgs *[]string) error {
if !c.Flags().Changed("package-manager") {
if !helpers.Confirm(cmd.globalFlagAll, m, true) {
if !utils.Confirm(cmd.globalFlagAll, m, true) {
return nil
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/cmd/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"go.uber.org/zap/zapcore"
)

var infoJsonData = `{"preset":"astro","mode":"deliver"}`

func TestNewCmd(t *testing.T) {
mock := &httpmock.Registry{}
f, _, _ := testutils.NewFactory(mock)
Expand Down Expand Up @@ -87,6 +89,9 @@ func Test_initCmd_Run(t *testing.T) {
name: "success flow",
fields: fields{
auto: true,
fileReader: func(filename string) ([]byte, error) {
return []byte(infoJsonData), nil
},
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
GlobalFlagAll: false,
Expand Down Expand Up @@ -160,6 +165,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error getWorkDir",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return []byte(infoJsonData), nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -235,6 +243,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error http.Get",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return []byte(infoJsonData), nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -310,6 +321,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error askForInput",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return []byte(infoJsonData), nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -385,6 +399,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error expected status OK",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return []byte(infoJsonData), nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -460,6 +477,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error readAll",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -537,6 +557,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error unmarshal",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -614,6 +637,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error askOne",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -689,6 +715,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error dir",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -766,6 +795,9 @@ func Test_initCmd_Run(t *testing.T) {
{
name: "error mkdirTemp",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
auto: true,
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
Expand Down Expand Up @@ -949,6 +981,9 @@ func Test_initCmd_deps(t *testing.T) {
{
name: "success flow",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
GlobalFlagAll: false,
Expand Down Expand Up @@ -977,6 +1012,9 @@ func Test_initCmd_deps(t *testing.T) {
{
name: "error depsInstall",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
f: &cmdutil.Factory{
Flags: cmdutil.Flags{
GlobalFlagAll: false,
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/init/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
writeFile func(filename string, data []byte, perm fs.FileMode) error
mkdir func(path string, perm os.FileMode) error
marshalIndent func(v any, prefix, indent string) ([]byte, error)
fileReader func(path string) ([]byte, error)
}
tests := []struct {
name string
Expand All @@ -33,6 +34,9 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
{
name: "success flow",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
name: "project_piece",
preset: "vite",
mode: "deliver",
Expand All @@ -49,6 +53,9 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
{
name: "error mkdir",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
name: "project_piece",
preset: "vite",
mode: "deliver",
Expand All @@ -65,6 +72,9 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
{
name: "error marshalIndent",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
name: "project_piece",
preset: "vite",
mode: "deliver",
Expand All @@ -83,6 +93,9 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
{
name: "error writeFile",
fields: fields{
fileReader: func(filename string) ([]byte, error) {
return nil, nil
},
name: "project_piece",
preset: "vite",
mode: "deliver",
Expand All @@ -106,6 +119,7 @@ func Test_initCmd_createTemplateAzion(t *testing.T) {
writeFile: tt.fields.writeFile,
mkdir: tt.fields.mkdir,
marshalIndent: tt.fields.marshalIndent,
fileReader: tt.fields.fileReader,
}
if err := cmd.createTemplateAzion(); (err != nil) != tt.wantErr {
t.Errorf("initCmd.createTemplateAzion() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
24 changes: 15 additions & 9 deletions pkg/cmd/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package init

import (
"fmt"
"os"
"path"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -58,7 +58,7 @@ func (cmd *initCmd) selectVulcanTemplates(vul *vulcanPkg.VulcanPkg) error {
return err
}

preset, mode, err := cmd.getVulcanEnvInfo()
preset, mode, err := cmd.getVulcanInfo()
if err != nil {
return err
}
Expand All @@ -82,15 +82,21 @@ func (cmd *initCmd) depsInstall() error {
return nil
}

func (cmd *initCmd) getVulcanEnvInfo() (string, string, error) {
err := cmd.load(cmd.pathWorkingDir + "/.vulcan")
func (cmd *initCmd) getVulcanInfo() (string, string, error) {

fileContent, err := cmd.fileReader(path.Join(cmd.pathWorkingDir, "info.json"))
if err != nil {
logger.Debug("Error reading template info", zap.Error(err))
return "", "", err
}

var infoJson map[string]string
err = cmd.unmarshal(fileContent, &infoJson)
if err != nil {
logger.Debug("Error loading .vulcan file", zap.Error(err))
logger.Debug("Error unmarshalling template info", zap.Error(err))
return "", "", err
}

// Access environment variables
preset := os.Getenv("preset")
mode := os.Getenv("mode")
return preset, mode, nil
logger.Debug("Information about the template:", zap.Any("preset", infoJson["preset"]), zap.Any("mode", infoJson["mode"]))
return infoJson["preset"], infoJson["mode"], nil
}
Loading

0 comments on commit cad0af3

Please sign in to comment.