Skip to content

Commit

Permalink
refactor(app): move helpers directory to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
varrcan committed Apr 9, 2024
1 parent 153cf29 commit 3635297
Show file tree
Hide file tree
Showing 23 changed files with 66 additions and 73 deletions.
14 changes: 7 additions & 7 deletions command/cert_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/cert"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand All @@ -29,13 +29,13 @@ func installCertCommand() *cobra.Command {
}

func installCertRun() {
certutilPath, err := helper.CertutilPath()
certutilPath, err := utils.CertutilPath()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
}

err = helper.CreateDirectory(filepath.Join(helper.CertDir(), "conf"))
err = utils.CreateDirectory(filepath.Join(utils.CertDir(), "conf"))
if err != nil {
pterm.FgRed.Printfln("Error: %s \n", err)
os.Exit(1)
Expand All @@ -45,7 +45,7 @@ func installCertRun() {
CertutilPath: certutilPath,
CaFileName: cert.CaRootName,
CaFileKeyName: cert.CaRootKeyName,
CaPath: helper.CertDir(),
CaPath: utils.CertDir(),
}

if reinstallCert {
Expand All @@ -55,11 +55,11 @@ func installCertRun() {
return
}
c.Uninstall()
helper.RemoveFilesInPath(filepath.Join(helper.CertDir(), "conf"))
helper.RemoveFilesInPath(helper.CertDir())
utils.RemoveFilesInPath(filepath.Join(utils.CertDir(), "conf"))
utils.RemoveFilesInPath(utils.CertDir())
}

_, err = os.Stat(filepath.Join(helper.CertDir(), cert.CaRootName))
_, err = os.Stat(filepath.Join(utils.CertDir(), cert.CaRootName))
if err != nil {
err := c.CreateCA()
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions command/cert_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"path/filepath"

"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/cert"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand All @@ -23,7 +23,7 @@ func uninstallCertCommand() *cobra.Command {
}

func uninstallCertRun() {
certutilPath, err := helper.CertutilPath()
certutilPath, err := utils.CertutilPath()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
Expand All @@ -33,7 +33,7 @@ func uninstallCertRun() {
CertutilPath: certutilPath,
CaFileName: cert.CaRootName,
CaFileKeyName: cert.CaRootKeyName,
CaPath: helper.CertDir(),
CaPath: utils.CertDir(),
}

err = c.LoadCA()
Expand All @@ -50,8 +50,8 @@ func uninstallCertRun() {

c.Uninstall()

helper.RemoveFilesInPath(filepath.Join(helper.CertDir(), "conf"))
helper.RemoveFilesInPath(helper.CertDir())
utils.RemoveFilesInPath(filepath.Join(utils.CertDir(), "conf"))
utils.RemoveFilesInPath(utils.CertDir())

storeCertConfig(false)
pterm.FgYellow.Println("The local CA is now uninstalled from the browsers trust store!")
Expand Down
8 changes: 4 additions & 4 deletions command/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os/exec"
"path/filepath"

"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/project"
"github.com/local-deploy/dl/utils"
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -33,11 +33,11 @@ func downRun() {
pterm.FgGreen.Printfln("Stopping project...")

if viper.GetBool("ca") {
_ = helper.RemoveDirectory(filepath.Join(helper.CertDir(), "conf", project.Env.GetString("NETWORK_NAME")+".yaml"))
_ = helper.RemoveDirectory(filepath.Join(helper.CertDir(), project.Env.GetString("NETWORK_NAME")))
_ = utils.RemoveDirectory(filepath.Join(utils.CertDir(), "conf", project.Env.GetString("NETWORK_NAME")+".yaml"))
_ = utils.RemoveDirectory(filepath.Join(utils.CertDir(), project.Env.GetString("NETWORK_NAME")))
}

bin, option := helper.GetCompose()
bin, option := utils.GetCompose()
Args := []string{bin}
preArgs := []string{"-p", project.Env.GetString("NETWORK_NAME"), "down"}

Expand Down
7 changes: 3 additions & 4 deletions command/self_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/docker/compose/v2/pkg/progress"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/github"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -48,7 +47,7 @@ func selfUpdateCommand() *cobra.Command {
func selfUpdateRun(ctx context.Context) (string, error) {
w := progress.ContextWriter(ctx)

if helper.IsAptInstall() {
if utils.IsAptInstall() {
pterm.FgYellow.Println("Please use command:")
pterm.Println()
pterm.FgGreen.Println("sudo apt update\nsudo apt install dl")
Expand Down Expand Up @@ -223,11 +222,11 @@ func extractArchive(archivePath string) error {
}

func copyBin() error {
binPath := helper.BinPath()
binPath := utils.BinPath()

tmpBinPath := filepath.Join(os.TempDir(), "dl", "dl")

if helper.IsBinFileExists() {
if utils.IsBinFileExists() {
err := os.Remove(binPath)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions command/service_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/compose-spec/compose-go/v2/types"
"github.com/local-deploy/dl/containers"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +51,7 @@ func MapsAppend[T comparable, U any](target map[T]U, source map[T]U) map[T]U {
}

func upServiceRun(ctx context.Context) error {
if !helper.WpdeployCheck() {
if !utils.WpdeployCheck() {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/docker"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,7 +87,7 @@ func getServices(ctx context.Context, cli *docker.Client) ([]docker.ContainerSum

func getProjects(ctx context.Context, cli *docker.Client) ([]docker.ContainerSummary, error) {
containerFilter := filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", api.WorkingDirLabel, helper.TemplateDir())),
filters.Arg("label", fmt.Sprintf("%s=%s", api.WorkingDirLabel, utils.TemplateDir())),
)
containers, _ := cli.DockerCli.Client().ContainerList(ctx, container.ListOptions{Filters: containerFilter, All: true})

Expand Down
5 changes: 2 additions & 3 deletions command/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"strings"

"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/project"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/docker"
Expand Down Expand Up @@ -37,7 +36,7 @@ Analogue of the "docker-compose up -d" command.`,
func upRun() {
project.LoadEnv()

if !helper.WpdeployCheck() {
if !utils.WpdeployCheck() {
return
}

Expand All @@ -63,7 +62,7 @@ func upRun() {
project.CreateCert()
}

bin, option := helper.GetCompose()
bin, option := utils.GetCompose()
Args := []string{bin}
preArgs := []string{"-p", project.Env.GetString("NETWORK_NAME"), "--project-directory", project.Env.GetString("PWD"), "up", "-d"}

Expand Down
4 changes: 2 additions & 2 deletions containers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"path/filepath"

"github.com/compose-spec/compose-go/v2/types"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
)

// ServicesNetworkName network name for service containers
Expand Down Expand Up @@ -59,7 +59,7 @@ func Traefik() types.ServiceConfig {
},
{
Type: types.VolumeTypeBind,
Source: filepath.Join(helper.ConfigDir(), "certs"),
Source: filepath.Join(utils.ConfigDir(), "certs"),
Target: "/certs",
ReadOnly: true,
},
Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/local-deploy/dl/command"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/pterm/pterm"
"github.com/spf13/viper"
Expand All @@ -29,11 +28,11 @@ func main() {
return
}

if !helper.IsConfigFileExists() {
if !utils.IsConfigFileExists() {
firstStart()
}

if !helper.IsCertPathExists() {
if !utils.IsCertPathExists() {
createCertDirectory()
}

Expand All @@ -43,7 +42,7 @@ func main() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
configDir := helper.ConfigDir()
configDir := utils.ConfigDir()

viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
Expand Down Expand Up @@ -74,7 +73,7 @@ func firstStart() {
os.Exit(1)
}

if !helper.IsAptInstall() {
if !utils.IsAptInstall() {
err = utils.CreateTemplates(true)
if err != nil {
pterm.FgRed.Printfln("Unable to create template files: %s \n", err)
Expand All @@ -84,9 +83,9 @@ func firstStart() {
}

func createConfigFile() error {
configDir := helper.ConfigDir()
configDir := utils.ConfigDir()

err := helper.CreateDirectory(configDir)
err := utils.CreateDirectory(configDir)
if err != nil {
return err
}
Expand All @@ -110,7 +109,7 @@ func createConfigFile() error {
}

func createCertDirectory() {
err := helper.CreateDirectory(filepath.Join(helper.CertDir(), "conf"))
err := utils.CreateDirectory(filepath.Join(utils.CertDir(), "conf"))
if err != nil {
pterm.FgRed.Printfln("Unable to create certs directory: %s \n", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions project/deploy_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/docker/compose/v2/pkg/progress"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/client"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -144,7 +144,7 @@ echo $settings["connections"]["value"]["default"]["password"]."\n";'`,
return nil, err
}

dbArray := helper.CleanSlice(strings.Split(strings.TrimSpace(string(cat)), "\n"))
dbArray := utils.CleanSlice(strings.Split(strings.TrimSpace(string(cat)), "\n"))
logrus.Infof("Received variables: %s", dbArray)
if len(dbArray) != 4 {
return nil, errors.New("failed to define DB variables, please specify accesses manually")
Expand Down
4 changes: 2 additions & 2 deletions project/deploy_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/docker/compose/v2/pkg/progress"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/local-deploy/dl/utils/client"
"github.com/sirupsen/logrus"
"golang.org/x/text/cases"
Expand Down Expand Up @@ -179,7 +179,7 @@ func ExtractArchive(ctx context.Context, path string) error {
for _, dir := range s {
logrus.Infof("Run chmod 775: %s", dir)
chmodDir := filepath.Join(destinationPath, dir)
err = helper.ChmodR(chmodDir, 0775)
err = utils.ChmodR(chmodDir, 0775)
if err != nil {
w.Event(progress.ErrorMessageEvent("Files", fmt.Sprint(err)))
return err
Expand Down
8 changes: 4 additions & 4 deletions project/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"strings"

"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -59,7 +59,7 @@ func LoadEnv() {
// setNetworkName Set network name from project name
func setDefaultEnv() {
dir, _ := os.Getwd()
home, _ := helper.HomeDir()
home, _ := utils.HomeDir()
Env.SetDefault("HOST_NAME", filepath.Base(dir))
Env.SetDefault("PWD", dir)
Env.SetDefault("HOME", home)
Expand All @@ -75,7 +75,7 @@ func setDefaultEnv() {
res := re.ReplaceAllString(projectName, "")
Env.SetDefault("NETWORK_NAME", res)

confDir := helper.TemplateDir()
confDir := utils.TemplateDir()
Env.SetDefault("NGINX_CONF", filepath.Join(confDir, "default.conf.template"))

customConfig := Env.GetString("NGINX_CONF")
Expand Down Expand Up @@ -107,7 +107,7 @@ func setDefaultEnv() {
// setComposeFile Set docker-compose files
func setComposeFiles() {
var files []string
templateDir := helper.TemplateDir()
templateDir := utils.TemplateDir()

images := map[string]string{
"mysql": templateDir + "/docker-compose-mysql.yaml",
Expand Down
Loading

0 comments on commit 3635297

Please sign in to comment.