Skip to content

Commit 076528a

Browse files
Improved DI, also added a configuration file for the linter, At this stage, the project can be considered fully operational and safe for tge user. There be delays a waiting for the next update.
1 parent a03112a commit 076528a

File tree

17 files changed

+200
-160
lines changed

17 files changed

+200
-160
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22

33
# thses are not code tests, but tests of the application itself
4-
test
4+
test
5+
script

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ to be called. For example `cps my-app`.
3131
2. Layout of the main file;
3232
3. File `go.mod` with project name;
3333
4. Makefile with parameters:
34-
1. build: builds the application;
35-
2. run: start the app in dev mode;
36-
3. test: rub all tests;
37-
4. lint: checks your project for design errors;
38-
5. Creates a local repository
34+
1. `build`: builds the application;
35+
2. `run`: start the app in dev mode;
36+
3. `test`: rub all tests;
37+
4. `lint`: checks your project for design errors;
38+
5. Creates a local repository;
39+
6. Create a configuration file for `golangci-lint`;
3940

4041
### What is expected next?
4142

4243
Expected in upcoming updates:
4344

44-
1. Adding configuration for **golangci-lint**
45+
1. Building the project in PKGBUILD;
46+
2. Add some flags to select the type of project, some values (eg port for the server) in the terminal;
4547

4648
### Conclusion
4749

cmd/create-project-struct.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ import (
44
"github.com/blackmarllboro/create-project-struct/internal/app/dir"
55
"github.com/blackmarllboro/create-project-struct/internal/app/file"
66
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
7-
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
87
"github.com/blackmarllboro/create-project-struct/internal/pkg/log"
8+
"github.com/blackmarllboro/create-project-struct/internal/pkg/temp"
9+
"github.com/blackmarllboro/create-project-struct/pkg/git"
910

1011
l "github.com/charmbracelet/log"
1112
)
1213

1314
func main() {
1415
logger := log.NewLogger(l.New())
16+
projName := args.NewArgs()
17+
template := temp.NewTemp()
18+
newFile := file.NewFile(template)
19+
project := dir.NewDirs(newFile, projName)
1520

16-
projName := args.ProjectName{}
17-
18-
project := dir.NewDirs(file.NewFile(), projName)
1921
if err := project.CreateProject(); err != nil {
2022
logger.Error(err)
23+
return
2124
}
2225

2326
if err := git.CreateLocalGitRepository(projName); err != nil {
2427
logger.Error(err)
28+
return
2529
}
2630

27-
logger.Info("the project dir has been successfully created")
31+
logger.Info("The project dir has been successfully created")
2832
}

internal/app/dir/dir.go

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package dir
22

33
import (
4-
"errors"
54
"fmt"
6-
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
75
"os"
86
"path"
97

10-
"github.com/blackmarllboro/create-project-struct/internal/app/file"
8+
fileI "github.com/blackmarllboro/create-project-struct/internal/app/file/interfaces"
9+
argsI "github.com/blackmarllboro/create-project-struct/internal/pkg/args/interfaces"
1110
)
1211

