Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/actions/generate-build-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ description: 'Read the version of the assembly and retain'
runs:
using: 'composite'
steps:
- name: 📥 Read version with Build scripts
- name: 📥 Read version from manifest
id: ver
shell: bash
run: |
set -Eeuo pipefail
nameBuild=$(./_run/scripts/sys.sh -n)
versionBuild=$(./_run/scripts/sys.sh -v)
gometagen="github.com/amazing-generators/gometagen/cmd/gometagen@latest"
nameBuild=$(go run "$gometagen" manifest get -field name -source ./_run/values.yml)
versionBuild=$(go run "$gometagen" version print -source ./_run/values.yml)

echo "BUILD_NAME=$nameBuild" > build_env.txt
echo "BUILD_VER=$versionBuild" >> build_env.txt
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ jobs:

- name: ✔️ Finish
run: |
new_version=$(./_run/scripts/sys.sh --increment --minor)
new_version=$(go run github.com/amazing-generators/gometagen/cmd/gometagen@latest version minor -source ./_run/values.yml)

git add ./_run/values/ver.txt
git add ./_run/values.yml
git commit -m "actions [$new_version] "$'\n'"Build: [${BUILD_VER}] >> [$new_version]"
git push origin HEAD:main

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target/
/tmp/
.idea/
*.bin
*.exe
.env
Expand Down
96 changes: 95 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The library accepts a repository URL, **auto-detects the provider** (GitHub, Git

## Authentication

This module does not implement authentication.
The library itself does not implement authentication:

- No tokens
- No OAuth
Expand All @@ -33,6 +33,69 @@ This module does not implement authentication.

It simply accepts a repository URL and works with public endpoints. If the platform requires authorization for a request, the request will fail and the error will be returned.

However, all API traffic goes through the shared `lightweigit.HttpClient`, so you can attach credentials yourself with a
custom `http.RoundTripper`. This is useful even for public repositories: authenticated GitHub requests get a much higher
rate limit (5000 requests/hour instead of 60/hour per IP).

### GitHub API token

```go
package main

import (
"net/http"
"os"
"time"

lightweigit "github.com/voluminor/lightweigit-loader"
)

type githubAuth struct {
token string
}

func (t githubAuth) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL.Host == "api.github.com" {
req = req.Clone(req.Context())
req.Header.Set("Authorization", "Bearer "+t.token)
}
return http.DefaultTransport.RoundTrip(req)
}

func main() {
lightweigit.HttpClient = &http.Client{
Timeout: 10 * time.Second,
Transport: githubAuth{token: os.Getenv("GITHUB_TOKEN")},
}

// ... use the library as usual
}
```

### Several providers at once

Each provider expects its own header, so scope credentials by host:

```go
type multiAuth struct{}

func (multiAuth) RoundTrip(req *http.Request) (*http.Response, error) {
req = req.Clone(req.Context())
switch req.URL.Host {
case "api.github.com":
req.Header.Set("Authorization", "Bearer "+os.Getenv("GITHUB_TOKEN"))
case "gitlab.com":
req.Header.Set("PRIVATE-TOKEN", os.Getenv("GITLAB_TOKEN"))
case "api.bitbucket.org":
req.Header.Set("Authorization", "Bearer "+os.Getenv("BITBUCKET_TOKEN"))
}
return http.DefaultTransport.RoundTrip(req)
}
```

> **Note:** asset and archive downloads (`ZIP()`, `TAR()`, asset `URL()`) are plain URLs that you fetch with your own
> HTTP client — for private repositories attach the same credentials to those requests as well.

## Installation

If you use this repository as a Go module:
Expand Down Expand Up @@ -402,6 +465,37 @@ lightweigit.HttpClient = &http.Client{
> **Note:** `lightweigit.HttpClient` is a shared global variable. Changing it affects all providers and all goroutines
> in the process. Set it once during initialization before making any API calls.

## Development setup (working on this repository)

The `target/` package (`meta_gen.go`, `map.go`, `global/`) is fully generated and git-ignored: release tags include it,
but a fresh clone of `main` will not compile until you generate it.

One-time bootstrap on Linux/macOS (bash):

```bash
bash _run/firststart.sh
```

This installs [gometagen](https://github.com/amazing-generators/gometagen), registers the git commit/push hooks, and
runs `go generate` + `go mod tidy`.

On Windows (or without bash) run the same steps manually:

```bash
go install github.com/amazing-generators/gometagen/cmd/gometagen@latest
go run github.com/amazing-generators/gometagen/cmd/gometagen@latest git add-commit-hook -source .
go run github.com/amazing-generators/gometagen/cmd/gometagen@latest git add-push-hook -source .
go generate .
go mod tidy
```

Note: the git hooks themselves are bash scripts (`_run/commit-hook.sh`, `_run/push-hook.sh`), so on Windows they
require Git Bash (bundled with Git for Windows) or WSL.

Versioning is driven by `_run/values.yml`: every `git push` bumps the patch version (push hook), and the release
workflow bumps the minor version after publishing a tag. The commit hook runs `go test -short ./...` —
network-dependent tests are skipped in short mode; run `go test ./...` for the full suite.

## Design notes

* Dependency-free (standard library only)
Expand Down
26 changes: 18 additions & 8 deletions _run/commit-hook.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#!/bin/bash
#!/usr/bin/env bash

set -Eeuo pipefail

echo "[HOOK]" "Commit"

run_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
values_dir="$run_dir/values"
script_dir="$run_dir/scripts"
root_path=$(cd "$run_dir/.." && pwd)
manifest="$run_dir/values.yml"
gometagen="github.com/amazing-generators/gometagen/cmd/gometagen@latest"

VERSION=$(bash "$script_dir/sys.sh" -v)
NAME=$(bash "$script_dir/git.sh" -b)
VERSION=$(go run "$gometagen" version print -source "$manifest")
BRANCH=$(go run "$gometagen" git branch -source "$root_path")

echo -e "$NAME [$VERSION] \n" $(cat "$1") > "$1"
tmp_file="${1}.tmp"
{
printf "%s [%s]\n\n" "$BRANCH" "$VERSION"
cat "$1"
} > "$tmp_file"
mv "$tmp_file" "$1"
#############################################################################

go test -v ./...
(
cd "$root_path"
go test -short -v ./...
)

#############################################################################
exit 0

22 changes: 15 additions & 7 deletions _run/firststart.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#!/bin/bash
#!/usr/bin/env bash

scripts/git.sh --add_commit
scripts/git.sh --add_push
set -Eeuo pipefail

cd ../
run_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_path="$(cd "$run_dir/.." && pwd)"
gometagen="github.com/amazing-generators/gometagen/cmd/gometagen@latest"

cd "$root_path"

mkdir -p target
mkdir -p tmp

go install "$gometagen"

go run "$gometagen" git add-commit-hook -source "$root_path"
go run "$gometagen" git add-push-hook -source "$root_path"

#go work sync
bash "_run/scripts/go_tidy_all.sh"
go generate
go generate .
go mod tidy
go generate .
25 changes: 17 additions & 8 deletions _run/push-hook.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#!/bin/bash
#!/usr/bin/env bash

set -Eeuo pipefail

echo "[HOOK]" "Push"

run_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
values_dir="$run_dir/values"
script_dir="$run_dir/scripts"
root_path=$(cd "$run_dir/.." && pwd)
manifest="$run_dir/values.yml"
gometagen="github.com/amazing-generators/gometagen/cmd/gometagen@latest"

#############################################################################

bash "$script_dir/go_tidy_all.sh"
(
cd "$root_path"
go generate .
go mod tidy
)

OLD_VER=$(bash "$script_dir/sys.sh" -v)
VERSION=$(bash "$script_dir/sys.sh" -i -pa)
OLD_VER=$(go run "$gometagen" version print -source "$manifest")
VERSION=$(go run "$gometagen" version patch -source "$manifest")

bash "$script_dir/go_creator_const.sh"
(
cd "$root_path"
go generate .
)

echo "Updated patch-ver:" "$OLD_VER >> $VERSION"

#############################################################################
exit 0

Loading
Loading