/
file.go
100 lines (88 loc) · 2.24 KB
/
file.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package goutils
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
/*
ListFolderFiles o antigo nome era: ListarArquivosDaPasta
*/
func ListFolderFiles(path string) []os.FileInfo {
files, err := ioutil.ReadDir(path)
if err != nil {
CreateFileDay(Message{File: "ListFolderFiles(path)", Error: fmt.Sprint("a pasta '", path, "' não existe!")})
}
return files
}
/*
ReadingFiles o antigo nome era: LeituraDosArquivos
*/
func ReadingFiles(path string, fileName string) string {
file, err := os.Open(fmt.Sprint(path, fileName))
if err != nil {
CreateFileDay(Message{File: "ReadingFiles(path, fileName)", Error: fmt.Sprint("O arquivo '", fileName, "' não está mais na pasta!")})
}
defer file.Close()
b, err := ioutil.ReadAll(file)
return string(b)
}
/*
RemoveFilesOfPath o antigo nome era: RemoveFile
*/
func RemoveFilesOfPath(path string) {
if os.Remove(path) != nil {
CreateFileDay(Message{Error: "Problema ao remover do diretório local"})
} else {
CreateFileDay(Message{Error: "Removido do local"})
}
}
/*
ReturnsTheRows o antigo nome era: RetornaAsLinhas
*/
func ReturnsTheRows(text string) []string {
var linhas []string
scanner := bufio.NewScanner(strings.NewReader(text))
for scanner.Scan() {
linhas = append(linhas, scanner.Text())
}
return linhas
}
/*
CreateDirectory o antigo nome era: CriaDiretorio
*/
func CreateDirectory(directory string) bool {
err := os.MkdirAll(directory, os.ModePerm)
if IsError(err) {
return false
}
return true
}
/*
CreateArchiveNodirectory o antigo nome era: CreateArchiveNodirectory
*/
func CreateArchiveNodirectory(nomeDoArquivo string, conteudo string) error {
return ioutil.WriteFile(nomeDoArquivo, []byte(conteudo), 0777)
}
/*
IsError o antigo nome era: IsError
*/
func IsError(err error) bool {
if err != nil {
CreateFileDay(Message{Error: err.Error()})
}
return err != nil
}
/*
Backup
*/
func Backup(path, name, texto string) {
path_novo := fmt.Sprint(path, "BACKUP/")
CreateDirectory(path_novo)
path_nome_arquivo_novo := fmt.Sprint(path, "BACKUP/", name)
CreateArchiveNodirectory(path_nome_arquivo_novo, texto)
path_nome_arquivo_antigo := fmt.Sprint(path, name)
os.Remove(path_nome_arquivo_antigo)
CreateFileDay(Message{Info: fmt.Sprint("Concluída a Leitura do Arquivo: ", name)})
}