diff --git a/.github/workflows/pushimage.yaml b/.github/workflows/pushimage.yaml new file mode 100644 index 0000000..4067ee7 --- /dev/null +++ b/.github/workflows/pushimage.yaml @@ -0,0 +1,28 @@ +name: build-push-image + +on: + push: + tags: + - 'v*' + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + packages: write + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: https://ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + push: true + tags: ghcr.io/tlinden/kleingebaeck:${{ github.ref_name}} diff --git a/.gitignore b/.gitignore index b992fd1..cb30975 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ test kleingebaeck releases t/out +.bak diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..70824cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:1.21-alpine as builder + +RUN apk update +RUN apk upgrade +RUN apk add --no-cache git make + +RUN git --version + +WORKDIR /work + +COPY go.mod . +COPY . . +RUN go mod download +RUN make + +FROM alpine:latest +LABEL maintainer="Thomas von Dein " + +WORKDIR /app +COPY --from=builder /work/kleingebaeck /app/kleingebaeck + +ENV KLEINGEBAECK_OUTDIR /backup +ENV LANG C.UTF-8 +USER 1001:1001 + +ENTRYPOINT ["/app/kleingebaeck"] +CMD ["-h"] diff --git a/README.md b/README.md index 2c7af18..d8a12b6 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,33 @@ installed - `make`. To install after building either copy the binary or execute `sudo make install`. +### Using the docker image + +A pre-built docker image is available, which you can use to test the +app without installing it. You need `docker-compose`. Copy the file +`docker-compose.yaml` to somewhere, cd to that directory and execute: + +```shell +mkdir kleinanzeigen-backup +USER_ID=$(id -u) GROUP_ID=$(id -g) OUTDIR=./kleinanzeigen-backup docker-compose run kleingebaeck -u XXX -v +``` + +`USER_ID` and `GROUP_ID` needs to be specified so that you are the +owner of the created backups. The backup directory `OUTDIR` must exist +prior to the execution, otherwise docker will create it as root, then +kleingebaeck will fail. You may also use a `.env` file in the same +directory containing the variables, such as: + +``` +USER_ID=1000 +GROUP_ID=1000 +OUTDIR=./kleinanzeigen-backup +``` + +You may of course also modify the `docker-compose.yaml` to suit your needs. + +If you want to build the image yourself, use the supplied Dockerfile. + ## Commandline options: ``` @@ -128,6 +155,13 @@ loglevel = verbose outdir = "test" ``` +## Environment Variables + +Kleingebaeck can also be configured using environment variables. Just prefix the config variables with `KLEINGEBAECK_` and put them to upper case. Eg: +```shell +% KLEINGEBAECK_OUTDIR=/backup kleingebaeck -v +``` + ## Usage To setup the tool, you need to lookup your userid on diff --git a/config.go b/config.go index 81d3980..bdb0392 100644 --- a/config.go +++ b/config.go @@ -23,9 +23,11 @@ import ( "os" "path/filepath" "runtime" + "strings" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/providers/confmap" + "github.com/knadh/koanf/providers/env" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/providers/posflag" "github.com/knadh/koanf/v2" @@ -162,7 +164,15 @@ func InitConfig(w io.Writer) (*Config, error) { // else: we ignore the file if it doesn't exists } - // command line overrides config file + // env overrides config file + if err := k.Load(env.Provider("KLEINGEBAECK_", ".", func(s string) string { + return strings.Replace(strings.ToLower( + strings.TrimPrefix(s, "KLEINGEBAECK_")), "_", ".", -1) + }), nil); err != nil { + return nil, errors.New("error loading environment: " + err.Error()) + } + + // command line overrides env if err := k.Load(posflag.Provider(f, ".", k), nil); err != nil { return nil, errors.New("error loading flags: " + err.Error()) } diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8cef53a --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,22 @@ +version: "3.9" +services: + init: + image: alpine:latest + user: "root" + group_add: + - '${GROUP_ID}' + volumes: + - ${OUTDIR}:/backup + command: chown -R ${USER_ID}:${USER_ID} /backup + + kleingebaeck: + container_name: kleingebaeck + user: "${USER_ID}:${USER_ID}" + volumes: + - ${OUTDIR}:/backup + working_dir: /backup + build: . + image: kleingebaeck:latest + depends_on: + init: + condition: service_completed_successfully diff --git a/go.mod b/go.mod index 9560640..efe6d19 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/andybalholm/cascadia v1.0.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect + github.com/knadh/koanf/providers/env v0.1.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect diff --git a/go.sum b/go.sum index 5d13c4b..c92ca20 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6OD github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18= github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPghxRVp7JbIvdvqzU= github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU= +github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg= +github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ= github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c= github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA= github.com/knadh/koanf/providers/posflag v0.1.0 h1:mKJlLrKPcAP7Ootf4pBZWJ6J+4wHYujwipe7Ie3qW6U= diff --git a/kleingebaeck.1 b/kleingebaeck.1 index c763139..2709a6b 100644 --- a/kleingebaeck.1 +++ b/kleingebaeck.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "KLEINGEBAECK 1" -.TH KLEINGEBAECK 1 "2024-01-16" "1" "User Commands" +.TH KLEINGEBAECK 1 "2024-01-17" "1" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -219,6 +219,22 @@ directory. Then just execute \f(CW\*(C`kleingebaeck\*(C'\fR. .PP You can use the \fB\-v\fR option to get verbose output or \fB\-d\fR to enable debugging. +.SH "ENVIRONMENT VARIABLES" +.IX Header "ENVIRONMENT VARIABLES" +The following environment variables are considered: +.PP +.Vb 7 +\& KLEINGEBAECK_USER +\& KLEINGEBAECK_DEBUG +\& KLEINGEBAECK_VERBOSE +\& KLEINGEBAECK_OUTDIR +\& KLEINGEBAECK_LIMIT +\& KLEINGEBAECK_CONFIG +\& KLEINGEBAECK_IGNOREERRORS +.Ve +.PP +Please note, that they take precedence over config file, but +commandline flags take precedence over env! .SH "BUGS" .IX Header "BUGS" In order to report a bug, unexpected behavior, feature requests diff --git a/kleingebaeck.go b/kleingebaeck.go index 0fc8b54..7488b98 100644 --- a/kleingebaeck.go +++ b/kleingebaeck.go @@ -74,6 +74,20 @@ SETUP You can use the -v option to get verbose output or -d to enable debugging. +ENVIRONMENT VARIABLES + The following environment variables are considered: + + KLEINGEBAECK_USER + KLEINGEBAECK_DEBUG + KLEINGEBAECK_VERBOSE + KLEINGEBAECK_OUTDIR + KLEINGEBAECK_LIMIT + KLEINGEBAECK_CONFIG + KLEINGEBAECK_IGNOREERRORS + + Please note, that they take precedence over config file, but commandline + flags take precedence over env! + BUGS In order to report a bug, unexpected behavior, feature requests or to submit a patch, please open an issue on github: diff --git a/kleingebaeck.pod b/kleingebaeck.pod index 2d964ae..cad4423 100644 --- a/kleingebaeck.pod +++ b/kleingebaeck.pod @@ -77,6 +77,23 @@ directory. Then just execute C. You can use the B<-v> option to get verbose output or B<-d> to enable debugging. +=head1 ENVIRONMENT VARIABLES + +The following environment variables are considered: + + KLEINGEBAECK_USER + KLEINGEBAECK_DEBUG + KLEINGEBAECK_VERBOSE + KLEINGEBAECK_OUTDIR + KLEINGEBAECK_LIMIT + KLEINGEBAECK_CONFIG + KLEINGEBAECK_IGNOREERRORS + +Please note, that they take precedence over config file, but +commandline flags take precedence over env! + + + =head1 BUGS In order to report a bug, unexpected behavior, feature requests