Skip to content

Commit 8a55521

Browse files
author
blackmarllbor0
committed
I reviewed the method of creating files with content and decided to use templates instead of direct input
1 parent 86c24af commit 8a55521

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed

cmd/create-project-struct.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package main
22

33
import (
4-
"github.com/blackmarllboro/create-project-struct/internal/app/dir"
5-
"github.com/blackmarllboro/create-project-struct/internal/app/file"
6-
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
7-
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
8-
"github.com/blackmarllboro/create-project-struct/internal/pkg/log"
4+
"log"
95

10-
l "github.com/charmbracelet/log"
6+
"github.com/blackmarllboro/create-project-struct/internal/pkg/temp"
117
)
128

139
func main() {
14-
logger := log.NewLogger(l.New())
15-
16-
projName := args.ProjectName{}
17-
18-
project := dir.NewDirs(file.NewFile(), projName)
19-
if err := project.CreateProject(); err != nil {
20-
logger.Error(err)
10+
if _, err := temp.GetTemplateByAlias(temp.MAIN); err != nil {
11+
log.Println(err)
2112
}
22-
23-
if err := git.CreateLocalGitRepository(projName); err != nil {
24-
logger.Error(err)
25-
}
26-
27-
logger.Info("the project dir has been successfully created")
13+
//logger := log.NewLogger(l.New())
14+
//
15+
//projName := args.ProjectName{}
16+
//
17+
//project := dir.NewDirs(file.NewFile(), projName)
18+
//if err := project.CreateProject(); err != nil {
19+
// logger.Error(err)
20+
//}
21+
//
22+
//if err := git.CreateLocalGitRepository(projName); err != nil {
23+
// logger.Error(err)
24+
//}
25+
//
26+
//logger.Info("the project dir has been successfully created")
2827
}

internal/app/file/file.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ func NewFile() *File {
1414
return &File{}
1515
}
1616

17+
func (fl File) readTemplate(templateFileName string) ([]byte, error) {
18+
temp, err := os.ReadFile(templateFileName)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
return temp, nil
24+
}
25+
1726
func (fl File) createAndWriteFile(dir, content string) error {
1827
file, err := os.Create(dir)
1928
if err != nil {
@@ -26,7 +35,12 @@ func (fl File) createAndWriteFile(dir, content string) error {
2635
return err
2736
}
2837

29-
if _, err := file.WriteString(content); err != nil {
38+
temp, err := fl.readTemplate(content)
39+
if err != nil {
40+
return err
41+
}
42+
43+
if _, err := file.Write(temp); err != nil {
3044
return err
3145
}
3246

internal/pkg/temp/temp.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package temp
2+
3+
import (
4+
"errors"
5+
"io/fs"
6+
"log"
7+
"path/filepath"
8+
)
9+
10+
const (
11+
MAIN = "main.go"
12+
MAKE = "make"
13+
LINT = "lint"
14+
)
15+
16+
// TODO we need to improve performance and fix minor errors.
17+
18+
func GetTemplateByAlias(alias string) (string, error) {
19+
var pathToTemp string
20+
21+
err := filepath.WalkDir("../../../", func(path string, d fs.DirEntry, err error) error {
22+
if err != nil {
23+
return err
24+
}
25+
26+
if d.IsDir() && d.Name() == alias {
27+
files, err := filepath.Glob(filepath.Join(path, "*"))
28+
if err != nil {
29+
return err
30+
}
31+
32+
for _, fl := range files {
33+
if filepath.Base(fl) == alias {
34+
log.Println(filepath.Base(fl))
35+
pathToTemp = fl
36+
} else {
37+
return errors.New("template with such alias does not exist")
38+
}
39+
40+
break
41+
}
42+
}
43+
44+
return nil
45+
})
46+
47+
if err != nil {
48+
return "", err
49+
}
50+
51+
return pathToTemp, err
52+
}

template/.golangci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
run:
2+
tests: true
3+
skip-dirs:
4+
- build
5+
6+
output:
7+
format: colored-line-number
8+
print-issued-lines: true
9+
print-linter-name: true
10+
unique-by-line: true
11+
sort-results: true
12+
13+
linters:
14+
disable-all: true # (false)
15+
enable:
16+
- errcheck
17+
- gosimple
18+
- govet
19+
- ineffassign
20+
- typecheck
21+
- unused
22+
- cyclop
23+
- dupl
24+
- dupword
25+
- funlen
26+
- lll
27+
- gochecknoglobals
28+
- goconst
29+
- gocritic
30+
- gocyclo
31+
- godot
32+
- gosec
33+
- nakedret
34+
- nestif
35+
- stylecheck
36+
- varnamelen

template/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("HELLO!")
7+
}

0 commit comments

Comments
 (0)