diff --git a/.github/scripts/packages/preinstall.sh b/.github/scripts/packages/preinstall.sh index 1b70619..88516b0 100644 --- a/.github/scripts/packages/preinstall.sh +++ b/.github/scripts/packages/preinstall.sh @@ -2,5 +2,4 @@ set -e install -m 0755 -d /etc/apt/keyrings -install -m 0755 -d /etc/dl/config-files install -m 0755 -d /usr/share/zsh/vendor-completions diff --git a/command/down.go b/command/down.go index 153becc..e2e24e2 100644 --- a/command/down.go +++ b/command/down.go @@ -33,8 +33,8 @@ func downRun() { pterm.FgGreen.Printfln("Stopping project...") if viper.GetBool("ca") { - _ = utils.RemoveDirectory(filepath.Join(utils.CertDir(), "conf", project.Env.GetString("NETWORK_NAME")+".yaml")) - _ = utils.RemoveDirectory(filepath.Join(utils.CertDir(), project.Env.GetString("NETWORK_NAME"))) + _ = utils.RemovePath(filepath.Join(utils.CertDir(), "conf", project.Env.GetString("NETWORK_NAME")+".yaml")) + _ = utils.RemovePath(filepath.Join(utils.CertDir(), project.Env.GetString("NETWORK_NAME"))) } bin, option := utils.GetCompose() diff --git a/command/env.go b/command/env.go index 02ef8db..74ecb8b 100644 --- a/command/env.go +++ b/command/env.go @@ -51,7 +51,7 @@ func showEnvMenu() { } func printEnvConfig() { - src, _ := utils.Templates.Open(filepath.Join("config-files", getEnvName())) + src, _ := utils.Templates.Open(filepath.Join("templates", getEnvName())) scanner := bufio.NewScanner(src) pterm.Println() @@ -87,7 +87,7 @@ func copyEnv() bool { return false } } else { - src = filepath.Join("config-files", getEnvName()) + src = filepath.Join("templates", getEnvName()) bytesRead, _ = utils.Templates.ReadFile(src) } diff --git a/main.go b/main.go index cec51b9..c9323cf 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ import ( var version = "dev" -//go:embed config-files/* +//go:embed templates/* var templates embed.FS func main() { @@ -28,7 +28,7 @@ func main() { return } - if !utils.IsConfigFileExists() { + if utils.IsNeedInstall() { firstStart() } @@ -73,12 +73,16 @@ func firstStart() { os.Exit(1) } - if !utils.IsAptInstall() { - err = utils.CreateTemplates(true) - if err != nil { - pterm.FgRed.Printfln("Unable to create template files: %s \n", err) - os.Exit(1) - } + err = utils.CreateTemplates(true) + if err != nil { + pterm.FgRed.Printfln("Unable to create template files: %s \n", err) + os.Exit(1) + } + + // remove old template directory + err = utils.RemovePath(filepath.Join(utils.ConfigDir(), "config-files")) + if err != nil { + pterm.FgRed.Printfln("Unable to remove old template files: %s \n", err) } } @@ -90,6 +94,11 @@ func createConfigFile() error { return err } + // do not overwrite the config if it exists + if utils.PathExists(filepath.Join(configDir, "config.yaml")) { + return nil + } + viper.AddConfigPath(configDir) viper.SetConfigType("yaml") viper.SetConfigName("config") diff --git a/nfpm.yaml b/nfpm.yaml index a477df1..bdf3a74 100644 --- a/nfpm.yaml +++ b/nfpm.yaml @@ -22,8 +22,6 @@ scripts: contents: - src: bin/dl dst: /usr/bin/dl - - src: config-files/ - dst: /etc/dl/config-files - src: .github/scripts/packages/completions/zsh dst: /usr/share/zsh/vendor-completions/_dl - src: .github/scripts/packages/completions/bash @@ -36,5 +34,5 @@ overrides: - nss deb: depends: - - docker-ce (>= 5:23) | docker-desktop + - docker-ce (>= 5:23) - libnss3-tools (>= 2:2) diff --git a/config-files/.env.example b/templates/.env.example similarity index 100% rename from config-files/.env.example rename to templates/.env.example diff --git a/config-files/.env.example-bitrix b/templates/.env.example-bitrix similarity index 100% rename from config-files/.env.example-bitrix rename to templates/.env.example-bitrix diff --git a/config-files/default.conf.template b/templates/default.conf.template similarity index 100% rename from config-files/default.conf.template rename to templates/default.conf.template diff --git a/config-files/docker-compose-apache.yaml b/templates/docker-compose-apache.yaml similarity index 100% rename from config-files/docker-compose-apache.yaml rename to templates/docker-compose-apache.yaml diff --git a/config-files/docker-compose-fpm.yaml b/templates/docker-compose-fpm.yaml similarity index 100% rename from config-files/docker-compose-fpm.yaml rename to templates/docker-compose-fpm.yaml diff --git a/config-files/docker-compose-mariadb.yaml b/templates/docker-compose-mariadb.yaml similarity index 100% rename from config-files/docker-compose-mariadb.yaml rename to templates/docker-compose-mariadb.yaml diff --git a/config-files/docker-compose-memcached.yaml b/templates/docker-compose-memcached.yaml similarity index 100% rename from config-files/docker-compose-memcached.yaml rename to templates/docker-compose-memcached.yaml diff --git a/config-files/docker-compose-mysql.yaml b/templates/docker-compose-mysql.yaml similarity index 100% rename from config-files/docker-compose-mysql.yaml rename to templates/docker-compose-mysql.yaml diff --git a/config-files/docker-compose-pgsql.yaml b/templates/docker-compose-pgsql.yaml similarity index 100% rename from config-files/docker-compose-pgsql.yaml rename to templates/docker-compose-pgsql.yaml diff --git a/config-files/docker-compose-redis.yaml b/templates/docker-compose-redis.yaml similarity index 100% rename from config-files/docker-compose-redis.yaml rename to templates/docker-compose-redis.yaml diff --git a/utils/docker/client.go b/utils/docker/client.go index 1886f47..2ff6d81 100644 --- a/utils/docker/client.go +++ b/utils/docker/client.go @@ -13,6 +13,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" + "github.com/sirupsen/logrus" ) // NewClient docker client initialization @@ -45,7 +46,7 @@ func newDockerCli() (*command.DockerCli, error) { } options := flags.NewClientOptions() - // options.LogLevel = "fatal" + options.LogLevel = logrus.GetLevel().String() err = dockerCLI.Initialize(options) if err != nil { diff --git a/utils/path.go b/utils/path.go index 8e1b2b6..d7e816f 100644 --- a/utils/path.go +++ b/utils/path.go @@ -29,13 +29,9 @@ func ConfigDir() string { return filepath.Join(conf, "dl") } -// TemplateDir template directory (~/.config/dl or /etc/dl) +// TemplateDir template directory (~/.config/dl/templates) func TemplateDir() string { - if IsAptInstall() { - return filepath.Join("/", "etc", "dl", "config-files") - } - - return filepath.Join(ConfigDir(), "config-files") + return filepath.Join(ConfigDir(), "templates") } // binDir path to bin directory @@ -73,7 +69,7 @@ func CertutilPath() (string, error) { out, err := exec.Command("brew", "--prefix", "nss").Output() if err == nil { certutilPath := filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil") - if pathExists(certutilPath) { + if PathExists(certutilPath) { logrus.Infof("Found certutil: %s", certutilPath) return certutilPath, nil } @@ -107,7 +103,8 @@ func BinaryExists(name string) bool { return err == nil } -func pathExists(path string) bool { +// PathExists check for the existence of a path +func PathExists(path string) bool { _, err := os.Stat(path) return err == nil } @@ -122,21 +119,22 @@ func IsAptInstall() bool { return strings.EqualFold(binDir(), "/usr/bin") } -// IsConfigFileExists checking for the existence of a configuration file -func IsConfigFileExists() bool { +// IsNeedInstall checking for the existence of a configuration file +func IsNeedInstall() bool { config := filepath.Join(ConfigDir(), "config.yaml") + templates := filepath.Join(ConfigDir(), "templates") - return pathExists(config) + return !PathExists(config) || !PathExists(templates) } // IsBinFileExists checks the existence of a binary func IsBinFileExists() bool { - return pathExists(BinPath()) + return PathExists(BinPath()) } // IsCertPathExists check if the certificate directory exists func IsCertPathExists() bool { - return pathExists(CertDir()) + return PathExists(CertDir()) } // ChmodR change file permissions recursively @@ -152,7 +150,7 @@ func ChmodR(path string, mode os.FileMode) error { // CreateDirectory recursively create directories func CreateDirectory(path string) error { - if !pathExists(path) { + if !PathExists(path) { err := os.MkdirAll(path, 0755) if err != nil { return err @@ -162,9 +160,9 @@ func CreateDirectory(path string) error { return nil } -// RemoveDirectory recursively remove directories -func RemoveDirectory(path string) error { - if pathExists(path) { +// RemovePath recursively remove directories +func RemovePath(path string) error { + if PathExists(path) { err := os.RemoveAll(path) if err != nil { return err @@ -176,7 +174,7 @@ func RemoveDirectory(path string) error { // RemoveFilesInPath deleting files in a directory func RemoveFilesInPath(path string) { - if pathExists(path) { + if PathExists(path) { dir, _ := os.ReadDir(path) if len(dir) > 0 { for _, dirEntry := range dir { diff --git a/utils/templates.go b/utils/templates.go index 0fa0e4e..11dfe93 100644 --- a/utils/templates.go +++ b/utils/templates.go @@ -15,7 +15,7 @@ func CreateTemplates(overwrite bool) error { // delete existing directory if overwrite { - err := RemoveDirectory(templateDir) + err := RemovePath(templateDir) if err != nil { return err } @@ -26,7 +26,7 @@ func CreateTemplates(overwrite bool) error { return err } - entries, err := Templates.ReadDir("config-files") + entries, err := Templates.ReadDir("templates") if err != nil { return err } @@ -37,7 +37,7 @@ func CreateTemplates(overwrite bool) error { return err } - data, err := Templates.ReadFile(filepath.Join("config-files", entry.Name())) + data, err := Templates.ReadFile(filepath.Join("templates", entry.Name())) if err != nil { return err }