-
Notifications
You must be signed in to change notification settings - Fork 7
/
implemantation.go
74 lines (61 loc) · 1.49 KB
/
implemantation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package manager
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/denizgursoy/gotouch/internal/executor"
"github.com/denizgursoy/gotouch/internal/logger"
"github.com/denizgursoy/gotouch/internal/store"
)
type (
fManager struct {
Executor executor.Executor `validate:"required"`
Store store.Store `validate:"required"`
Logger logger.Logger `validate:"required"`
}
)
func init() {
manager = newFileManager()
}
func newFileManager() Manager {
return &fManager{
Executor: executor.GetInstance(),
Store: store.GetInstance(),
Logger: logger.NewLogger(),
}
}
func (f *fManager) CreateDirectoryIfNotExist(directoryName string) error {
return os.Mkdir(directoryName, os.ModePerm)
}
func (f *fManager) GetExtractLocation() string {
wd, err := os.Getwd()
if err != nil {
log.Fatal("could not get working directory", err)
}
return wd
}
func (f *fManager) CreateFile(reader io.ReadCloser, path string) error {
fullPath := filepath.Join(f.Store.GetValue(store.ProjectFullPath), path)
directoryOfFile := filepath.Dir(fullPath)
err := os.MkdirAll(directoryOfFile, os.ModePerm)
if err != nil {
return err
}
f.Logger.LogInfo(fmt.Sprintf("Creating file -> %s", fullPath))
create, createError := os.Create(fullPath)
if createError != nil {
return createError
}
defer create.Close()
all, readError := io.ReadAll(reader)
if readError != nil {
return readError
}
_, writeError := create.Write(all)
if writeError != nil {
return writeError
}
return nil
}