Skip to content

Commit 3c708ac

Browse files
Merge pull request #8 from ssh-connection-manager/141-create-logger-pkg
141 create logger pkg
2 parents d9615c8 + 92129c8 commit 3c708ac

File tree

10 files changed

+212
-38
lines changed

10 files changed

+212
-38
lines changed

.github/workflows/go-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ jobs:
1414
- name: Install dependencies
1515
run: go get ./...
1616
- name: Test with the Go CLI
17-
run: go test ./... -test.v
17+
run: go test ./... -test.v -tags module

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func existOrCreateConfig(fl file.File) {
3535
func setConfigVariable() {
3636
viper.Set("NameFileConnects", NameFileConnects)
3737
viper.Set("NameFileCryptKey", NameFileCryptKey)
38+
viper.Set("NameFileLogger", NameFileLogger)
3839
viper.Set("FullPathConfig", getHomeDir())
3940
viper.Set("Separator", Separator)
4041
viper.Set("Space", Space)

config/enums.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
const (
1313
NameFileConnects string = "connect-ssh.json"
1414
NameFileCryptKey string = "key.txt"
15+
NameFileLogger string = "log.txt"
1516
)
1617

1718
const (

inits/inits.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package inits
22

33
import (
44
"github.com/spf13/viper"
5+
"github.com/ssh-connection-manager/kernel/v2/internal/logger"
56

67
"github.com/ssh-connection-manager/kernel/v2/config"
78
"github.com/ssh-connection-manager/kernel/v2/pkg/crypt"
@@ -38,8 +39,21 @@ func generateCryptKey() {
3839
}
3940
}
4041

42+
func generateLogFile() {
43+
pathConf := viper.GetString("FullPathConfig")
44+
fileNameKey := viper.GetString("NameFileLogger")
45+
46+
fileLogger := file.File{Path: pathConf, Name: fileNameKey}
47+
48+
err := logger.GenerateFile(fileLogger)
49+
if err != nil {
50+
output.GetOutError("err generate logger")
51+
}
52+
}
53+
4154
func SetDependencies() {
4255
generateConfigFile()
4356
createFileConnects()
4457
generateCryptKey()
58+
generateLogFile()
4559
}

internal/logger/file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package logger
2+
3+
import "github.com/ssh-connection-manager/kernel/v2/pkg/file"
4+
5+
var fl file.File
6+
7+
func SetFile(file file.File) {
8+
fl = file
9+
}
10+
11+
func GetFile() file.File {
12+
return fl
13+
}

internal/logger/logger.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package logger
2+
3+
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/ssh-connection-manager/kernel/v2/pkg/file"
9+
"github.com/ssh-connection-manager/kernel/v2/pkg/output"
10+
)
11+
12+
func GenerateFile(fl file.File) error {
13+
SetFile(fl)
14+
logFile := GetFile()
15+
16+
if !logFile.IsExistFile() {
17+
err := logFile.CreateFile()
18+
if err != nil {
19+
return err
20+
}
21+
}
22+
23+
return nil
24+
}
25+
26+
// TODO переписать логику на пакет file
27+
func getOpenLogFile(fl file.File) (*os.File, error) {
28+
path := filepath.Join(fl.Path, fl.Name)
29+
30+
logFile, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return logFile, nil
35+
}
36+
37+
// Info Danger TODO написать одну логику а функции только различаются строкой
38+
func Info(message string) {
39+
logFile := GetFile()
40+
openLogFile, err := getOpenLogFile(logFile)
41+
if err != nil {
42+
output.GetOutError("dont open log file")
43+
}
44+
45+
infoLog := log.New(openLogFile, "[info] ", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
46+
infoLog.Println(message)
47+
}
48+
49+
func Danger(message string) {
50+
logFile := GetFile()
51+
openLogFile, err := getOpenLogFile(logFile)
52+
if err != nil {
53+
output.GetOutError("dont open log file")
54+
}
55+
56+
errorLog := log.New(openLogFile, "[error] ", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
57+
errorLog.Println(message)
58+
}

test/helpers.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ import (
66
"os/user"
77
)
88

9-
func RandomString(count int) string {
10-
letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
9+
const DefaultChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
10+
const DefaultCountChars = 5
1111

12-
randomStr := make([]rune, count)
13-
for i := range randomStr {
14-
randomStr[i] = letterRunes[rand.Intn(len(letterRunes))]
15-
}
16-
17-
return string(randomStr)
12+
func RandomString() string {
13+
return generateRandomString(DefaultCountChars)
1814
}
1915

2016
func GetDirForTests() string {
@@ -26,3 +22,14 @@ func GetDirForTests() string {
2622

2723
return usr.HomeDir + "/test/"
2824
}
25+
26+
func generateRandomString(count int) string {
27+
letterRunes := []rune(DefaultChars)
28+
29+
randomStr := make([]rune, count)
30+
for i := range randomStr {
31+
randomStr[i] = letterRunes[rand.Intn(len(letterRunes))]
32+
}
33+
34+
return string(randomStr)
35+
}

test/module/file_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
package module
44

55
import (
6-
fl "github.com/ssh-connection-manager/kernel/v2/pkg/file"
76
"testing"
7+
8+
fl "github.com/ssh-connection-manager/kernel/v2/pkg/file"
9+
10+
"github.com/ssh-connection-manager/kernel/v2/test"
811
)
912

1013
func TestCreateFile(t *testing.T) {
1114
testDir := test.GetDirForTests()
1215

13-
randomStr := test.RandomString(5)
16+
randomStr := test.RandomString()
1417

1518
file := fl.File{Name: randomStr, Path: testDir}
1619
err := file.CreateFile()
@@ -26,7 +29,7 @@ func TestCreateFile(t *testing.T) {
2629
func TestWriteToFile(t *testing.T) {
2730
testDir := test.GetDirForTests()
2831

29-
randomStr := test.RandomString(5) + ".txt"
32+
randomStr := test.RandomString() + ".txt"
3033

3134
file := fl.File{Name: randomStr, Path: testDir}
3235

@@ -39,7 +42,7 @@ func TestWriteToFile(t *testing.T) {
3942
t.Fatal("File dont created")
4043
}
4144

42-
randomText := test.RandomString(100)
45+
randomText := test.RandomString()
4346
err = file.WriteFile([]byte(randomText))
4447
if err != nil {
4548
t.Fatal("Error write to file")
@@ -57,13 +60,13 @@ func TestWriteToFile(t *testing.T) {
5760

5861
func TestReadFile(t *testing.T) {
5962
files := [7]fl.File{
60-
{Name: test.RandomString(5) + ".json", Path: test.GetDirForTests()},
61-
{Name: test.RandomString(5) + ".txt", Path: test.GetDirForTests()},
62-
{Name: test.RandomString(5) + ".PNG", Path: test.GetDirForTests()},
63-
{Name: test.RandomString(5) + ".PDF", Path: test.GetDirForTests()},
64-
{Name: test.RandomString(5) + ".PDF", Path: test.GetDirForTests()},
65-
{Name: test.RandomString(5) + ".DOC", Path: test.GetDirForTests()},
66-
{Name: test.RandomString(5), Path: test.GetDirForTests()},
63+
{Name: test.RandomString() + ".json", Path: test.GetDirForTests()},
64+
{Name: test.RandomString() + ".txt", Path: test.GetDirForTests()},
65+
{Name: test.RandomString() + ".PNG", Path: test.GetDirForTests()},
66+
{Name: test.RandomString() + ".PDF", Path: test.GetDirForTests()},
67+
{Name: test.RandomString() + ".PDF", Path: test.GetDirForTests()},
68+
{Name: test.RandomString() + ".DOC", Path: test.GetDirForTests()},
69+
{Name: test.RandomString(), Path: test.GetDirForTests()},
6770
}
6871

6972
for _, file := range files {
@@ -76,7 +79,7 @@ func TestReadFile(t *testing.T) {
7679
t.Fatal("File dont created")
7780
}
7881

79-
randomText := test.RandomString(100)
82+
randomText := test.RandomString()
8083

8184
err = file.WriteFile([]byte(randomText))
8285
if err != nil {
@@ -98,8 +101,8 @@ func TestReadFile(t *testing.T) {
98101
func TestIsExistFile(t *testing.T) {
99102
testDir := test.GetDirForTests()
100103

101-
randomStr := test.RandomString(5)
102-
randomStr2 := test.RandomString(5)
104+
randomStr := test.RandomString()
105+
randomStr2 := test.RandomString()
103106

104107
file := fl.File{Name: randomStr, Path: testDir}
105108
fileWithDontExistName := fl.File{Name: randomStr2, Path: testDir}

test/module/json_test.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
package module
44

55
import (
6-
"github.com/ssh-connection-manager/kernel/v2/inits"
6+
"testing"
7+
78
fl "github.com/ssh-connection-manager/kernel/v2/pkg/file"
9+
10+
"github.com/ssh-connection-manager/kernel/v2/inits"
811
"github.com/ssh-connection-manager/kernel/v2/pkg/json"
912
"github.com/ssh-connection-manager/kernel/v2/test"
10-
"testing"
1113
)
1214

1315
func TestWriteToJsonFile(t *testing.T) {
1416
inits.SetDependencies()
1517

1618
path := test.GetDirForTests()
17-
fileName := test.RandomString(5) + ".json"
19+
fileName := test.RandomString() + ".json"
1820

1921
file := fl.File{Name: fileName, Path: path}
2022

@@ -24,12 +26,12 @@ func TestWriteToJsonFile(t *testing.T) {
2426
}
2527

2628
connect := json.Connect{
27-
Alias: test.RandomString(5),
28-
Login: test.RandomString(5),
29-
Address: test.RandomString(5),
30-
Password: test.RandomString(5),
31-
CreatedAt: test.RandomString(5),
32-
UpdatedAt: test.RandomString(5),
29+
Alias: test.RandomString(),
30+
Login: test.RandomString(),
31+
Address: test.RandomString(),
32+
Password: test.RandomString(),
33+
CreatedAt: test.RandomString(),
34+
UpdatedAt: test.RandomString(),
3335
}
3436

3537
connection := json.Connections{Connects: make([]json.Connect, 1)}
@@ -45,12 +47,12 @@ func TestWriteToJsonFile(t *testing.T) {
4547
}
4648

4749
connectTwin := json.Connect{
48-
Alias: test.RandomString(5),
49-
Login: test.RandomString(5),
50-
Address: test.RandomString(5),
51-
Password: test.RandomString(5),
52-
CreatedAt: test.RandomString(5),
53-
UpdatedAt: test.RandomString(5),
50+
Alias: test.RandomString(),
51+
Login: test.RandomString(),
52+
Address: test.RandomString(),
53+
Password: test.RandomString(),
54+
CreatedAt: test.RandomString(),
55+
UpdatedAt: test.RandomString(),
5456
}
5557

5658
err = connection.WriteConnectToJson(connectTwin)

test/module/logger_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//go:build module
2+
3+
package module
4+
5+
import (
6+
"testing"
7+
8+
fl "github.com/ssh-connection-manager/kernel/v2/pkg/file"
9+
10+
"github.com/ssh-connection-manager/kernel/v2/internal/logger"
11+
"github.com/ssh-connection-manager/kernel/v2/test"
12+
)
13+
14+
func TestGenerateLogFile(t *testing.T) {
15+
filePath := test.GetDirForTests()
16+
fileName := test.RandomString() + ".log"
17+
18+
logFile := fl.File{
19+
Name: fileName,
20+
Path: filePath,
21+
}
22+
23+
err := logger.GenerateFile(logFile)
24+
if err != nil {
25+
t.Fatal("Error creating file for logs")
26+
}
27+
28+
if !logFile.IsExistFile() {
29+
t.Fatal("Log file dont created")
30+
}
31+
}
32+
33+
func TestInfo(t *testing.T) {
34+
filePath := test.GetDirForTests()
35+
fileName := test.RandomString() + ".log"
36+
37+
logFile := fl.File{
38+
Name: fileName,
39+
Path: filePath,
40+
}
41+
42+
err := logger.GenerateFile(logFile)
43+
if err != nil {
44+
t.Fatal("Error creating file for logs")
45+
}
46+
47+
if !logFile.IsExistFile() {
48+
t.Fatal("Log file dont created")
49+
}
50+
51+
textMessage := test.RandomString()
52+
logger.Info(textMessage)
53+
}
54+
55+
func TestDanger(t *testing.T) {
56+
filePath := test.GetDirForTests()
57+
fileName := test.RandomString() + ".log"
58+
59+
logFile := fl.File{
60+
Name: fileName,
61+
Path: filePath,
62+
}
63+
64+
err := logger.GenerateFile(logFile)
65+
if err != nil {
66+
t.Fatal("Error creating file for logs")
67+
}
68+
69+
if !logFile.IsExistFile() {
70+
t.Fatal("Log file dont created")
71+
}
72+
73+
textMessage := test.RandomString()
74+
logger.Danger(textMessage)
75+
}

0 commit comments

Comments
 (0)