Skip to content

Commit 158e43e

Browse files
authored
Merge pull request #4 from Format-C-eft/dev
refactoring
2 parents 0a0150f + 8f06060 commit 158e43e

File tree

14 files changed

+335
-227
lines changed

14 files changed

+335
-227
lines changed

.golangci.yaml

Lines changed: 0 additions & 37 deletions
This file was deleted.

.golangci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# More info on config here: https://golangci-lint.run/usage/configuration/#config-file
2+
run:
3+
concurrency: 8
4+
timeout: 10m
5+
issues-exit-code: 1
6+
tests: true
7+
8+
output:
9+
formats:
10+
- format: colored-line-number
11+
print-issued-lines: true
12+
print-linter-name: true
13+
14+
linters-settings:
15+
govet:
16+
enable:
17+
- shadow
18+
dupl:
19+
threshold: 100
20+
goconst:
21+
min-len: 2
22+
min-occurrences: 2
23+
24+
linters:
25+
disable-all: true
26+
enable:
27+
- errcheck
28+
- goconst
29+
- goimports
30+
- gosec
31+
- gosimple
32+
- govet
33+
- ineffassign
34+
- revive
35+
- staticcheck
36+
- unused
37+
38+
issues:
39+
exclude-dirs:
40+
- bin
41+
- vendor
42+
- var
43+
- tmp
44+
- .cache
45+
exclude-use-default: false
46+
exclude:
47+
- G104
48+
- G115
49+
- G204
50+
- should have a package comment
51+
- should have comment or be unexported
52+
- comment on exported const
53+
- should have comment \(or a comment on this block\)
54+
- don't use an underscore in package name
55+
- package-comments

Makefile

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
1+
include build.mk
2+
13
export GO111MODULE=on
4+
# export GOSUMDB=off
5+
6+
BUILD_ENV_PARAMS:=CGO_ENABLED=0
27

3-
ENV_PARAMS ?= CGO_ENABLED=0
4-
LOCAL_BIN := $(CURDIR)/bin
5-
LINT_BIN := $(LOCAL_BIN)/golangci-lint
8+
space := $(subst ,, )
9+
CURDIR_ESCAPE:=$(subst $(space),\ ,$(CURDIR))
610

7-
.PHONY: deps
8-
deps:
9-
$(info Download dependencies...)
10-
go mod download
11+
LOCAL_BIN:=$(CURDIR_ESCAPE)/bin
12+
LINT_BIN:=$(LOCAL_BIN)/golangci-lint
13+
LINT_VERSION:=1.60.3
1114

15+
###### TEST ######
1216
.PHONY: test
1317
test:
14-
$(info Running test...)
15-
go test ./...
16-
17-
.PHONY: .lint
18-
.lint:
19-
$(info Running lint...)
20-
$(LINT_BIN) run --new-from-rev=origin/master --config=.golangci.yaml ./...
21-
22-
.PHONY: .lint-full
23-
.lint-full:
24-
$(info Running lint-full...)
25-
$(LINT_BIN) run --config=.golangci.yaml ./...
26-
27-
.PHONY: lint
28-
lint: install-lint .lint
29-
30-
.PHONY: lint-full
31-
lint-full: install-lint .lint-full
18+
go test ./... -count=1 -timeout=60s -v -short
19+
###### TEST ######
3220

33-
LINT_TAG ?= 1.55.2
34-
install-lint: export GOBIN := $(LOCAL_BIN)
21+
###### LINT ######
22+
.PHONY: install-lint
3523
install-lint:
36-
$(info Installing golangci-lint v$(LINT_TAG))
37-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_TAG)
38-
go mod tidy
39-
40-
.PHONY: build
41-
build: build-linux build-darwin
42-
43-
.PHONY: build-linux
44-
build-linux:
45-
go mod download && CGO_ENABLED=0 \
46-
GOOS=linux GOARCH=${GOARCH} go build -o ${LOCAL_BIN}/git-update-linux-${GOARCH} ./cmd/main.go;
47-
48-
.PHONY: build-darwin
49-
build-darwin:
50-
go mod download && CGO_ENABLED=0 \
51-
GOOS=darwin GOARCH=${GOARCH} go build -o ${LOCAL_BIN}/git-update-darwin-${GOARCH} ./cmd/main.go;
24+
ifeq ($(wildcard $(LINT_BIN)),)
25+
$(info Installing golangci-lint v$(LINT_VERSION))
26+
GOBIN=$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION)
27+
# Устанавливаем текущий путь для исполняемого файла линтера.
28+
else
29+
$(info Golangci-lint is already installed to $(LINT_VERSION))
30+
endif
31+
32+
33+
PHONY: lint
34+
lint: install-lint
35+
$(info Running lint against changed files...)
36+
$(LINT_BIN) run \
37+
--new-from-rev=origin/master \
38+
--config=.golangci.yml \
39+
./...
40+
41+
PHONY: lint-full
42+
lint-full: install-lint
43+
$(info Running lint against all project files...)
44+
$(LINT_BIN) run \
45+
--config=.golangci.yml \
46+
./...
47+
###### LINT ######