1312
const perm = 0755 // Access rights to create folders
@@ -24,21 +23,22 @@ const (
2423
type Dirs struct {
2524
projectName string
2625
isCurrentDir bool
27-
file *file.File
28-
name args.GetProjectName
26+
27+
file fileI.File
28+
name argsI.GetProjectName
2929
}
3030

31-
func NewDirs(f *file.File, p args.ProjectName) *Dirs {
32-
return &Dirs{file: f, name: p}
31+
func NewDirs(file fileI.File, name argsI.GetProjectName) *Dirs {
32+
return &Dirs{file: file, name: name}
3333
}
3434

3535
func (d *Dirs) CreateProject() error {
3636
if err := d.createProjectDir(); err != nil {
37-
return err
37+
return fmt.Errorf("failed to create project directory: %v", err)
3838
}
3939

4040
if err := d.createProjectDirs(); err != nil {
41-
return err
41+
return fmt.Errorf("failed to create project directories: %v", err)
4242
}
4343

4444
return nil
@@ -49,12 +49,12 @@ func (d *Dirs) createProjectDir() error {
4949
d.projectName = path.Base(projectDir)
5050
d.isCurrentDir = currentDir
5151
if err != nil {
52-
return err
52+
return fmt.Errorf("failed to get project name, err: %s", err)
5353
}
5454

5555
if !currentDir {
5656
if err := os.Mkdir(d.projectName, perm); err != nil {
57-
return errors.New("this directory already exists")
57+
return fmt.Errorf("failed to create dir, err: %v", err)
5858
}
5959
}
6060

@@ -75,17 +75,17 @@ func (d *Dirs) createProjectDirs() error {
7575
}
7676

7777
if err := os.Mkdir(dir, perm); err != nil {
78-
return err
78+
return fmt.Errorf("failed to create dir, err: %v", err)
7979
}
8080

81-
// depending on the current directory being created, create files or subdirectories.
82-
if err := d.createFilesInSubdirs(currentDir, dir); err != nil {
83-
return err
84-
}
8581
}
8682

87-
if err := d.file.GenerateFilesInMainDir(d.projectName, d.isCurrentDir); err != nil {
88-
return err
83+
if err := d.createInternalSubDir(); err != nil {
84+
return fmt.Errorf("failed to create internal sub dir, err: %v", err)
85+
}
86+
87+
if err := d.file.GenerateFiles(d.isCurrentDir, d.projectName); err != nil {
88+
return fmt.Errorf("failed to generate files, err: %v", err)
8989
}
9090

9191
return nil
@@ -99,32 +99,13 @@ func (d *Dirs) createInternalSubDir() error {
9999

100100
var createSubDirPath string
101101
if !d.isCurrentDir {
102-
createSubDirPath = d.projectName + "/" + internalDir + "/" + currentDir
102+
createSubDirPath = fmt.Sprintf("%s/%s/%s", d.projectName, internalDir, currentDir)
103103
} else {
104-
createSubDirPath = internalDir + "/" + currentDir
104+
createSubDirPath = fmt.Sprintf("%s/%s", internalDir, currentDir)
105105
}
106106

107107
if err := os.Mkdir(createSubDirPath, perm); err != nil {
108-
return err
109-
}
110-
}
111-
112-
return nil
113-
}
114-
115-
func (d *Dirs) createFilesInSubdirs(currentDir, dir string) error {
116-
switch currentDir {
117-
case cmdDir:
118-
if err := d.file.GenerateMainFile(dir + fmt.Sprintf("/%s", d.projectName) + ".go"); err != nil {
119-
return err
120-
}
121-
case internalDir:
122-
if err := d.createInternalSubDir(); err != nil {
123-
return err
124-
}
125-
case cfgDir:
126-
if err := d.file.GenerateCfgFile(dir); err != nil {
127-
return err
108+
return fmt.Errorf("failed to create dir, err: %v", err)
128109
}
129110
}
130111

internal/app/file/file.go

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,89 @@ package file
33
import (
44
"fmt"
55
"os"
6-
"path"
76

87
"github.com/blackmarllboro/create-project-struct/pkg/version"
9-
)
108

11-
// TODO need to works on layouts.
9+
"github.com/blackmarllboro/create-project-struct/internal/pkg/temp/interfaces"
10+
)
1211

13-
type File struct{}
12+
type File struct {
13+
temp interfaces.Template
14+
}
1415

15-
func NewFile() *File {
16-
return &File{}
16+
func NewFile(temp interfaces.Template) *File {
17+
return &File{temp: temp}
1718
}
1819

1920
func (fl File) createAndWriteFile(dir, content string) error {
2021
file, err := os.Create(dir)
2122
if err != nil {
2223
return err
2324
}
25+
2426
defer func() {
2527
err = file.Close()
2628
}()
29+
2730
if err != nil {
2831
return err
2932
}
3033

3134
if _, err := file.WriteString(content); err != nil {
32-
return err
35+
return fmt.Errorf("failed to write file, err: %v", err)
3336
}
3437

3538
return nil
3639
}
3740

38-
func (fl File) GenerateMainFile(dir string) error {
39-
const content = `
40-
package main
41-
42-
import "fmt"
43-
44-
func main() {
45-
fmt.Println("Hello!")
46-
}
47-
`
48-
49-
if err := fl.createAndWriteFile(dir, content); err != nil {
50-
return err
41+
func (fl File) getFileContent(fileName, projectName, goVersion string) string {
42+
switch fileName {
43+
case fmt.Sprintf("%s.go", projectName):
44+
return fl.temp.GetMain()
45+
case "golangci.yml":
46+
return fl.temp.GetLintConfig()
47+
case "Makefile":
48+
return fl.temp.GetMakefile(projectName)
49+
case "go.mod":
50+
return fl.temp.GetGoMod(projectName, goVersion)
51+
default:
52+
return ""
5153
}
52-
53-
return nil
5454
}
5555

56-
func (fl File) GenerateCfgFile(dir string) error {
57-
if err := fl.createAndWriteFile(dir+"/config.yaml", ""); err != nil {
58-
return err
59-
}
60-
61-
return nil
62-
}
63-
64-
func (fl File) generateGoModFile(dir string, isCurrentDir bool) error {
56+
func (fl File) GenerateFiles(isCurrentDir bool, projectName string) error {
6557
goVersion, err := version.GoVersion()
6658
if err != nil {
67-
return err
59+
return fmt.Errorf("failed to get go version, err: %v", err)
6860
}
6961

70-
projectName := path.Base(dir)
71-
72-
content := fmt.Sprintf("module %s\n\n%s", projectName, goVersion)
73-
74-
var creatingFile string
75-
if isCurrentDir {
76-
creatingFile = "go.mod"
77-
} else {
78-
creatingFile = projectName + "/go.mod"
62+
mainGoFile := fmt.Sprintf("%s.go", projectName)
63+
fileNames := []string{
64+
mainGoFile,
65+
"golangci.yml",
66+
"Makefile",
67+
"go.mod",
68+
"config.yml",
7969
}
8070

81-
if err := fl.createAndWriteFile(creatingFile, content); err != nil {
82-
return err
83-
}
84-
85-
return nil
86-
}
87-
88-
func (fl File) generateMakefile(projectName string, isCurrentDir bool) error {
71+
dirPrefix := "./"
8972
if !isCurrentDir {
90-
projectName = path.Base(projectName)
91-
}
92-
93-
content := fmt.Sprintf(
94-
"PROJECT_NAME = %s\n"+
95-
"PROJECT_PATH = cmd/$(PROJECT_NAME).go\n\n"+
96-
".PHONY:run\nrun:\n\tgo run $(PROJECT_PATH)\n\n"+
97-
".PHONY:build\nbuild:\n\tgo build -o bin/$(PROGRAM_NAME) $(PROJECT_PATH)\n\n"+
98-
".PHONY:test\ntest:\n\tgo test ./...\n\n"+
99-
".PHONY:lint\nlint:\n\tgolangci-lint run",
100-
projectName,
101-
)
102-
103-
var creatingFile string
104-
if isCurrentDir {
105-
creatingFile = "Makefile"
106-
} else {
107-
creatingFile = projectName + "/Makefile"
108-
}
109-
110-
if err := fl.createAndWriteFile(creatingFile, content); err != nil {
111-
return err
112-
}
113-
114-
return nil
115-
}
116-
117-
func (fl File) GenerateFilesInMainDir(projectName string, isCurrentDir bool) error {
118-
currentDir, err := os.Getwd()
119-
if err != nil {
120-
return err
73+
dirPrefix = fmt.Sprintf("%s/", projectName)
12174
}
12275

123-
if !isCurrentDir {
124-
currentDir += fmt.Sprintf("/%s", projectName)
125-
}
76+
for _, fileName := range fileNames {
77+
filePath := dirPrefix + fileName
12678

127-
if err := fl.generateGoModFile(currentDir, isCurrentDir); err != nil {
128-
return err
129-
}
79+
switch fileName {
80+
case mainGoFile:
81+
filePath = fmt.Sprintf("%s/cmd/%s", dirPrefix, fileName)
82+
case "config.yml":
83+
filePath = fmt.Sprintf("%s/config/%s", dirPrefix, fileName)
84+
}
13085

131-
if err := fl.generateMakefile(currentDir, isCurrentDir); err != nil {
132-
return err
86+
if err := fl.createAndWriteFile(filePath, fl.getFileContent(fileName, projectName, goVersion)); err != nil {
87+
return fmt.Errorf("failed to write file data, err: %v", err)
88+
}
13389
}
13490

13591
return nil

internal/app/file/interfaces/file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interfaces
2+
3+
type File interface {
4+
GenerateFiles(isCurrentDir bool, projectName string) error
5+
}

0 commit comments

Comments
 (0)