From 0ea6cd556ccba4fcb4197542e95ac3186ba10066 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:30:37 +0100 Subject: [PATCH 01/10] chore(deps): bump github/codeql-action from 2 to 3 (#11) --- .github/workflows/codeql-analysis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 496bb3a..2929dee 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -26,9 +26,9 @@ jobs: uses: actions/checkout@v4 - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} - name: CodeQL Analysis - uses: github/codeql-action/analyze@v2 \ No newline at end of file + uses: github/codeql-action/analyze@v3 \ No newline at end of file From 00a1ed912bd0b3afe89db4ea962b61777f96067f Mon Sep 17 00:00:00 2001 From: martin <74269598+martabal@users.noreply.github.com> Date: Tue, 19 Dec 2023 01:55:45 +0100 Subject: [PATCH 02/10] fix: reconnect automatically after disconnect (#13) --- go.mod | 2 +- go.sum | 4 ++-- src/immich/data.go | 44 +++++++++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 606331b..77f3dc6 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,6 @@ require ( github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.15.0 // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go.sum b/go.sum index 02595d1..7f85153 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= diff --git a/src/immich/data.go b/src/immich/data.go index 61c5bdc..571563f 100644 --- a/src/immich/data.go +++ b/src/immich/data.go @@ -18,6 +18,10 @@ import ( var wg sync.WaitGroup +var ( + mutex sync.Mutex +) + var unmarshalError = "Can not unmarshal JSON" func Allrequests(r *prometheus.Registry) { @@ -52,8 +56,7 @@ func Analyze(r *prometheus.Registry) { res3, err3 := (<-serverinfo)() - if err != nil && err2 != nil && err3 != nil { - } else { + if err == nil && err2 == nil && err3 == nil { prom.SendBackMessagePreference(res3, res2, res1, r) } } @@ -62,38 +65,36 @@ func GetAllUsers(c chan func() (*models.StructAllUsers, error)) { defer wg.Done() resp, err := Apirequest("/api/user?isAll=true", "GET") if err == nil { - if models.GetPromptError() == true { - models.SetPromptError(false) - } + body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { result := new(models.StructAllUsers) - if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer + if err := json.Unmarshal(body, &result); err != nil { log.Error(unmarshalError) } c <- (func() (*models.StructAllUsers, error) { return result, nil }) + return } } + c <- (func() (*models.StructAllUsers, error) { return new(models.StructAllUsers), err }) } func ServerVersion(r *prometheus.Registry) { defer wg.Done() resp, err := Apirequest("/api/server-info/version", "GET") if err == nil { - if models.GetPromptError() == true { - models.SetPromptError(false) - } + body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { var result models.StructServerVersion - if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer + if err := json.Unmarshal(body, &result); err != nil { log.Error(unmarshalError) } @@ -107,22 +108,20 @@ func ServerInfo(c chan func() (*models.StructServerInfo, error)) { resp, err := Apirequest("/api/server-info/statistics", "GET") if err == nil { - if models.GetPromptError() == true { - models.SetPromptError(false) - } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { result := new(models.StructServerInfo) - if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer + if err := json.Unmarshal(body, &result); err != nil { log.Println(unmarshalError) } c <- (func() (*models.StructServerInfo, error) { return result, nil }) - + return } } + c <- (func() (*models.StructServerInfo, error) { return new(models.StructServerInfo), err }) } func GetAllJobsStatus(c chan func() (*models.StructAllJobsStatus, error)) { @@ -130,22 +129,20 @@ func GetAllJobsStatus(c chan func() (*models.StructAllJobsStatus, error)) { resp, err := Apirequest("/api/jobs", "GET") if err == nil { - if models.GetPromptError() == true { - models.SetPromptError(false) - } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { result := new(models.StructAllJobsStatus) - if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer + if err := json.Unmarshal(body, &result); err != nil { log.Println(unmarshalError) } c <- (func() (*models.StructAllJobsStatus, error) { return result, nil }) - + return } } + c <- (func() (*models.StructAllJobsStatus, error) { return new(models.StructAllJobsStatus), err }) } func Apirequest(uri string, method string) (*http.Response, error) { @@ -160,19 +157,22 @@ func Apirequest(uri string, method string) (*http.Response, error) { resp, err := client.Do(req) if err != nil { err := fmt.Errorf("Can't connect to server") + mutex.Lock() if models.GetPromptError() == false { log.Error(err.Error()) models.SetPromptError(true) } - + mutex.Unlock() return resp, err } switch resp.StatusCode { case http.StatusOK: + mutex.Lock() if models.GetPromptError() { models.SetPromptError(false) } + mutex.Unlock() return resp, nil case http.StatusNotFound: err := fmt.Errorf("%d", resp.StatusCode) @@ -188,10 +188,12 @@ func Apirequest(uri string, method string) (*http.Response, error) { return resp, err default: err := fmt.Errorf("%d", resp.StatusCode) + mutex.Lock() if !models.GetPromptError() { models.SetPromptError(true) log.Debug("Error code ", resp.StatusCode) } + mutex.Unlock() return resp, err } From b52e18aca17be0921ae4f6d3b4c9e035b5882d37 Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Tue, 19 Dec 2023 03:14:05 +0100 Subject: [PATCH 03/10] ci: more docker tags --- .github/workflows/docker.yml | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 287f775..63b33a4 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -25,16 +25,6 @@ jobs: ref: "main" fetch-depth: 0 - - name: Release tag - id: releasetag - run: | - if [ -z "${{ inputs.tag }}" ]; then - latest_release_tag=$(curl -s "https://api.github.com/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/releases/latest" | jq -r .tag_name) - echo "release_tag=${latest_release_tag}" >> $GITHUB_ENV - else - echo "release_tag=${{ inputs.tag }}" >> $GITHUB_ENV - fi - - name: Docker meta id: meta uses: docker/metadata-action@v5 @@ -42,9 +32,11 @@ jobs: images: | ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }} tags: | - type=raw,value=latest,enable=true + type=raw,value=latest,enable=${{ github.event_name == 'release' }} type=sha - type=raw,value=${{ steps.releasetag.outputs.release_tag }},enable=true + type=ref,event=branch + type=ref,event=pr + type=raw,value=${{ inputs.tags }},enable=${{ github.event_name == 'workflow_dispatch' }} - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -58,7 +50,6 @@ jobs: with: username: ${{ github.repository_owner }} password: ${{ secrets.DOCKERHUB_TOKEN }} - if: ${{ github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} - name: Login to GitHub Container Registry uses: docker/login-action@v3 @@ -66,13 +57,12 @@ jobs: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GH_TOKEN }} - if: ${{ github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} - name: Build and push uses: docker/build-push-action@v5 with: context: ./ platforms: linux/arm/v7,linux/amd64,linux/arm64 - push: ${{ github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} + push: ${{ !github.event.pull_request.head.repo.fork }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} From 7e3d56c3e06fffa3ad9f8746827e48bc2790afb3 Mon Sep 17 00:00:00 2001 From: martin <74269598+martabal@users.noreply.github.com> Date: Sat, 23 Dec 2023 21:58:57 +0100 Subject: [PATCH 04/10] ci: linter (#14) * ci: add linter * chore: update dependencies * remove package.json * feat: use enums * ci: rename workflow --- .github/workflows/docker.yml | 5 ++-- .github/workflows/test.yml | 9 ++++++- Dockerfile | 1 - go.mod | 2 +- go.sum | 8 ++---- makefile | 14 +++++++++++ package.json | 24 ------------------ src/immich/data.go | 48 +++++++++++++++++++++++++----------- src/init.go | 32 ++++++++---------------- 9 files changed, 71 insertions(+), 72 deletions(-) create mode 100644 makefile delete mode 100644 package.json diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 63b33a4..d4964af 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,4 +1,4 @@ -name: Publish Release +name: docker on: workflow_dispatch: @@ -16,7 +16,7 @@ on: branches: ["main"] jobs: - build_docker_release: + build_and_push: runs-on: ubuntu-latest steps: - name: Checkout @@ -33,7 +33,6 @@ jobs: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }} tags: | type=raw,value=latest,enable=${{ github.event_name == 'release' }} - type=sha type=ref,event=branch type=ref,event=pr type=raw,value=${{ inputs.tags }},enable=${{ github.event_name == 'workflow_dispatch' }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c5b55e..a778c71 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,14 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + - name: Run unit tests run: go test -v ./src/tests + - name: Run formatter - run: test -z $(gofmt -l ./src) \ No newline at end of file + run: test -z $(gofmt -l ./src) + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.55.2 diff --git a/Dockerfile b/Dockerfile index d76989f..098ba2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,6 @@ RUN go build -o /go/bin/immich-exporter ./src FROM alpine:3.18 COPY --from=builder /go/bin/immich-exporter /go/bin/immich-exporter -COPY package.json /go/bin/ WORKDIR /go/bin diff --git a/go.mod b/go.mod index 77f3dc6..8d1135d 100644 --- a/go.mod +++ b/go.mod @@ -16,5 +16,5 @@ require ( github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect golang.org/x/sys v0.15.0 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect ) diff --git a/go.sum b/go.sum index 7f85153..a1671f3 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= @@ -31,10 +29,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/makefile b/makefile new file mode 100644 index 0000000..9109588 --- /dev/null +++ b/makefile @@ -0,0 +1,14 @@ +build: + go build -o ./qbittorrent-exporter.out ./src +dev : + go run ./src +dev-env : + go run ./src -e +format : + go fmt ./src +lint: + docker run --rm -v ./:/app -w /app golangci/golangci-lint:latest golangci-lint run -v +test: + go test -v ./src/tests +update: + go get -u ./src && go mod tidy \ No newline at end of file diff --git a/package.json b/package.json deleted file mode 100644 index 2cb6898..0000000 --- a/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "immich-exporter", - "version": "1.2.0", - "description": "exporter for immich", - "main": "src/main.go", - "scripts": { - "build" : "go build -o ./immich-exporter.out ./src && ./immich-exporter.out", - "build:env" : "go build -o ./immich-exporter.out ./src && ./immich-exporter.out -e", - "dev" : "go run ./src", - "dev:env" : "go run ./src -e", - "test": "go test -v ./src/tests", - "update": "go get -u ./src && go mod tidy" - }, - "keywords": [ - "exporter", - "immich", - "grafana", - "dashboard", - "metrics", - "prometheus" - ], - "author": "martabal", - "license": "MIT" -} diff --git a/src/immich/data.go b/src/immich/data.go index 571563f..910ac11 100644 --- a/src/immich/data.go +++ b/src/immich/data.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "immich-exp/src/models" - "io/ioutil" + "io" "net/http" "sync" @@ -22,6 +22,28 @@ var ( mutex sync.Mutex ) +type Data struct { + URL string + HTTPMethod string +} + +var httpGetUsers = Data{ + URL: "/api/user?isAll=true", + HTTPMethod: http.MethodGet, +} +var httpServerVersion = Data{ + URL: "/api/server-info/version", + HTTPMethod: http.MethodGet, +} +var httpStatistics = Data{ + URL: "/api/server-info/statistics", + HTTPMethod: http.MethodGet, +} +var httpGetJobs = Data{ + URL: "/api/jobs", + HTTPMethod: http.MethodGet, +} + var unmarshalError = "Can not unmarshal JSON" func Allrequests(r *prometheus.Registry) { @@ -63,10 +85,10 @@ func Analyze(r *prometheus.Registry) { func GetAllUsers(c chan func() (*models.StructAllUsers, error)) { defer wg.Done() - resp, err := Apirequest("/api/user?isAll=true", "GET") + resp, err := Apirequest(httpGetUsers.URL, httpGetUsers.HTTPMethod) if err == nil { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { @@ -85,10 +107,10 @@ func GetAllUsers(c chan func() (*models.StructAllUsers, error)) { func ServerVersion(r *prometheus.Registry) { defer wg.Done() - resp, err := Apirequest("/api/server-info/version", "GET") + resp, err := Apirequest(httpServerVersion.URL, httpServerVersion.HTTPMethod) if err == nil { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { @@ -105,10 +127,10 @@ func ServerVersion(r *prometheus.Registry) { func ServerInfo(c chan func() (*models.StructServerInfo, error)) { defer wg.Done() - resp, err := Apirequest("/api/server-info/statistics", "GET") + resp, err := Apirequest(httpStatistics.URL, httpStatistics.HTTPMethod) if err == nil { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { @@ -126,10 +148,10 @@ func ServerInfo(c chan func() (*models.StructServerInfo, error)) { func GetAllJobsStatus(c chan func() (*models.StructAllJobsStatus, error)) { defer wg.Done() - resp, err := Apirequest("/api/jobs", "GET") + resp, err := Apirequest(httpGetJobs.URL, httpGetJobs.HTTPMethod) if err == nil { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } else { @@ -158,7 +180,7 @@ func Apirequest(uri string, method string) (*http.Response, error) { if err != nil { err := fmt.Errorf("Can't connect to server") mutex.Lock() - if models.GetPromptError() == false { + if !models.GetPromptError() { log.Error(err.Error()) models.SetPromptError(true) } @@ -175,17 +197,15 @@ func Apirequest(uri string, method string) (*http.Response, error) { mutex.Unlock() return resp, nil case http.StatusNotFound: - err := fmt.Errorf("%d", resp.StatusCode) log.Fatal("Error code ", resp.StatusCode, " for ", models.Getbaseurl()+uri) - return resp, err + return resp, fmt.Errorf("%d", resp.StatusCode) case http.StatusUnauthorized, http.StatusForbidden: - err := fmt.Errorf("%d", resp.StatusCode) log.Fatal("Api key unauthorized") - return resp, err + return resp, fmt.Errorf("%d", resp.StatusCode) default: err := fmt.Errorf("%d", resp.StatusCode) mutex.Lock() diff --git a/src/init.go b/src/init.go index eb9a2d4..07b3605 100644 --- a/src/init.go +++ b/src/init.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "flag" "fmt" immich "immich-exp/src/immich" @@ -20,16 +19,24 @@ import ( ) const DEFAULTPORT = 8090 +const AUTHOR = "martabal" +const VERSION = "1.2.0" +const PROJECT_NAME = "immich-exporter" func main() { loadenv() - projectinfo() + fmt.Printf("%s (version %s)\n", PROJECT_NAME, VERSION) + fmt.Println("Author: ", AUTHOR) + fmt.Println("Using log level: ", log.GetLevel()) log.Info("Immich URL: ", models.Getbaseurl()) log.Info("Started") http.HandleFunc("/metrics", metrics) addr := ":" + strconv.Itoa(models.GetPort()) log.Info("Listening on port ", models.GetPort()) - http.ListenAndServe(addr, nil) + err := http.ListenAndServe(addr, nil) + if err != nil { + log.Fatalln(err) + } } func metrics(w http.ResponseWriter, r *http.Request) { @@ -40,25 +47,6 @@ func metrics(w http.ResponseWriter, r *http.Request) { h.ServeHTTP(w, r) } -func projectinfo() { - fileContent, err := os.ReadFile("./package.json") - if err != nil { - log.Fatal(err) - return - } - - var res map[string]interface{} - err = json.Unmarshal(fileContent, &res) - if err != nil { - log.Fatal(err) - return - } - - fmt.Print(res["name"], " (version ", res["version"], ")\n") - fmt.Print("Author: ", res["author"], "\n") - fmt.Print("Using log level: ", log.GetLevel(), "\n") -} - func loadenv() { var envfile bool flag.BoolVar(&envfile, "e", false, "Use .env file") From 907ba3a708095d9a13818141ebc55dc0cb530dc9 Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Sat, 23 Dec 2023 21:59:44 +0100 Subject: [PATCH 05/10] chore: pump version --- src/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/init.go b/src/init.go index 07b3605..3bd7be0 100644 --- a/src/init.go +++ b/src/init.go @@ -20,7 +20,7 @@ import ( const DEFAULTPORT = 8090 const AUTHOR = "martabal" -const VERSION = "1.2.0" +const VERSION = "1.3.1" const PROJECT_NAME = "immich-exporter" func main() { From d6a4b3cc6cffaeef16912bc23df84df60bdd57ee Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Sat, 23 Dec 2023 22:28:19 +0100 Subject: [PATCH 06/10] fix: docker tag --- .github/workflows/docker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d4964af..748c869 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -35,6 +35,7 @@ jobs: type=raw,value=latest,enable=${{ github.event_name == 'release' }} type=ref,event=branch type=ref,event=pr + type=ref,event=tag type=raw,value=${{ inputs.tags }},enable=${{ github.event_name == 'workflow_dispatch' }} - name: Set up QEMU From c59a3d23d6922f865f22de8d1a03a0d10b1b7ef8 Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Mon, 1 Jan 2024 01:43:35 +0100 Subject: [PATCH 07/10] chore: bump dependencies --- Dockerfile | 4 ++-- makefile => Makefile | 8 +++++++- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) rename makefile => Makefile (87%) diff --git a/Dockerfile b/Dockerfile index 098ba2a..8738b7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21-alpine3.18 AS builder +FROM golang:1.21-alpine3.19 AS builder WORKDIR /app @@ -7,7 +7,7 @@ COPY . . RUN go build -o /go/bin/immich-exporter ./src -FROM alpine:3.18 +FROM alpine:3.19 COPY --from=builder /go/bin/immich-exporter /go/bin/immich-exporter diff --git a/makefile b/Makefile similarity index 87% rename from makefile rename to Makefile index 9109588..ecb6526 100644 --- a/makefile +++ b/Makefile @@ -1,14 +1,20 @@ build: go build -o ./qbittorrent-exporter.out ./src + dev : go run ./src + dev-env : go run ./src -e + format : go fmt ./src + lint: docker run --rm -v ./:/app -w /app golangci/golangci-lint:latest golangci-lint run -v + test: go test -v ./src/tests + update: - go get -u ./src && go mod tidy \ No newline at end of file + go get -u ./src && go mod tidy diff --git a/go.mod b/go.mod index 8d1135d..187b18a 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/joho/godotenv v1.5.1 - github.com/prometheus/client_golang v1.17.0 + github.com/prometheus/client_golang v1.18.0 github.com/sirupsen/logrus v1.9.3 ) diff --git a/go.sum b/go.sum index a1671f3..3a8fc5b 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvls github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= -github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= From bd5c47c7fce468b7de88c38f63be843bca90b53e Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 10 Jan 2024 20:48:37 +0800 Subject: [PATCH 08/10] chore: switch from Docker Hub examples to Github Container Registry (#15) The docker hub images haven't been updated for several months, I ran into errors following the examples in the readme. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 546a513..6a07a7b 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ docker run --name=immich-exporter \ -e IMMICH_BASE_URL=http://192.168.1.10:8080 \ -e IMMICH_API_KEY= \ -p 8090:8090 \ - martabal/immich-exporter + ghcr.io/martabal/immich-exporter ``` ### Docker-compose @@ -31,7 +31,7 @@ docker run --name=immich-exporter \ version: "2.1" services: immich: - image: martabal/immich-exporter:latest + image: ghcr.io/martabal/immich-exporter:latest container_name: immich-exporter environment: - IMMICH_BASE_URL=http://192.168.1.10:8080 From 560a8c093c717d934b51ef650e3ef001d3935eb9 Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Wed, 10 Jan 2024 13:49:12 +0100 Subject: [PATCH 09/10] ci: publish to docker hub --- .github/workflows/docker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 748c869..d7d121e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,6 +30,7 @@ jobs: uses: docker/metadata-action@v5 with: images: | + ${{ github.repository_owner }}/${{ github.event.repository.name }} ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }} tags: | type=raw,value=latest,enable=${{ github.event_name == 'release' }} From e176ed4db9165887b69801d095ea3730ccf080e8 Mon Sep 17 00:00:00 2001 From: martin <74269598+martabal@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:14:54 +0100 Subject: [PATCH 10/10] refactor: project structure (#16) --- .github/workflows/build.yml | 7 +++---- .github/workflows/codeql-analysis.yml | 27 ++++++++++++++++----------- .github/workflows/docker.yml | 4 ++-- .github/workflows/test.yml | 7 ++++--- Dockerfile | 14 ++++++++++---- Makefile | 14 +++++++------- go.mod => src/go.mod | 5 ++--- go.sum => src/go.sum | 10 ++++------ src/immich/data.go | 4 ++-- src/init.go | 17 ++++++++++------- src/prometheus/prometheus.go | 2 +- src/tests/prometheus_test.go | 4 ++-- 12 files changed, 63 insertions(+), 52 deletions(-) rename go.mod => src/go.mod (72%) rename go.sum => src/go.sum (83%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 68696f3..054ea34 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,11 +2,10 @@ name: Build on: workflow_dispatch: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] jobs: - build: name: Run build runs-on: ubuntu-latest @@ -14,4 +13,4 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - name: Build - run: go build -o ./immich.out ./src \ No newline at end of file + run: make build diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 2929dee..7483ddd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -3,9 +3,9 @@ name: CodeQL on: workflow_dispatch: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] jobs: analyze: @@ -19,16 +19,21 @@ jobs: strategy: fail-fast: false matrix: - language: [ 'go' ] + language: ["go"] steps: - - name: Checkout repo - uses: actions/checkout@v4 + - name: Checkout repo + uses: actions/checkout@v4 - - name: Initialize CodeQL - uses: github/codeql-action/init@v3 - with: - languages: ${{ matrix.language }} + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} - - name: CodeQL Analysis - uses: github/codeql-action/analyze@v3 \ No newline at end of file + - name: Autobuild + uses: github/codeql-action/autobuild@v3 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d7d121e..4666511 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,4 +1,4 @@ -name: docker +name: Docker Build on: workflow_dispatch: @@ -30,7 +30,6 @@ jobs: uses: docker/metadata-action@v5 with: images: | - ${{ github.repository_owner }}/${{ github.event.repository.name }} ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }} tags: | type=raw,value=latest,enable=${{ github.event_name == 'release' }} @@ -67,3 +66,4 @@ jobs: push: ${{ !github.event.pull_request.head.repo.fork }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: BUILD_VERSION=${{ github.event.release.name }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a778c71..cdb0625 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Test +name: Tests on: workflow_dispatch: push: @@ -15,12 +15,13 @@ jobs: uses: actions/checkout@v4 - name: Run unit tests - run: go test -v ./src/tests + run: make test - name: Run formatter - run: test -z $(gofmt -l ./src) + run: cd src && test -z $(gofmt -l .) - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: version: v1.55.2 + working-directory: src diff --git a/Dockerfile b/Dockerfile index 8738b7e..9caaef3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,17 @@ FROM golang:1.21-alpine3.19 AS builder +ARG BUILD_VERSION + WORKDIR /app -COPY . . - -RUN go build -o /go/bin/immich-exporter ./src +COPY src src +RUN cd src && \ + if [ -n "${BUILD_VERSION}" ]; then \ + go build -o /go/bin/immich-exporter -ldflags="-X 'main.Version=${BUILD_VERSION}'" . ; \ + else \ + go build -o /go/bin/immich-exporter . ; \ + fi FROM alpine:3.19 @@ -13,4 +19,4 @@ COPY --from=builder /go/bin/immich-exporter /go/bin/immich-exporter WORKDIR /go/bin -CMD ["/go/bin/immich-exporter"] \ No newline at end of file +CMD ["/go/bin/immich-exporter"] diff --git a/Makefile b/Makefile index ecb6526..dee6f07 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,20 @@ build: - go build -o ./qbittorrent-exporter.out ./src + cd src && go build -o ../immich-exporter.out . dev : - go run ./src + cd src && go run . dev-env : - go run ./src -e + cd src && go run . -e format : - go fmt ./src + cd src && test -z $(gofmt -l .) lint: - docker run --rm -v ./:/app -w /app golangci/golangci-lint:latest golangci-lint run -v + docker run --rm -v ./src:/app -w /app golangci/golangci-lint:latest golangci-lint run -v test: - go test -v ./src/tests + cd src && go test -v ./tests update: - go get -u ./src && go mod tidy + cd src && go get -u . && go mod tidy \ No newline at end of file diff --git a/go.mod b/src/go.mod similarity index 72% rename from go.mod rename to src/go.mod index 187b18a..04ae9ba 100644 --- a/go.mod +++ b/src/go.mod @@ -11,10 +11,9 @@ require ( require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.45.0 // indirect + github.com/prometheus/common v0.46.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.16.0 // indirect google.golang.org/protobuf v1.32.0 // indirect ) diff --git a/go.sum b/src/go.sum similarity index 83% rename from go.sum rename to src/go.sum index 3a8fc5b..b6b2a86 100644 --- a/go.sum +++ b/src/go.sum @@ -9,16 +9,14 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= -github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= -github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y= +github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -27,8 +25,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/src/immich/data.go b/src/immich/data.go index 910ac11..396019c 100644 --- a/src/immich/data.go +++ b/src/immich/data.go @@ -3,13 +3,13 @@ package immich import ( "encoding/json" "fmt" - "immich-exp/src/models" + "immich-exp/models" "io" "net/http" "sync" - prom "immich-exp/src/prometheus" + prom "immich-exp/prometheus" log "github.com/sirupsen/logrus" diff --git a/src/init.go b/src/init.go index 3bd7be0..6f43cd4 100644 --- a/src/init.go +++ b/src/init.go @@ -3,8 +3,8 @@ package main import ( "flag" "fmt" - immich "immich-exp/src/immich" - "immich-exp/src/models" + immich "immich-exp/immich" + "immich-exp/models" "net/http" "strconv" @@ -19,14 +19,17 @@ import ( ) const DEFAULTPORT = 8090 -const AUTHOR = "martabal" -const VERSION = "1.3.1" -const PROJECT_NAME = "immich-exporter" + +var ( + Version = "dev" + Author = "martabal" + ProjectName = "immich-exporter" +) func main() { loadenv() - fmt.Printf("%s (version %s)\n", PROJECT_NAME, VERSION) - fmt.Println("Author: ", AUTHOR) + fmt.Printf("%s (version %s)\n", ProjectName, Version) + fmt.Println("Author: ", Author) fmt.Println("Using log level: ", log.GetLevel()) log.Info("Immich URL: ", models.Getbaseurl()) log.Info("Started") diff --git a/src/prometheus/prometheus.go b/src/prometheus/prometheus.go index 2b62a56..99e20f2 100644 --- a/src/prometheus/prometheus.go +++ b/src/prometheus/prometheus.go @@ -1,7 +1,7 @@ package prom import ( - "immich-exp/src/models" + "immich-exp/models" "strconv" "strings" diff --git a/src/tests/prometheus_test.go b/src/tests/prometheus_test.go index a7b260c..59afd3a 100644 --- a/src/tests/prometheus_test.go +++ b/src/tests/prometheus_test.go @@ -1,8 +1,8 @@ package prom import ( - "immich-exp/src/models" - prom "immich-exp/src/prometheus" + "immich-exp/models" + prom "immich-exp/prometheus" "reflect" "testing" )