Skip to content

Commit 6db9af5

Browse files
API changes for V1 (#29)
1 parent c6892b9 commit 6db9af5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1371
-1569
lines changed

.github/workflows/build.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@ jobs:
1313
runs-on: ${{ matrix.os }}
1414
strategy:
1515
matrix:
16-
go_version: ['1.16', '1.17', '1.18']
16+
go_version: ['1.17', '1.18', '1.19']
1717
os: [ubuntu-latest, windows-latest, macos-latest]
1818

1919
steps:
2020

21+
- name: Check out code into the Go module directory
22+
uses: actions/checkout@v3
23+
2124
- name: Set up Go ${{ matrix.go_version }}
22-
uses: actions/setup-go@v2
25+
uses: actions/setup-go@v3
2326
with:
2427
go-version: ${{ matrix.go_version }}
25-
26-
- name: Check out code into the Go module directory
27-
uses: actions/checkout@v2
28+
check-latest: true
29+
cache: true
2830

2931
- name: Get dependencies
3032
run: |
31-
go get -v -t -d ./...
33+
go mod download
3234
3335
- name: Build
3436
run: go build -v ./...
@@ -39,9 +41,8 @@ jobs:
3941
if [[ "${GITHUB_TOKEN}" != "" ]]; then
4042
go test -v -race -coverprofile=coverage.txt . ./update
4143
else
42-
go test -v -race -short . ./update
44+
go test -v -race -short -coverprofile=coverage.txt . ./update
4345
fi
4446
45-
- name: Code coverage
46-
shell: bash
47-
run: "if [ -f coverage.out ]; then bash <(curl -s https://codecov.io/bash); fi"
47+
- name: Code coverage with codecov
48+
uses: codecov/codecov-action@v3

.github/workflows/codeql-analysis.yml

-67
This file was deleted.

Makefile

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ clean:
4545
$(GOCLEAN)
4646

4747
toc:
48-
go get github.com/ekalinin/github-markdown-toc.go
49-
go install github.com/ekalinin/github-markdown-toc.go
50-
go mod tidy
51-
cat ${README} | github-markdown-toc.go --hide-footer > ${TOC_PATH}
48+
@echo "[*] $@"
49+
$(GOINSTALL) github.com/ekalinin/github-markdown-toc.go/cmd/gh-md-toc@latest
50+
cat ${README} | gh-md-toc --hide-footer > ${TOC_PATH}
5251
sed -i ".1" "/${TOC_START}/,/${TOC_END}/{//!d;}" "${README}"
5352
sed -i ".2" "/${TOC_START}/r ${TOC_PATH}" "${README}"
54-
rm ${README}.1 ${README}.2 ${TOC_PATH}
53+
rm ${README}.1 ${README}.2 ${TOC_PATH}

README.md

+140-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
Self-Update library for GitHub/Gitea hosted applications in Go
1+
Self-Update library for Github, Gitea and Gitlab hosted applications in Go
22
==============================================================
33

44
[![Godoc reference](https://godoc.org/github.com/creativeprojects/go-selfupdate?status.svg)](http://godoc.org/github.com/creativeprojects/go-selfupdate)
55
[![Build](https://github.com/creativeprojects/go-selfupdate/workflows/Build/badge.svg)](https://github.com/creativeprojects/go-selfupdate/actions)
66
[![codecov](https://codecov.io/gh/creativeprojects/go-selfupdate/branch/main/graph/badge.svg?token=3FejM0fkw2)](https://codecov.io/gh/creativeprojects/go-selfupdate)
77

88
<!--ts-->
9-
* [Self\-Update library for GitHub/Gitea hosted applications in Go](#self-update-library-for-githubgitea-hosted-applications-in-go)
9+
* [Self\-Update library for Github, Gitea and Gitlab hosted applications in Go](#self-update-library-for-github-gitea-and-gitlab-hosted-applications-in-go)
1010
* [Introduction](#introduction)
1111
* [Example](#example)
12-
* [Important note](#important-note)
12+
* [Upgrade from v0\+ to v1](#upgrade-from-v0-to-v1)
13+
* [Repository](#repository)
14+
* [ParseSlug](#parseslug)
15+
* [NewRepositorySlug](#newrepositoryslug)
16+
* [NewRepositoryID (GitLab only)](#newrepositoryid-gitlab-only)
17+
* [Context](#context)
18+
* [Package functions](#package-functions)
19+
* [Methods on Source interface](#methods-on-source-interface)
20+
* [Methods on Updater struct](#methods-on-updater-struct)
1321
* [Naming Rules of Released Binaries](#naming-rules-of-released-binaries)
1422
* [Naming Rules of Versions (=Git Tags)](#naming-rules-of-versions-git-tags)
1523
* [Structure of Releases](#structure-of-releases)
@@ -18,7 +26,9 @@ Self-Update library for GitHub/Gitea hosted applications in Go
1826
* [SHA256](#sha256)
1927
* [ECDSA](#ecdsa)
2028
* [Using a single checksum file for all your assets](#using-a-single-checksum-file-for-all-your-assets)
21-
* [Using other providers than Github](#using-other-providers-than-github)
29+
* [Other providers than Github](#other-providers-than-github)
30+
* [GitLab](#gitlab)
31+
* [Example:](#example-1)
2232
* [Copyright](#copyright)
2333

2434
<!--te-->
@@ -47,7 +57,7 @@ This library started as a fork of https://github.com/rhysd/go-github-selfupdate.
4757
- able to detect different ARM CPU architectures (the original library wasn't working on my different versions of raspberry pi)
4858
- support for assets compressed with bzip2 (.bz2)
4959
- can use a single file containing the sha256 checksums for all the files (one per line)
50-
- separate the provider and the updater, so we can add more providers (GitHub, Gitea, Gitlab, etc.)
60+
- separate the provider and the updater, so we can add more providers (Github, Gitea, Gitlab, etc.)
5161
- return well defined wrapped errors that can be checked with `errors.Is(err error, target error)`
5262

5363
# Example
@@ -56,9 +66,9 @@ Here's an example how to use the library for an application to update itself
5666

5767
```go
5868
func update(version string) error {
59-
latest, found, err := selfupdate.DetectLatest("creativeprojects/resticprofile")
69+
latest, found, err := selfupdate.DetectLatest(context.Background(), selfupdate.ParseSlug("creativeprojects/resticprofile"))
6070
if err != nil {
61-
return fmt.Errorf("error occurred while detecting version: %v", err)
71+
return fmt.Errorf("error occurred while detecting version: %w", err)
6272
}
6373
if !found {
6474
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
@@ -73,18 +83,79 @@ func update(version string) error {
7383
if err != nil {
7484
return errors.New("could not locate executable path")
7585
}
76-
if err := selfupdate.UpdateTo(latest.AssetURL, latest.AssetName, exe); err != nil {
77-
return fmt.Errorf("error occurred while updating binary: %v", err)
86+
if err := selfupdate.UpdateTo(context.Background(), latest.AssetURL, latest.AssetName, exe); err != nil {
87+
return fmt.Errorf("error occurred while updating binary: %w", err)
7888
}
7989
log.Printf("Successfully updated to version %s", latest.Version())
8090
return nil
8191
}
8292
```
8393

84-
# Important note
94+
# Upgrade from v0+ to v1
95+
96+
Version v1+ has a **stable** API. It is slightly different from the API of versions 0+.
97+
98+
## Repository
99+
100+
Some functions needed a couple `owner`/`repo` and some other a single string called `slug`. These have been replaced by a `Repository`.
101+
102+
Two constructors are available:
103+
104+
### ParseSlug
105+
106+
Parses a *slug* string like `owner/repository_name`
107+
108+
```go
109+
func ParseSlug(slug string) RepositorySlug
110+
```
111+
112+
### NewRepositorySlug
113+
114+
Creates a repository from both owner and repo strings
115+
116+
```go
117+
func NewRepositorySlug(owner, repo string) RepositorySlug
118+
```
119+
120+
### NewRepositoryID (GitLab only)
121+
122+
GitLab can also refer to a repository via its internal ID. This constructor can be used with a numeric repository ID.
123+
124+
```go
125+
func NewRepositoryID(id int) RepositoryID
126+
```
127+
128+
### Context
129+
130+
All methods are now accepting a `context` as their first parameter. You can use it to cancel a long running operation.
131+
132+
## Package functions
133+
134+
| v0 | v1 |
135+
|----|----|
136+
| UpdateTo(assetURL, assetFileName, cmdPath string) error | UpdateTo(ctx context.Context, assetURL, assetFileName, cmdPath string) error |
137+
| DetectLatest(slug string) (*Release, bool, error) | DetectLatest(ctx context.Context, repository Repository) (*Release, bool, error) |
138+
| DetectVersion(slug string, version string) (*Release, bool, error) | DetectVersion(ctx context.Context, repository Repository, version string) (*Release, bool, error) |
139+
| UpdateCommand(cmdPath string, current string, slug string) (*Release, error) | UpdateCommand(ctx context.Context, cmdPath string, current string, repository Repository) (*Release, error) |
140+
| UpdateSelf(current string, slug string) (*Release, error) | UpdateSelf(ctx context.Context, current string, repository Repository) (*Release, error) |
141+
142+
## Methods on Source interface
143+
144+
| v0 | v1 |
145+
|----|----|
146+
| ListReleases(owner, repo string) ([]SourceRelease, error) | ListReleases(ctx context.Context, repository Repository) ([]SourceRelease, error) |
147+
| DownloadReleaseAsset(owner, repo string, releaseID, id int64) (io.ReadCloser, error) | DownloadReleaseAsset(ctx context.Context, rel *Release, assetID int64) (io.ReadCloser, error) |
148+
149+
## Methods on Updater struct
150+
151+
| v0 | v1 |
152+
|----|----|
153+
| DetectLatest(slug string) (release *Release, found bool, err error) | DetectLatest(ctx context.Context, repository Repository) (release *Release, found bool, err error) |
154+
| DetectVersion(slug string, version string) (release *Release, found bool, err error) | DetectVersion(ctx context.Context, repository Repository, version string) (release *Release, found bool, err error) |
155+
| UpdateCommand(cmdPath string, current string, slug string) (*Release, error) | UpdateCommand(ctx context.Context, cmdPath string, current string, repository Repository) (*Release, error) |
156+
| UpdateSelf(current string, slug string) (*Release, error) | UpdateSelf(ctx context.Context, current string, repository Repository) (*Release, error) |
157+
| UpdateTo(rel *Release, cmdPath string) error | UpdateTo(ctx context.Context, rel *Release, cmdPath string) error |
85158

86-
**The API can change anytime until it reaches version 1.0.**
87-
It is unlikely it will change drastically though, but it can.
88159

89160
# Naming Rules of Released Binaries
90161

@@ -229,14 +300,68 @@ Tools like [goreleaser][] produce a single checksum file for all your assets. A
229300
updater, _ := NewUpdater(Config{Validator: &ChecksumValidator{UniqueFilename: "checksums.txt"}})
230301
```
231302

232-
# Using other providers than Github
303+
# Other providers than Github
233304

234305
This library can be easily extended by providing a new source and release implementation for any git provider
235306
Currently implemented are
236307
- Github (default)
237-
- Gitea
308+
- Gitea
309+
- Gitlab
238310

239-
Check the *-custom examples in cmd to see how a custom source like the GiteaSource can be used
311+
# GitLab
312+
313+
Support for GitLab is experimental.
314+
315+
To be able to download assets from a private instance of GitLab, you have to publish your files to the [Generic Package Registry](https://docs.gitlab.com/ee/user/packages/package_registry/index.html).
316+
317+
If you're using goreleaser, you just need to add this option:
318+
319+
```yaml
320+
# .goreleaser.yml
321+
gitlab_urls:
322+
use_package_registry: true
323+
324+
```
325+
326+
See [goreleaser documentation](https://goreleaser.com/scm/gitlab/#generic-package-registry) for more information.
327+
328+
## Example:
329+
330+
```go
331+
func update() {
332+
source, err := selfupdate.NewGitLabSource(selfupdate.GitLabConfig{
333+
BaseURL: "https://private.instance.on.gitlab.com/",
334+
})
335+
if err != nil {
336+
log.Fatal(err)
337+
}
338+
updater, err := selfupdate.NewUpdater(selfupdate.Config{
339+
Source: source,
340+
Validator: &selfupdate.ChecksumValidator{UniqueFilename: "checksums.txt"}, // checksum from goreleaser
341+
})
342+
if err != nil {
343+
log.Fatal(err)
344+
}
345+
release, found, err := updater.DetectLatest(context.Background(), selfupdate.NewRepositorySlug("owner", "cli-tool"))
346+
if err != nil {
347+
log.Fatal(err)
348+
}
349+
if !found {
350+
log.Print("Release not found")
351+
return
352+
}
353+
fmt.Printf("found release %s\n", release.Version())
354+
355+
exe, err := os.Executable()
356+
if err != nil {
357+
return errors.New("could not locate executable path")
358+
}
359+
err = updater.UpdateTo(context.Background(), release, exe)
360+
if err != nil {
361+
log.Fatal(err)
362+
}
363+
}
364+
```
240365

241366
# Copyright
242367

arch.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ const (
1111

1212
// generateAdditionalArch we can use depending on the type of CPU
1313
func generateAdditionalArch(arch string, goarm uint8) []string {
14-
additionalArch := make([]string, 0, maxARM-minARM)
1514
if arch == "arm" && goarm >= minARM && goarm <= maxARM {
15+
additionalArch := make([]string, 0, maxARM-minARM)
1616
for v := goarm; v >= minARM; v-- {
1717
additionalArch = append(additionalArch, fmt.Sprintf("armv%d", v))
1818
}
19+
return additionalArch
1920
}
2021
if arch == "amd64" {
21-
additionalArch = append(additionalArch, "x86_64")
22+
return []string{"x86_64"}
2223
}
23-
return additionalArch
24+
return []string{}
2425
}

arch_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestAdditionalArch(t *testing.T) {
1919
{"arm", 6, []string{"armv6", "armv5"}},
2020
{"arm", 5, []string{"armv5"}},
2121
{"arm", 4, []string{}}, // go is not supporting below armv5
22+
{"amd64", 0, []string{"x86_64"}},
2223
}
2324

2425
for _, testItem := range testData {

0 commit comments

Comments
 (0)