From 87af242a6eded78f1d1a9ab337aad56efe563242 Mon Sep 17 00:00:00 2001 From: Dweb Fan Date: Tue, 30 Apr 2024 22:57:15 -0700 Subject: [PATCH] enable github workflow Signed-off-by: Dweb Fan --- .github/workflows/main.yml | 33 +++++++++++++++++++++ .golangci.yml | 58 +++++++++++++++++++++++++++++++++++++ Makefile | 26 +++++++++++++++++ README.md | 5 ++++ cmd/lomob/iso.go | 3 +- cmd/lomob/list-local.go | 1 - cmd/lomob/main.go | 4 +-- cmd/lomob/upload-iso.go | 2 +- common/datasize/datasize.go | 8 ++--- common/util.go | 4 +-- common/util_test.go | 2 +- dockerfiles/dev-image | 19 ++++++++++++ dockerfiles/dev-run | 9 ++++++ dockerfiles/entrypoint.sh | 8 +++++ 14 files changed, 169 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/main.yml create mode 100644 .golangci.yml create mode 100644 Makefile create mode 100644 dockerfiles/dev-image create mode 100644 dockerfiles/dev-run create mode 100755 dockerfiles/entrypoint.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..345c99b --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,33 @@ +name: test-pr +on: + push: + #pull_request: + # types: + # - opened + # - synchronize + # - reopened +jobs: + lint: + runs-on: ubuntu-22.04 + container: + image: lomorage/lomo-backup:1.0 + env: + PATH: /bin:/usr/bin:/usr/local/bin:/usr/local/go/bin + GOFLAGS: "-buildvcs=false" + steps: + - uses: actions/checkout@v4 + - run: make lint + + install: + runs-on: ubuntu-22.04 + container: + image: lomorage/lomo-backup:1.0 + env: + PATH: /bin:/usr/bin:/usr/local/bin:/usr/local/go/bin + GOBIN: /usr/local/bin + GOFLAGS: "-buildvcs=false" + steps: + - uses: actions/checkout@v4 + with: + submodules: 'true' + - run: make install diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..84d6239 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,58 @@ +linters: + disable-all: true + enable: + # default linter + - govet + #- errcheck + - staticcheck + - unused + - gosimple + - ineffassign + # enabled linter + - bodyclose # checks whether HTTP response body is closed successfully + - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. + # rowserrcheck - checks whether Err of rows is checked successfully + # stylecheck - Stylecheck is a replacement for golint + # gosec - Inspects source code for security problems + # interfacer - Linter that suggests narrower interface types + - unconvert # Remove unnecessary type conversions + # dupl - Tool for code clone detection + # goconst - Finds repeated strings that could be replaced by a constant + - gocyclo # Computes and checks the cyclomatic complexity of functions + # gocognit - Computes and checks the cognitive complexity of functions + # asciicheck - Simple linter to check that your code does not contain non-ASCII identifiers + - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification + - goimports # Goimports does everything that gofmt does. Additionally it checks unused imports + # maligned - Tool to detect Go structs that would take less memory if their fields were sorted + # - depguard # Go linter that checks if package imports are in a list of acceptable packages + - misspell # Finds commonly misspelled English words in comments + #- lll # Reports long lines + - unparam # Reports unused function parameters + # dogsled - Checks assignments with too many blank identifiers (e.g. x, , , _, := f()) + # - nakedret # Finds naked returns in functions greater than a specified function length + - prealloc # Finds slice declarations that could potentially be preallocated + # scopelint - Scopelint checks for unpinned variables in go programs + # gocritic - The most opinionated Go source code linter + # gochecknoinits - Checks that no init functions are present in Go code + # gochecknoglobals - Checks that no globals are present in Go code + # godox - Tool for detection of FIXME, TODO and other comment keywords + # funlen - Tool for detection of long functions + - whitespace # Tool for detection of leading and trailing whitespace + # wsl - Whitespace Linter - Forces you to use empty lines! + # goprintffuncname - Checks that printf-like functions are named with f at the end + # gomnd - An analyzer to detect magic numbers. + # goerr113 - Golang linter to check the errors handling expressions + # gomodguard - Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. + # godot # Check if comments end in a period + # testpackage - linter that makes you use a separate _test package + # nestif - Reports deeply nested if statements + # exportloopref - An analyzer that finds exporting pointers for loop variables. + # nolintlint - Reports ill-formed or insufficient nolint directives + - typecheck +linters-settings: + revive: + confidence: 0.7 +issues: + exclude-use-default: false +run: + deadline: 20m diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a040473 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +.PHONY: vendor + +SHELL=/bin/bash # Use bash syntax +GOPATH=/go + +vendor: + go mod vendor + +lint: + golangci-lint run --modules-download-mode vendor -v --max-same-issues 10 + +install: + go install -v ./cmd/... + +dev-container: + docker build --tag "lomorage/lomo-backup:1.0" -f dockerfiles/dev-image . + +dev: + docker build --tag "lomorage/lomo-backup" -f dockerfiles/dev-run . + docker rm -f lomo-backup + docker run \ + --name lomo-backup --hostname lomo-backup \ + --privileged --cap-add=ALL -v /dev:/dev -v /lib/modules:/lib/modules \ + -v "${PWD}:/go/src/github.com/lomorage/lomo-backup" \ + --net host --dns-search local \ + -it "lomorage/lomo-backup" -d bash diff --git a/README.md b/README.md index 0a8aea2..3157908 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +# Lomo-backup - Most cost saving 2-stages cloud backup solution + +[![GitHub Actions](https://github.com/lomorage/lomo-backup/workflows/main/badge.svg)](https://github.com/lomorage/lomo-backup/actions?query=workflow%3Amain) + + # Motivation Photos/videos are very import personal assets and we want to store in our home instead of clouds. We developped lomorage application to self host our own google photo alternative solutions, which has met our main goal. diff --git a/cmd/lomob/iso.go b/cmd/lomob/iso.go index b6bbe65..8a84ac3 100644 --- a/cmd/lomob/iso.go +++ b/cmd/lomob/iso.go @@ -65,9 +65,8 @@ func mkISO(ctx *cli.Context) error { for { if currentSizeNotInISO < isoSize.Bytes() { currSize := datasize.ByteSize(currentSizeNotInISO) - expSize := datasize.ByteSize(isoSize) fmt.Printf("Total size of un-backedup files is %s, less than %s, skip\n", - currSize.HR(), expSize.HR()) + currSize.HR(), isoSize.HR()) return nil } diff --git a/cmd/lomob/list-local.go b/cmd/lomob/list-local.go index 8631d52..5e6edba 100644 --- a/cmd/lomob/list-local.go +++ b/cmd/lomob/list-local.go @@ -136,7 +136,6 @@ func printDirsByTree(dirs map[int]*types.DirInfo) { for _, tree := range scanRootTree { fmt.Println(tree) } - } func printDirsByTable(dirs map[int]*types.DirInfo) { diff --git a/cmd/lomob/main.go b/cmd/lomob/main.go index a25347a..92d3eee 100644 --- a/cmd/lomob/main.go +++ b/cmd/lomob/main.go @@ -43,12 +43,12 @@ func main() { Flags: []cli.Flag{ cli.StringFlag{ Name: "ignore-files, if", - Usage: "List of ignored files, seperated by comman", + Usage: "List of ignored files, separated by comma", Value: ".DS_Store,._.DS_Store,Thumbs.db", }, cli.StringFlag{ Name: "ignore-dirs, in", - Usage: "List of ignored directories, seperated by comman", + Usage: "List of ignored directories, separated by comma", Value: ".idea,.git,.github", }, cli.IntFlag{ diff --git a/cmd/lomob/upload-iso.go b/cmd/lomob/upload-iso.go index 3fa362e..2447970 100644 --- a/cmd/lomob/upload-iso.go +++ b/cmd/lomob/upload-iso.go @@ -462,7 +462,7 @@ func calculatePartHash(ctx *cli.Context) error { } var curr, partLength int64 - var remaining = int64(info.Size()) + var remaining = info.Size() var no = 1 var prs *common.FilePartReadSeeker var h hash.Hash diff --git a/common/datasize/datasize.go b/common/datasize/datasize.go index b00be56..0f8a235 100644 --- a/common/datasize/datasize.go +++ b/common/datasize/datasize.go @@ -71,7 +71,7 @@ func (b ByteSize) EBytes() float64 { func (b ByteSize) String() string { switch { case b == 0: - return fmt.Sprint("0B") + return "0B" case b%EB == 0: return fmt.Sprintf("%dEB", b/EB) case b%PB == 0: @@ -208,15 +208,15 @@ ParseLoop: Overflow: *b = ByteSize(maxUint64) - return &strconv.NumError{fnUnmarshalText, string(t0), strconv.ErrRange} + return &strconv.NumError{Func: fnUnmarshalText, Num: string(t0), Err: strconv.ErrRange} SyntaxError: *b = 0 - return &strconv.NumError{fnUnmarshalText, string(t0), strconv.ErrSyntax} + return &strconv.NumError{Func: fnUnmarshalText, Num: string(t0), Err: strconv.ErrSyntax} BitsError: *b = 0 - return &strconv.NumError{fnUnmarshalText, string(t0), ErrBits} + return &strconv.NumError{Func: fnUnmarshalText, Num: string(t0), Err: ErrBits} } func Parse(t []byte) (ByteSize, error) { diff --git a/common/util.go b/common/util.go index d75a401..a79a713 100644 --- a/common/util.go +++ b/common/util.go @@ -110,7 +110,7 @@ func (prs *FilePartReadSeeker) Seek(offset int64, whence int) (n int64, err erro } else { if prs.current+offset >= prs.end { n, err = prs.f.Seek(prs.end, io.SeekStart) - n -= int64(prs.start) + n -= prs.start prs.current = prs.end return } @@ -225,7 +225,7 @@ func CalculateMultiPartsHash(path string, partSize int) ([][]byte, error) { partsHash := [][]byte{} var curr, partLength int64 - var remaining = int64(info.Size()) + var remaining = info.Size() for curr = 0; remaining != 0; curr += partLength { if remaining < int64(partSize) { partLength = remaining diff --git a/common/util_test.go b/common/util_test.go index 8fa5c37..897e824 100644 --- a/common/util_test.go +++ b/common/util_test.go @@ -243,7 +243,7 @@ func TestFilePartReadSeekerReadSeek(t *testing.T) { prs = NewFilePartReadSeeker(fpart, 1000, 2000) verifyReadSeek(t, expectFile, prs, 500, 1500, 500, io.SeekStart) - verifyReadSeek(t, expectFile, prs, 500, -500, -500, io.SeekCurrent) + verifyReadSeek(t, expectFile, prs, 400, -500, -500, io.SeekCurrent) } func TestConcatAndCalculateBase64Hash(t *testing.T) { diff --git a/dockerfiles/dev-image b/dockerfiles/dev-image new file mode 100644 index 0000000..72f32b1 --- /dev/null +++ b/dockerfiles/dev-image @@ -0,0 +1,19 @@ +FROM ubuntu:22.04 + +ENV DEBIAN_FRONTEND noninteractive +ENV GOPATH=/go + +RUN apt-get -yqq update && apt-get install -yq --no-install-recommends man-db vim build-essential \ + sqlite3 curl apt-transport-https ca-certificates software-properties-common \ + && apt-get install -yq sqlite3 \ + && apt-get clean autoclean && apt-get autoremove -y && rm -rf /var/lib/{apt,dpkg,cache,log}/ + +# install docker +RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg +RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null +RUN apt-get -yqq update && apt-get install -yq docker-ce && apt-get clean autoclean && apt-get autoremove -y && rm -rf /var/lib/{apt,dpkg,cache,log}/ + + +RUN curl -fsSL https://go.dev/dl/go1.21.0.linux-amd64.tar.gz | tar xz -C /usr/local + +RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 diff --git a/dockerfiles/dev-run b/dockerfiles/dev-run new file mode 100644 index 0000000..d64b995 --- /dev/null +++ b/dockerfiles/dev-run @@ -0,0 +1,9 @@ +FROM "lomorage/lomo-backup:1.0" +ENV GOPATH=/go +ENV GOROOT=/usr/local/go +ENV PATH=$PATH:$GOROOT/bin:$GOPATH/bin +WORKDIR /go/src/github.com/lomorage/lomo-backup + +COPY ./dockerfiles/entrypoint.sh /entrypoint.sh +RUN chmod 755 /entrypoint.sh +ENTRYPOINT /entrypoint.sh "$@" diff --git a/dockerfiles/entrypoint.sh b/dockerfiles/entrypoint.sh new file mode 100755 index 0000000..1648ce8 --- /dev/null +++ b/dockerfiles/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +set -x + +echo user: $USER +echo uid: $UID + +$@