Skip to content

Commit 5490645

Browse files
author
blackmarllbor0
committed
creating a Makefile with setting for startup, build and lint && wrote a sh script to clear the dir for tests
1 parent 5893a36 commit 5490645

File tree

7 files changed

+78
-14
lines changed

7 files changed

+78
-14
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
.idea
1+
.idea
2+
3+
# thses are not code tests, but tests of the application itself
4+
test

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ rm-program:
1515
sudo rm /usr/local/bin/cps
1616

1717
test:
18-
sudo make rm-program && make exec
18+
sudo make rm-program && make exec
19+
20+
clear:
21+
sh ./script/clear.sh

cmd/create-project-struct.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ func main() {
1313
if err := project.CreateProject(); err != nil {
1414
logger.Error(err)
1515
}
16+
17+
logger.Info("the project dir has been successfully created")
1618
}

internal/app/dir/dir.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const (
2222
)
2323

2424
type Dirs struct {
25-
projectName string
26-
currentDir bool
27-
file *file.File
25+
projectName string
26+
isCurrentDir bool
27+
file *file.File
2828
}
2929

3030
func NewDirs() *Dirs {
@@ -46,7 +46,7 @@ func (d *Dirs) CreateProject() error {
4646
func (d *Dirs) createProjectDir() error {
4747
projectDir, currentDir, err := args.GetProjectName()
4848
d.projectName = path.Base(projectDir)
49-
d.currentDir = currentDir
49+
d.isCurrentDir = currentDir
5050
if err != nil {
5151
return err
5252
}
@@ -62,11 +62,12 @@ func (d *Dirs) createProjectDir() error {
6262

6363
func (d *Dirs) createProjectDirs() error {
6464
projectDirs := [4]string{cmdDir, pkgDir, internalDir, cfgDir}
65+
6566
for i := 0; i < len(projectDirs); i++ {
6667
currentDir := projectDirs[i]
6768

6869
var dir string
69-
if d.currentDir {
70+
if d.isCurrentDir {
7071
dir = currentDir
7172
} else {
7273
dir = fmt.Sprintf("%s/%s", d.projectName, currentDir)
@@ -76,7 +77,7 @@ func (d *Dirs) createProjectDirs() error {
7677
return err
7778
}
7879

79-
// в зависимости от текущего создаваемого каталога создаём файлы или подкаталоги.
80+
// depending on the current directory being created, create files or subdirectories.
8081
if currentDir == cmdDir {
8182
if err := d.file.GenerateMainFile(dir + "/" + d.projectName + ".go"); err != nil {
8283
return err
@@ -97,7 +98,11 @@ func (d *Dirs) createProjectDirs() error {
9798
return err
9899
}
99100

100-
if err := d.file.GenerateGoModFile(currentDir+"/"+d.projectName, d.currentDir); err != nil {
101+
if err := d.file.GenerateGoModFile(currentDir+"/"+d.projectName, d.isCurrentDir); err != nil {
102+
return err
103+
}
104+
105+
if err := d.file.GenerateMakefile(d.projectName, d.isCurrentDir); err != nil {
101106
return err
102107
}
103108

@@ -111,7 +116,7 @@ func (d *Dirs) createInternalSubDir() error {
111116
currentDir := internalSubDirs[i]
112117

113118
var createSubDirPath string
114-
if !d.currentDir {
119+
if !d.isCurrentDir {
115120
createSubDirPath = d.projectName + "/" + internalDir + "/" + currentDir
116121
} else {
117122
createSubDirPath = internalDir + "/" + currentDir

internal/app/file/file.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
"github.com/blackmarllboro/create-project-struct/pkg/version"
99
)
1010

11-
type File struct {
12-
}
11+
type File struct{}
1312

1413
func NewFile() *File {
1514
return &File{}
@@ -60,7 +59,7 @@ func (fl File) GenerateCfgFile(dir string) error {
6059
return nil
6160
}
6261

63-
func (fl File) GenerateGoModFile(dir string, currentDir bool) error {
62+
func (fl File) GenerateGoModFile(dir string, isCurrentDir bool) error {
6463
goVersion, err := version.GoVersion()
6564
if err != nil {
6665
return err
@@ -71,7 +70,7 @@ func (fl File) GenerateGoModFile(dir string, currentDir bool) error {
7170
content := fmt.Sprintf("module %s\n\n%s", projectName, goVersion)
7271

7372
var creatingFile string
74-
if currentDir {
73+
if isCurrentDir {
7574
creatingFile = "go.mod"
7675
} else {
7776
creatingFile = projectName + "/go.mod"
@@ -83,3 +82,27 @@ func (fl File) GenerateGoModFile(dir string, currentDir bool) error {
8382

8483
return nil
8584
}
85+
86+
func (fl File) GenerateMakefile(projectName string, isCurrentDir bool) error {
87+
content := fmt.Sprintf(
88+
"PROJECT_NAME = %s\n"+
89+
"PROJECT_PATH = cmd/$(PROJECT_NAME).go\n"+
90+
"run:\n\tgo run $(PROJECT_PATH)\n\n"+
91+
"build:\n\tgo build -o bin/$(PROGRAM_NAME) $(PROJECT_PATH)\n\n"+
92+
"lint:\n\tgolangci-lint run",
93+
projectName,
94+
)
95+
96+
var creatingFile string
97+
if isCurrentDir {
98+
creatingFile = "Makefile"
99+
} else {
100+
creatingFile = projectName + "/Makefile"
101+
}
102+
103+
if err := fl.createAndWriteFile(creatingFile, content); err != nil {
104+
return err
105+
}
106+
107+
return nil
108+
}

internal/pkg/args/args.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func GetProjectName() (string, bool, error) {
2121
if err != nil {
2222
return "", false, err
2323
}
24+
2425
return pwd, true, nil
2526
}
2627

script/clear.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
test="$( cd "$( dirname "$0" )" && pwd)/../test"
4+
5+
current="$test/current"
6+
new="$test/new"
7+
8+
msg="everything is clean as it is"
9+
10+
if [ ! -d "$current" ]; then
11+
mkdir "$current"
12+
if [ ! -d "$new" ]; then
13+
echo "$msg"
14+
else
15+
rm -r "$new"
16+
fi
17+
else
18+
if [ ! -d "$new" ] && [ -n "$( ls -A "$current" )" ]; then
19+
rm -rf "${current:?}/"*
20+
else
21+
if [ -n "$( ls -A "$current" )" ]; then
22+
rm -rf "${current:?}/"*
23+
fi
24+
25+
rm -r "$new"
26+
fi
27+
fi

0 commit comments

Comments
 (0)