Skip to content

Commit e9d9a38

Browse files
added another layout, also wrote a class for accessing the folder with layouts
1 parent 8a55521 commit e9d9a38

File tree

8 files changed

+134
-49
lines changed

8 files changed

+134
-49
lines changed

cmd/create-project-struct.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
package main
22

3-
import (
4-
"log"
5-
6-
"github.com/blackmarllboro/create-project-struct/internal/pkg/temp"
7-
)
8-
93
func main() {
10-
if _, err := temp.GetTemplateByAlias(temp.MAIN); err != nil {
11-
log.Println(err)
12-
}
134
//logger := log.NewLogger(l.New())
145
//
156
//projName := args.ProjectName{}

internal/app/file/file.go

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

11+
// TODO need to works on layouts.
12+
1113
type File struct{}
1214

1315
func NewFile() *File {

internal/pkg/args/tests/args_test.go renamed to internal/pkg/args/args_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package tests
1+
package args
22

33
import (
44
"os"
55
"testing"
6-
7-
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
86
)
97

108
func TestGetProjectName(t *testing.T) {
11-
projName := args.ProjectName{}
9+
projName := ProjectName{}
1210

1311
t.Log("no project name provided")
1412
{

internal/pkg/args/tests/mocks/GetProjectName.go renamed to internal/pkg/args/mocks/GetProjectName.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ type IsCurrentDirIsTrue struct{}
1212

1313
func (i IsCurrentDirIsTrue) GetProjectName() (string, bool, error) {
1414
return "", true, nil
15-
}
15+
}

internal/pkg/git/tests/git_test.go renamed to internal/pkg/git/git_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package tests
1+
package git
22

33
import (
44
"fmt"
55
"os"
66
"testing"
77

8+
"github.com/blackmarllboro/create-project-struct/internal/pkg/args/mocks"
9+
810
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
9-
"github.com/blackmarllboro/create-project-struct/internal/pkg/args/tests/mocks"
10-
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
1111
)
1212

1313
func TestCreateLocalGitRepository(t *testing.T) {
@@ -44,7 +44,7 @@ func TestCreateLocalGitRepository(t *testing.T) {
4444

4545
t.Log("create repository in dir: ", dataT.name)
4646
{
47-
if err := git.CreateLocalGitRepository(dataT.mock); err != nil {
47+
if err := CreateLocalGitRepository(dataT.mock); err != nil {
4848
t.Fatalf("Expected no error, but got: %s", err)
4949
}
5050
}

internal/pkg/temp/temp.go

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
11
package temp
22

33
import (
4-
"errors"
5-
"io/fs"
6-
"log"
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path"
78
"path/filepath"
89
)
910

1011
const (
1112
MAIN = "main.go"
12-
MAKE = "make"
13-
LINT = "lint"
13+
MAKE = "Makefile"
14+
LINT = ".golangci.yml"
1415
)
1516

16-
// TODO we need to improve performance and fix minor errors.
17+
type Template struct{}
1718

18-
func GetTemplateByAlias(alias string) (string, error) {
19+
func NewTemplate() *Template {
20+
return &Template{}
21+
}
22+
23+
func (t Template) GetTemplateByAlias(fileName string) (string, error) {
1924
var pathToTemp string
2025

21-
err := filepath.WalkDir("../../../", func(path string, d fs.DirEntry, err error) error {
22-
if err != nil {
23-
return err
24-
}
26+
tempDir, err := t.getTemplateDir()
27+
if err != nil {
28+
return "", fmt.Errorf("fail to get template dir, err: %v", err)
29+
}
30+
31+
filesInDirectory, err := ioutil.ReadDir(path.Base(tempDir))
32+
if err != nil {
33+
return "", fmt.Errorf("could not read dir, maybe there are no files threr, err: %v", err)
34+
}
2535

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-
}
36+
for _, file := range filesInDirectory {
37+
if file.Name() == fileName {
38+
pathToTemp = filepath.Join(tempDir, file.Name())
4239
}
4340

44-
return nil
45-
})
41+
break
42+
}
43+
44+
if pathToTemp == "" {
45+
return "", fmt.Errorf("file with name \"%s\" not found, err: %v", fileName, err)
46+
}
47+
48+
return pathToTemp, nil
49+
}
4650

51+
func (Template) getTemplateDir() (string, error) {
52+
exePath, err := os.Executable()
4753
if err != nil {
48-
return "", err
54+
return "", fmt.Errorf("unable to get path to executable file, err: %v", err)
55+
}
56+
57+
targetDir := filepath.Join(exePath, "template")
58+
59+
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
60+
return "", fmt.Errorf("directory \"%s\" not found, err: %v", targetDir, err)
4961
}
5062

51-
return pathToTemp, err
63+
return targetDir, nil
5264
}

internal/pkg/temp/temp_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package temp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TODO need to write tests.
8+
9+
func TestTemplate_GetTemplateByAlias(t *testing.T) {
10+
type args struct {
11+
fileName string
12+
}
13+
14+
tests := []struct {
15+
name string
16+
args args
17+
want string
18+
wantErr bool
19+
}{
20+
{},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t1 *testing.T) {
25+
t := Template{}
26+
got, err := t.GetTemplateByAlias(tt.args.fileName)
27+
if (err != nil) != tt.wantErr {
28+
t1.Errorf("GetTemplateByAlias() error = %v, wantErr %v", err, tt.wantErr)
29+
return
30+
}
31+
if got != tt.want {
32+
t1.Errorf("GetTemplateByAlias() got = %v, want %v", got, tt.want)
33+
}
34+
})
35+
}
36+
}
37+
38+
func TestTemplate_getTemplateDir(t *testing.T) {
39+
tests := []struct {
40+
name string
41+
want string
42+
wantErr bool
43+
}{
44+
{
45+
name: "test",
46+
want: "test",
47+
wantErr: false,
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
te := Template{}
53+
got, err := te.getTemplateDir()
54+
if (err != nil) != tt.wantErr {
55+
t.Errorf("getTemplateDir() error = %v, wantErr %v", err, tt.wantErr)
56+
return
57+
}
58+
if got != tt.want {
59+
t.Errorf("getTemplateDir() got = %v, want %v", got, tt.want)
60+
}
61+
})
62+
}
63+
}

template/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PROJECT_NAME = $(project_name)
2+
PROJECT_PATH = cmd/$(PROJECT_NAME).go
3+
4+
.PHONY:run
5+
run:
6+
go run $(PROJECT_PATH)
7+
8+
.PHONY:build
9+
build:
10+
go build -o bin/$(PROGRAM_NAME) $(PROJECT_PATH)
11+
12+
.PHONY:test
13+
test:
14+
go test ./...
15+
16+
.PHONY:lint
17+
lint:
18+
golangci-lint run
19+

0 commit comments

Comments
 (0)