Skip to content

Commit

Permalink
init: refactor directories creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
psergee authored and LeonidVas committed Nov 8, 2022
1 parent f5945d0 commit 0020a79
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 51 deletions.
74 changes: 23 additions & 51 deletions cli/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ func loadCartridgeConfig(configPath string) (appDirInfo, error) {
}, nil
}

// createDirectories creates directories specified in dirList.
func createDirectories(dirList []string) error {
for _, dirName := range dirList {
if dirName == "" {
continue
}
if err := util.CreateDirectory(dirName, defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", dirName)
}
return nil
}

// generateTtEnv generates environment config in configPath using directories info from
// appDirInfo.
func generateTtEnv(configPath string, appDirInfo appDirInfo) error {
Expand All @@ -81,64 +95,22 @@ func generateTtEnv(configPath string, appDirInfo appDirInfo) error {
return err
}

// Create instances enabled directory if required.
if appDirInfo.instancesEnabled != "" {
cfg.CliConfig.App.InstancesEnabled = appDirInfo.instancesEnabled
if err := util.CreateDirectory(appDirInfo.instancesEnabled,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", appDirInfo.instancesEnabled)
}

// Create modules directory.
if cfg.CliConfig.Modules.Directory != "" {
if err := util.CreateDirectory(cfg.CliConfig.Modules.Directory,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", cfg.CliConfig.Modules.Directory)
}

// Create include directory.
if cfg.CliConfig.App.IncludeDir != "" {
if err := util.CreateDirectory(cfg.CliConfig.App.IncludeDir,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", cfg.CliConfig.App.IncludeDir)
}

// Create binary directory.
if cfg.CliConfig.App.BinDir != "" {
if err := util.CreateDirectory(cfg.CliConfig.App.BinDir,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", cfg.CliConfig.App.BinDir)
}

// Create install directory.
if cfg.CliConfig.App.BinDir != "" {
if err := util.CreateDirectory(cfg.CliConfig.Repo.Install,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", cfg.CliConfig.Repo.Install)
directoriesToCreate := []string{
cfg.CliConfig.App.InstancesEnabled,
cfg.CliConfig.Modules.Directory,
cfg.CliConfig.App.IncludeDir,
cfg.CliConfig.App.BinDir,
cfg.CliConfig.Repo.Install,
}

// Create templates directories.
if cfg.CliConfig.App.BinDir != "" {
for _, templatesPathOpts := range cfg.CliConfig.Templates {
if err := util.CreateDirectory(templatesPathOpts.Path,
defaultDirPermissions); err != nil {
return err
}
log.Debugf("'%s' directory is created.", templatesPathOpts)
}
for _, templatesPathOpts := range cfg.CliConfig.Templates {
directoriesToCreate = append(directoriesToCreate, templatesPathOpts.Path)
}

return nil
return createDirectories(directoriesToCreate)
}

// Run creates tt environment config for the application in current dir.
Expand Down
18 changes: 18 additions & 0 deletions cli/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,21 @@ func TestInitRunInvalidConfigSkipIt(t *testing.T) {
require.NoError(t, Run(&InitCtx{SkipConfig: true}))
checkDefaultEnv(t, configure.ConfigName, configure.InstancesEnabledDirName)
}

func TestCreateDirectories(t *testing.T) {
tmpDir := t.TempDir()
wd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(tmpDir))
defer os.Chdir(wd)

require.NoError(t, createDirectories([]string{
"dir1",
"dir2",
"",
"dir3/subdir",
}))
assert.DirExists(t, "dir1")
assert.DirExists(t, "dir2")
assert.DirExists(t, "dir3/subdir")
}

0 comments on commit 0020a79

Please sign in to comment.