Skip to content

Commit 25fb635

Browse files
committed
Added Docker release for CLI, updated documentation, added docker-specific features to CLI
1 parent 9a574db commit 25fb635

File tree

7 files changed

+202
-8
lines changed

7 files changed

+202
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Docker Publish CLI Custom Release Multiarch
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
tagname:
7+
description: 'Tag name to be built'
8+
required: true
9+
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
build:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- run: git checkout tags/${{ github.event.inputs.tagname }}
23+
- name: install buildx
24+
id: buildx
25+
uses: crazy-max/ghaction-docker-buildx@v1
26+
with:
27+
version: latest
28+
- name: login to docker hub
29+
run: echo "${{ secrets.DOCKER_PW }}" | docker login -u "${{ secrets.DOCKER_USER }}" --password-stdin
30+
- name: build and push the image
31+
run: |
32+
docker buildx build -f Dockerfile.cli-tool --push --tag f0rc3/gokapi-cli:${{ github.event.inputs.tagname }} --platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64 .

Dockerfile cli-tool

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.24.0-alpine AS build_base
2+
3+
## Usage:
4+
## docker build . -f Dockerfile.cli-tool -t gokapi-cli
5+
## docker run --rm -v gokapi-cli-config:/app/config gokapi-cli
6+
7+
RUN mkdir /compile
8+
COPY go.mod /compile
9+
RUN cd /compile && go mod download
10+
11+
COPY . /compile
12+
13+
RUN cd /compile && go generate ./... && CGO_ENABLED=0 go build -ldflags="-s -w -X 'github.com/forceu/gokapi/internal/environment.IsDocker=true' -X 'github.com/forceu/gokapi/internal/environment.Builder=Project Docker File' -X 'github.com/forceu/gokapi/internal/environment.BuildTime=$(date)'" -o /compile/gokapi-cli github.com/forceu/gokapi/cmd/cli-uploader
14+
15+
FROM alpine:3.19
16+
17+
COPY --from=build_base /compile/gokapi-cli /app/gokapi-cli
18+
WORKDIR /app
19+
20+
ENTRYPOINT ["/app/gokapi-cli"]

Dockerfile.cli-tool

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.24.0-alpine AS build_base
2+
3+
## Usage:
4+
## docker build . -f Dockerfile.cli-tool -t gokapi-cli
5+
## docker run -it --rm -v gokapi-cli-config:/app/config gokapi-cli
6+
7+
RUN mkdir /compile
8+
COPY go.mod /compile
9+
RUN cd /compile && go mod download
10+
11+
COPY . /compile
12+
13+
RUN cd /compile && go generate ./... && CGO_ENABLED=0 go build -ldflags="-s -w -X 'github.com/forceu/gokapi/internal/environment.IsDocker=true' -X 'github.com/forceu/gokapi/internal/environment.Builder=Project Docker File' -X 'github.com/forceu/gokapi/internal/environment.BuildTime=$(date)'" -o /compile/gokapi-cli github.com/forceu/gokapi/cmd/cli-uploader
14+
15+
FROM alpine:3.19
16+
17+
COPY --from=build_base /compile/gokapi-cli /app/gokapi-cli
18+
WORKDIR /app
19+
20+
ENTRYPOINT ["/app/gokapi-cli"]

cmd/cli-uploader/Main.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"github.com/forceu/gokapi/cmd/cli-uploader/cliapi"
77
"github.com/forceu/gokapi/cmd/cli-uploader/cliconfig"
88
"github.com/forceu/gokapi/cmd/cli-uploader/cliflags"
9+
"github.com/forceu/gokapi/internal/environment"
10+
"github.com/forceu/gokapi/internal/helper"
911
"os"
1012
)
1113

