Skip to content

Commit 411e589

Browse files
committed
Eliminate the reviewer's requirements
1 parent 21ee950 commit 411e589

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ install: build
1010
lint:
1111
golangci-lint run
1212

13-
# Запуск линтера
13+
# Запуск тестов
1414
test:
1515
go test -v ./...

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,36 @@ This is a disk size analysis program that allows you to get information about oc
1212
- golangci-lint
1313

1414
## Build and Setup (for global use)
15-
- git clone https://github.com/alexvitayu/go-project-242.git
16-
17-
- cd go-project-242
18-
19-
- make build
20-
15+
```ch
16+
git clone https://github.com/alexvitayu/go-project-242.git
17+
```
18+
```ch
19+
cd go-project-242
20+
```
21+
```ch
22+
make build
23+
```
2124
## Run linter
22-
- make lint
23-
25+
```ch
26+
make lint
27+
```
2428
## Run tests
25-
- make test
26-
29+
```ch
30+
make test
31+
```
2732
## Install
2833
### for make install make sure that you have GOBIN set up in your PATH
2934
### to discover this just run 'echo $PATH'
30-
- make install
31-
35+
```ch
36+
make install
37+
```
3238
## Help
33-
- hexlet-path-size -h --help
34-
39+
```ch
40+
hexlet-path-size -h --help
41+
```
3542
## Run the application from the terminal
36-
- hexlet-path-size
37-
43+
```ch
44+
hexlet-path-size
45+
```
3846
## Follow the link to watch how to install and use this application:
3947
https://asciinema.org/a/uNqXjcIaNnYQVZGDtlmRriGOx

cmd/hexlet-path-size/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
r := cmd.Bool("recursive")
4444
size, err := code.GetPathSize(path, r, h, a)
4545
if err != nil {
46-
log.Println(err.Error())
46+
log.Fatalf("Program failed to get path for %s: %v", path, err)
4747
}
4848
str := fmt.Sprintf("%v\t%s", size, path)
4949
fmt.Println(str)

path_size.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ package code
33
import (
44
"errors"
55
"fmt"
6-
"math"
76
"os"
87
"path/filepath"
98
"strconv"
109
"strings"
1110
)
1211

12+
const (
13+
KB = 1024
14+
MB = KB * 1024
15+
GB = MB * 1024
16+
TB = GB * 1024
17+
PB = TB * 1024
18+
EB = PB * 1024
19+
)
20+
1321
func GetPathSize(path string, recursive, human, all bool) (string, error) {
1422
if path == "" {
1523
return "", errors.New("не указан путь")
@@ -19,21 +27,16 @@ func GetPathSize(path string, recursive, human, all bool) (string, error) {
1927
return "", fmt.Errorf("%w", err)
2028
}
2129
return fmt.Sprintf("%v", FormatSize(sum, human)), nil
22-
2330
}
2431

2532
func GetSize(path string, recursive, human, all bool) (int64, error) {
26-
info, err := os.Stat(path)
33+
info, err := os.Lstat(path)
2734
if err != nil {
2835
return 0, errors.New("не удалось прочитать путь к файлу или директории")
2936
}
3037
// If path is a file but not a directory
3138
if !info.IsDir() {
32-
i, err := os.Lstat(path)
33-
if err != nil {
34-
return 0, errors.New("не удалось получить информацию о файле")
35-
}
36-
return i.Size(), nil
39+
return info.Size(), nil
3740
}
3841

3942
// If path is a directory not a file
@@ -72,17 +75,17 @@ func FormatSize(size int64, human bool) string {
7275
case len(str) <= 3:
7376
return fmt.Sprintf("%vB", size)
7477
case len(str) > 3 && len(str) <= 6:
75-
return fmt.Sprintf("%.1fKB", float64(size)/1000)
78+
return fmt.Sprintf("%.1fKB", float64(size)/KB)
7679
case len(str) >= 7 && len(str) < 10:
77-
return fmt.Sprintf("%.1fMB", float64(size)/math.Pow(10, 6))
80+
return fmt.Sprintf("%.1fMB", float64(size)/MB)
7881
case len(str) >= 10 && len(str) < 13:
79-
return fmt.Sprintf("%.1fGB", float64(size)/math.Pow(10, 9))
82+
return fmt.Sprintf("%.1fGB", float64(size)/GB)
8083
case len(str) >= 13 && len(str) < 16:
81-
return fmt.Sprintf("%.1fTB", float64(size)/math.Pow(10, 12))
84+
return fmt.Sprintf("%.1fTB", float64(size)/TB)
8285
case len(str) >= 16 && len(str) < 19:
83-
return fmt.Sprintf("%.1fPB", float64(size)/math.Pow(10, 15))
86+
return fmt.Sprintf("%.1fPB", float64(size)/PB)
8487
default:
85-
return fmt.Sprintf("%.1fEB", float64(size)/math.Pow(10, 18))
88+
return fmt.Sprintf("%.1fEB", float64(size)/EB)
8689
}
8790
}
8891
return fmt.Sprintf("%vB", size)

0 commit comments

Comments
 (0)