Skip to content

Commit 2291440

Browse files
Merge branch 'remote'
2 parents a2f1c5e + 076528a commit 2291440

File tree

18 files changed

+268
-236
lines changed

18 files changed

+268
-236
lines changed

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 & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,131 +3,89 @@ package file
33
import (
44
"fmt"
55
"os"
6-
"path"
76

87
"github.com/blackmarllboro/create-project-struct/pkg/version"
8+
9+
"github.com/blackmarllboro/create-project-struct/internal/pkg/temp/interfaces"
910
)
1011

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

13-
func NewFile() *File {
14-
return &File{}
16+
func NewFile(temp interfaces.Template) *File {
17+
return &File{temp: temp}
1518
}
1619

1720
func (fl File) createAndWriteFile(dir, content string) error {
1821
file, err := os.Create(dir)
1922
if err != nil {
2023
return err
2124
}
25+
2226
defer func() {
2327
err = file.Close()
2428
}()
29+
2530
if err != nil {
2631
return err
2732
}
2833

2934
if _, err := file.WriteString(content); err != nil {
30-
return err
31-
}
32-
33-
return nil
34-
}
35-
36-
func (fl File) GenerateMainFile(dir string) error {
37-
const content = `
38-
package main
39-
40-
import "fmt"
41-
42-
func main() {
43-
fmt.Println("Hello!")
44-
}
45-
`
46-
47-
if err := fl.createAndWriteFile(dir, content); err != nil {
48-
return err
35+
return fmt.Errorf("failed to write file, err: %v", err)
4936
}
5037

5138
return nil
5239
}
5340

54-
func (fl File) GenerateCfgFile(dir string) error {
55-
if err := fl.createAndWriteFile(dir+"/config.yaml", ""); err != nil {
56-
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 ""
5753
}
58-
59-
return nil
6054
}
6155

62-
func (fl File) generateGoModFile(dir string, isCurrentDir bool) error {
56+
func (fl File) GenerateFiles(isCurrentDir bool, projectName string) error {
6357
goVersion, err := version.GoVersion()
6458
if err != nil {
65-
return err
66-
}
67-
68-
projectName := path.Base(dir)
69-
70-
content := fmt.Sprintf("module %s\n\n%s", projectName, goVersion)
71-
72-
var creatingFile string
73-
if isCurrentDir {
74-
creatingFile = "go.mod"
75-
} else {
76-
creatingFile = projectName + "/go.mod"
59+
return fmt.Errorf("failed to get go version, err: %v", err)
7760
}
7861

79-
if err := fl.createAndWriteFile(creatingFile, content); err != nil {
80-
return err
62+
mainGoFile := fmt.Sprintf("%s.go", projectName)
63+
fileNames := []string{
64+
mainGoFile,
65+
"golangci.yml",
66+
"Makefile",
67+
"go.mod",
68+
"config.yml",
8169
}
8270

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

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

125-
if err := fl.generateGoModFile(currentDir, isCurrentDir); err != nil {
126-
return err
127-
}
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+
}
12885

129-
if err := fl.generateMakefile(currentDir, isCurrentDir); err != nil {
130-
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+
}
13189
}
13290

13391
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+
}

internal/pkg/args/args.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ package args
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67
)
78

8-
type ProjectName struct{}
9+
type Args struct{}
10+
11+
func NewArgs() *Args {
12+
return &Args{}
13+
}
914

1015
// GetProjectName function for getting the project name. The name is specified
1116
// via a command line argument. Set the argument "." to create a project in the
1217
// current directory. Boolean value returns true if the application is created in
1318
// the current directory.
14-
func (p ProjectName) GetProjectName() (string, bool, error) {
19+
func (p Args) GetProjectName() (string, bool, error) {
1520
if len(os.Args) < 2 {
1621
return "", false, errors.New("the project name has not been transferred")
1722
}
@@ -21,7 +26,7 @@ func (p ProjectName) GetProjectName() (string, bool, error) {
2126
if projectName == "." {
2227
pwd, err := os.Getwd()
2328
if err != nil {
24-
return "", false, err
29+
return "", false, fmt.Errorf("failed to get the current directory, err: %s", err)
2530
}
2631

2732
return pwd, true, nil

0 commit comments

Comments
 (0)