Skip to content

Commit

Permalink
refactor(app): placing compose files in the user directory
Browse files Browse the repository at this point in the history
Refs: CU-869494c6m
  • Loading branch information
varrcan committed May 2, 2024
1 parent aa87a4b commit 73d3aeb
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 38 deletions.
1 change: 0 additions & 1 deletion .github/scripts/packages/preinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions command/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions command/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

Expand Down
25 changes: 17 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var version = "dev"

//go:embed config-files/*
//go:embed templates/*
var templates embed.FS

func main() {
Expand All @@ -28,7 +28,7 @@ func main() {
return
}

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

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,5 +34,5 @@ overrides:
- nss
deb:
depends:
- docker-ce (>= 5:23) | docker-desktop
- docker-ce (>= 5:23)
- libnss3-tools (>= 2:2)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion utils/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 16 additions & 18 deletions utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions utils/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 73d3aeb

Please sign in to comment.