build.mk

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
GO_VERSION_SHORT:=$(shell echo `go version` | sed -E 's/.* go(.*) .*/\1/g')
2+
ifneq ("1.23","$(shell printf "$(GO_VERSION_SHORT)\n1.23" | sort -V | head -1)")
3+
$(error NEED GO VERSION >= 1.23. Found: $(GO_VERSION_SHORT))
4+
endif
5+
6+
##################### PROJECT RELATED VARIABLES #####################
7+
ifndef GIT_BRANCH
8+
GIT_BRANCH:=$(shell git rev-parse --abbrev-ref HEAD)
9+
endif
10+
11+
#GIT_BRANCH:=$(shell git branch 2> /dev/null | grep '*' | cut -f2 -d' ')
12+
ifndef GIT_HASH
13+
GIT_HASH:=$(shell git log --format="%h" -n 1 2> /dev/null)
14+
endif
15+
16+
ifndef BUILD_TS
17+
BUILD_TS:=$(shell date +%FT%T%z)
18+
endif
19+
20+
LDFLAGS = -X 'github.com/Format-C-eft/git-update/internal/config.branch=$(GIT_BRANCH)'\
21+
-X 'github.com/Format-C-eft/git-update/internal/config.commitHash=$(GIT_HASH)'\
22+
-X 'github.com/Format-C-eft/git-update/internal/config.timeBuild=$(BUILD_TS)'
23+
24+
25+
.PHONY: .build
26+
.build:
27+
$(info Building...)
28+
$(BUILD_ENV_PARAMS) go build -ldflags "$(LDFLAGS)" -o ./bin/git-update ./cmd/git-update
29+
30+
.PHONY: build
31+
build: .bin-deps .build
32+
33+
.PHONY: .bin-deps
34+
.bin-deps:
35+
$(info Installing binary dependencies...)

cmd/git-update/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Format-C-eft/git-update/internal/cmd"
7+
"github.com/Format-C-eft/git-update/internal/config"
8+
)
9+
10+
func main() {
11+
if config.FlagVersion {
12+
showVersion()
13+
return
14+
}
15+
16+
if errRun := cmd.Run(); errRun != nil {
17+
fmt.Println(errRun.Error())
18+
return
19+
}
20+
}
21+
22+
func showVersion() {
23+
fmt.Printf("Name - '%s'\n", config.GetVersion().Name)
24+
fmt.Printf("Branch - '%s'\n", config.GetVersion().Branch)
25+
fmt.Printf("Commit hash - '%s'\n", config.GetVersion().CommitHash)
26+
fmt.Printf("Time build - '%s'\n", config.GetVersion().TimeBuild)
27+
}

cmd/main.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
module github.com/Format-C-eft/git-update
22

3-
go 1.22
4-
5-
require github.com/spf13/cobra v1.8.0
6-
7-
require (
8-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
9-
github.com/spf13/pflag v1.0.5 // indirect
10-
)
3+
go 1.23

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2-
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3-
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5-
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
6-
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
7-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8-
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/cmd/dto.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,17 @@ import (
44
"fmt"
55
"strings"
66
"time"
7+
8+
"github.com/Format-C-eft/git-update/internal/config"
79
)
810

911
const (
10-
LayoutDateFormat = "15:04:05.999"
12+
layoutDateFormat = "15:04:05.999"
1113
gitStatusOk = "nothing to commit, working tree clean"
1214
)
1315

14-
var (
15-
flagDir string
16-
flagAll bool
17-
flagCheckout bool
18-
flagFetch bool
19-
flagPull bool
20-
flagResetHard bool
21-
flagDefaultBranch string
22-
23-
flagExecuteTimeout *time.Duration
24-
)
25-
2616
type (
27-
resultLogChan struct {
17+
resultLog struct {
2818
dir string
2919
logs []logs
3020
}
@@ -35,19 +25,26 @@ type (
3525
}
3626
)
3727

38-
func (r *resultLogChan) AddLog(message string) {
28+
func (r *resultLog) AddLog(message string, verboseMessage string) {
3929
r.logs = append(r.logs, logs{
4030
Time: time.Now(),
4131
messages: message,
4232
})
33+
34+
if verboseMessage != "" && config.FlagVerbose {
35+
r.logs = append(r.logs, logs{
36+
Time: time.Now(),
37+
messages: "verbose: " + verboseMessage,
38+
})
39+
}
4340
}
4441

45-
func (r *resultLogChan) String() string {
42+
func (r *resultLog) String() string {
4643
result := make([]string, 0, len(r.logs))
4744
for _, l := range r.logs {
4845
result = append(result, fmt.Sprintf(
4946
"%s: %s: %s",
50-
l.Time.Format(LayoutDateFormat),
47+
l.Time.Format(layoutDateFormat),
5148
r.dir,
5249
l.messages,
5350
),

internal/cmd/init.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)