1214
func main() {
15+
cliflags.Init(cliconfig.DockerFolderConfigFile, cliconfig.DockerFolderUpload)
1316
mode := cliflags.Parse()
1417
switch mode {
1518
case cliflags.ModeLogin:
16-
cliconfig.CreateLogin()
19+
doLogin()
1720
case cliflags.ModeLogout:
1821
doLogout()
1922
case cliflags.ModeUpload:
@@ -23,6 +26,11 @@ func main() {
2326
}
2427
}
2528

29+
func doLogin() {
30+
checkDockerFolders()
31+
cliconfig.CreateLogin()
32+
}
33+
2634
func processUpload() {
2735
cliconfig.Load()
2836
uploadParam := cliflags.GetUploadParameters()
@@ -36,11 +44,12 @@ func processUpload() {
3644
if uploadParam.JsonOutput {
3745
jsonStr, _ := json.Marshal(result)
3846
fmt.Println(string(jsonStr))
39-
} else {
40-
fmt.Println("File uploaded successfully")
41-
fmt.Println("File ID: " + result.Id)
42-
fmt.Println("File Download URL: " + result.UrlDownload)
47+
return
4348
}
49+
fmt.Println("Upload successful")
50+
fmt.Println("File Name: " + result.Name)
51+
fmt.Println("File ID: " + result.Id)
52+
fmt.Println("File Download URL: " + result.UrlDownload)
4453
}
4554

4655
func doLogout() {
@@ -52,3 +61,16 @@ func doLogout() {
5261
}
5362
fmt.Println("Logged out. To login again, run: gokapi-cli login")
5463
}
64+
65+
func checkDockerFolders() {
66+
if !environment.IsDockerInstance() {
67+
return
68+
}
69+
if !helper.FolderExists(cliconfig.DockerFolderConfig) {
70+
fmt.Println("Warning: Docker folder does not exist, configuration will be lost when creating a new container")
71+
helper.CreateDir(cliconfig.DockerFolderConfig)
72+
}
73+
if !helper.FolderExists(cliconfig.DockerFolderUpload) {
74+
helper.CreateDir(cliconfig.DockerFolderUpload)
75+
}
76+
}

cmd/cli-uploader/cliconfig/cliconfig.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
const minGokapiVersionInt = 20100
1616
const minGokapiVersionStr = "2.1.0"
1717

18+
const DockerFolderConfig = "/app/config/"
19+
const DockerFolderConfigFile = DockerFolderConfig + "config.json"
20+
const DockerFolderUpload = "/upload/"
21+
1822
type configFile struct {
1923
Url string `json:"Url"`
2024
Apikey string `json:"Apikey"`

cmd/cli-uploader/cliflags/cliflags.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cliflags
22

33
import (
44
"fmt"
5+
"github.com/forceu/gokapi/internal/environment"
56
"os"
7+
"path/filepath"
68
"strconv"
79
)
810

@@ -13,6 +15,9 @@ const (
1315
ModeInvalid
1416
)
1517

18+
var dockerConfigFile string
19+
var dockerUploadFolder string
20+
1621
type UploadConfig struct {
1722
File string
1823
JsonOutput bool
@@ -22,6 +27,11 @@ type UploadConfig struct {
2227
Password string
2328
}
2429

30+
func Init(dockerConfigPath, dockerUploadPath string) {
31+
dockerConfigFile = dockerConfigPath
32+
dockerUploadFolder = dockerUploadPath
33+
}
34+
2535
func Parse() int {
2636
if len(os.Args) < 2 {
2737
printUsage()
@@ -59,8 +69,17 @@ func GetUploadParameters() UploadConfig {
5969
}
6070
}
6171
if result.File == "" {
62-
fmt.Println("ERROR: Missing parameter -f")
63-
os.Exit(2)
72+
if environment.IsDockerInstance() {
73+
ok, dockerFile := getDockerUpload()
74+
if !ok {
75+
fmt.Println("ERROR: Missing parameter -f and no file or more than one file found in " + dockerUploadFolder)
76+
os.Exit(2)
77+
}
78+
result.File = dockerFile
79+
} else {
80+
fmt.Println("ERROR: Missing parameter -f")
81+
os.Exit(2)
82+
}
6483
}
6584
if result.ExpiryDownloads < 0 {
6685
result.ExpiryDownloads = 0
@@ -71,7 +90,37 @@ func GetUploadParameters() UploadConfig {
7190
return result
7291
}
7392

93+
func getDockerUpload() (bool, string) {
94+
if !environment.IsDockerInstance() {
95+
return false, ""
96+
}
97+
entries, err := os.ReadDir(dockerUploadFolder)
98+
if err != nil {
99+
return false, ""
100+
}
101+
102+
var fileName string
103+
var fileFound bool
104+
for _, entry := range entries {
105+
if entry.Type().IsRegular() {
106+
if fileFound {
107+
// More than one file exist
108+
return false, ""
109+
}
110+
fileName = entry.Name()
111+
fileFound = true
112+
}
113+
}
114+
if !fileFound {
115+
return false, ""
116+
}
117+
return true, filepath.Join(dockerUploadFolder, fileName)
118+
}
119+
74120
func GetConfigLocation() string {
121+
if environment.IsDockerInstance() {
122+
return dockerConfigFile
123+
}
75124
for i := 2; i < len(os.Args); i++ {
76125
switch os.Args[i] {
77126
case "-c":

docs/advanced.rst

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,38 @@ Migrating Redis (``127.0.0.1:6379, User: test, Password: 1234, Prefix: gokapi_,
210210
CLI Tool
211211
********************************
212212

213-
Gokapi also has a CLI tool that allows uploads from the command line. Binaries are avaible on the release page (``gokapi-cli``) for Linux, Windows and MacOs. To compile it yourself, download the repository and run ``make build-cli`` in the top directory.
213+
Gokapi also has a CLI tool that allows uploads from the command line. Binaries are avaible on the `Github release page <https://github.com/Forceu/Gokapi/releases>`_ for Linux, Windows and MacOS. To compile it yourself, download the repository and run ``make build-cli`` in the top directory.
214+
215+
Alternatively you can use the tool with Docker, although it will be slightly less user-friendly.
216+
217+
.. note::
218+
219+
Gokapi v2.1.0 or newer is required to use the CLI tool.
214220

215221
Login
216222
=================================
217223

218224
First you need to login with the command ``gokapi-cli login``. You will then be asked for your server URL and a valid API key with upload permission. If end-to-end encryption is enabled, you will also need to enter your encyption key. By default the login data is saved to ``gokapi-cli.json``, but you can define a different location with the ``-c`` parameter.
219225

226+
220227
To logout, either delete the configuration file or run ``gokapi-cli logout``.
221228

222229
.. warning::
223230

224231
The configuration file contains the login data as plain text.
225232

226233

234+
Docker
235+
---------------------------------
236+
237+
If you are using Docker, your config will always be saved to ``/app/config/config.json`` and the location cannot be changed. To login, execute the following command:
238+
239+
docker run -it --rm -v gokapi-cli-config:/app/config docker.io/f0rc3/gokapi-cli:latest login
240+
241+
The volume ``gokapi-cli-config:/app/config`` is not required if you re-use the container, but it is still highly recommended. If the volume is not mounted, you will need to log in again after every new container creation.
242+
243+
244+
227245
Upload
228246
=================================
229247

@@ -244,11 +262,40 @@ To upload a file, simply run ``gokapi-cli upload -f /path/to/file``. By default
244262
| -c [path] | Use the configuration file specified |
245263
+-----------------------------+---------------------------------------------------+
246264

265+
Example: Uploading the file ``/tmp/example``. It will expire in 10 days, has unlimited downloads and requires the password ``abcd``:
266+
::
247267

268+
gokapi-cli upload -f /tmp/example --expiry-days 10 --password abcd
269+
270+
248271
.. warning::
249272

250273
If you are using end-to-end encryption, do not upload other encrypted files simultaneously to avoid race conditions.
251274

275+
276+
277+
Docker
278+
---------------------------------
279+
280+
As a Docker container cannot access your host files without a volume, you will need to mount the folder that contains your file to upload and then specify the internal file path with ``-f``. If no ``-f`` parameter is supplied and only a single file exists in the container folder ``/upload/``, this file will be uploaded.
281+
282+
Example: Uploading the file ``/tmp/example``. It will expire after 5 downloads, has no time expiry and has no password.
283+
::
284+
285+
docker run --rm -v gokapi-cli-config:/app/config -v /tmp/:/upload/ docker.io/f0rc3/gokapi-cli:latest upload -f /upload/example --expiry-downloads 5
286+
287+
Example: Uploading the file ``/tmp/single/example``. There is no other file in the folder ``/tmp/single/``.
288+
::
289+
290+
docker run --rm -v gokapi-cli-config:/app/config -v /tmp/single/:/upload/ docker.io/f0rc3/gokapi-cli:latest upload
291+
292+
Example: Uploading the file ``/tmp/multiple/example``. There are other files in the folder ``/tmp/multiple/``.
293+
::
294+
295+
docker run --rm -v gokapi-cli-config:/app/config -v /tmp/multiple/example:/upload/example docker.io/f0rc3/gokapi-cli:latest upload
296+
297+
298+
252299
.. _api:
253300

254301

0 commit comments

Comments
 (0)