Skip to content

Commit d5392b5

Browse files
committed
Added -c flag, added documentation, added to build script
1 parent 1e4f63d commit d5392b5

File tree

4 files changed

+88
-14
lines changed

4 files changed

+88
-14
lines changed

build/entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ for target in $targets; do
2121
rm $output
2222
done
2323

24+
25+
for target in $targets; do
26+
os="$(echo $target | cut -d '/' -f1)"
27+
arch="$(echo $target | cut -d '/' -f2)"
28+
output="build/gokapi-cli-${os}_${arch}"
29+
if [ $os = "windows" ]; then
30+
output+='.exe'
31+
fi
32+
33+
echo "----> Building Gokapi CLI for $target"
34+
GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -ldflags="-s -w -X 'github.com/forceu/gokapi/internal/environment.Builder=Github Release Builder' -X 'github.com/forceu/gokapi/internal/environment.BuildTime=$(date)'" -o $output github.com/forceu/gokapi/cmd/cli-uploader
35+
zip -j $output.zip $output >/dev/null
36+
rm $output
37+
done
38+
2439
echo "----> Build is complete. List of files at build/:"
2540
cd build/
2641
ls -l gokapi-*

cmd/cli-uploader/cliconfig/cliconfig.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"github.com/forceu/gokapi/cmd/cli-uploader/cliapi"
9+
"github.com/forceu/gokapi/cmd/cli-uploader/cliflags"
910
"github.com/forceu/gokapi/internal/helper"
1011
"os"
1112
"strings"
@@ -14,8 +15,6 @@ import (
1415
const minGokapiVersionInt = 20100
1516
const minGokapiVersionStr = "2.1.0"
1617

17-
const filename = "gokapi-cli.json"
18-
1918
type configFile struct {
2019
Url string `json:"Url"`
2120
Apikey string `json:"Apikey"`
@@ -29,6 +28,8 @@ func CreateLogin() {
2928
fmt.Println("ERROR: URL must start with http:// or https://")
3029
os.Exit(1)
3130
}
31+
32+
url = strings.TrimSuffix(url, "/admin")
3233
if strings.HasPrefix(url, "http://") {
3334
fmt.Println("WARNING: This URL uses an insecure connection. All data, including your API key, will be sent in plain text. This is not recommended for production use.")
3435
}
@@ -114,16 +115,16 @@ func save(url, apikey string, e2ekey []byte) error {
114115
return err
115116
}
116117

117-
return os.WriteFile(filename, jsonData, 0600)
118+
return os.WriteFile(cliflags.GetConfigLocation(), jsonData, 0600)
118119
}
119120

120121
func Load() {
121-
if !helper.FileExists(filename) {
122+
if !helper.FileExists(cliflags.GetConfigLocation()) {
122123
fmt.Println("ERROR: No login information found")
123124
fmt.Println("Please run 'gokapi-cli login' to create a login")
124125
os.Exit(1)
125126
}
126-
data, err := os.ReadFile(filename)
127+
data, err := os.ReadFile(cliflags.GetConfigLocation())
127128
if err != nil {
128129
fmt.Println("ERROR: Could not read login information")
129130
os.Exit(1)
@@ -140,8 +141,8 @@ func Load() {
140141
}
141142

142143
func Delete() error {
143-
if !helper.FileExists(filename) {
144+
if !helper.FileExists(cliflags.GetConfigLocation()) {
144145
return nil
145146
}
146-
return os.Remove(filename)
147+
return os.Remove(cliflags.GetConfigLocation())
147148
}

cmd/cli-uploader/cliflags/cliflags.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GetUploadParameters() UploadConfig {
4848
result.JsonOutput = true
4949
case "--disable-e2e":
5050
result.DisableE2e = true
51-
case "--file":
51+
case "-f":
5252
result.File = getParameter(&i)
5353
case "--expiry-days":
5454
result.ExpiryDays = requireInt(getParameter(&i))
@@ -59,7 +59,7 @@ func GetUploadParameters() UploadConfig {
5959
}
6060
}
6161
if result.File == "" {
62-
fmt.Println("ERROR: Missing parameter --file")
62+
fmt.Println("ERROR: Missing parameter -f")
6363
os.Exit(2)
6464
}
6565
if result.ExpiryDownloads < 0 {
@@ -71,6 +71,16 @@ func GetUploadParameters() UploadConfig {
7171
return result
7272
}
7373

74+
func GetConfigLocation() string {
75+
for i := 2; i < len(os.Args); i++ {
76+
switch os.Args[i] {
77+
case "-c":
78+
return getParameter(&i)
79+
}
80+
}
81+
return "gokapi-cli.json"
82+
}
83+
7484
func getParameter(position *int) string {
7585
newPosition := *position + 1
7686
position = &newPosition
@@ -91,14 +101,14 @@ func requireInt(input string) int {
91101
}
92102

93103
func printUsage() {
94-
fmt.Println("Gokapi CLI")
104+
fmt.Println("Gokapi CLI v1.0")
95105
fmt.Println()
96106
fmt.Println("Valid options are:")
97-
fmt.Println(" gokapi-cli login")
98-
fmt.Println(" gokapi-cli logout")
99-
fmt.Println(" gokapi-cli upload --file /file/to/upload [--json] [--disable-e2e]\n" +
107+
fmt.Println(" gokapi-cli login [-c /path/to/config]")
108+
fmt.Println(" gokapi-cli logout [-c /path/to/config]")
109+
fmt.Println(" gokapi-cli upload --f /file/to/upload [--json] [--disable-e2e]\n" +
100110
" [--expiry-days INT] [--expiry-downloads INT]\n" +
101-
" [--password STRING] ")
111+
" [--password STRING] [-c /path/to/config]")
102112
fmt.Println()
103113
fmt.Println("gokapi-cli upload:")
104114
fmt.Println("--json Outputs the result as JSON only")

docs/advanced.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,54 @@ Migrating Redis (``127.0.0.1:6379, User: test, Password: 1234, Prefix: gokapi_,
201201

202202
gokapi --migrate "redis://test:1234@127.0.0.1:6379?prefix=gokapi_&ssl=true" sqlite://./data/gokapi.sqlite
203203

204+
205+
206+
.. _clitool:
207+
208+
209+
********************************
210+
CLI Tool
211+
********************************
212+
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.
214+
215+
Login
216+
=================================
217+
218+
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.
219+
220+
To logout, either delete the configuration file or run ``gokapi-cli logout``.
221+
222+
.. warning::
223+
224+
The configuration file contains the login data as plain text.
225+
226+
227+
Upload
228+
=================================
229+
230+
231+
To upload a file, simply run ``gokapi-cli upload -f /path/to/file``. By default the files are encrypted (if enabled) and stored without any expiration. These additional parameters are available:
232+
233+
+-----------------------------+---------------------------------------------------+
234+
| --json | Only outputs in JSON format, unless upload failed |
235+
+-----------------------------+---------------------------------------------------+
236+
| --disable-e2e | Disables end-to-end encryption for this upload |
237+
+-----------------------------+---------------------------------------------------+
238+
| --expiry-days [number] | Sets the expiry date of the file in days |
239+
+-----------------------------+---------------------------------------------------+
240+
| --expiry-downloads [number] | Sets the allowed downloads |
241+
+-----------------------------+---------------------------------------------------+
242+
| --password [string] | Sets a password |
243+
+-----------------------------+---------------------------------------------------+
244+
| -c [path] | Use the configuration file specified |
245+
+-----------------------------+---------------------------------------------------+
246+
247+
248+
.. warning::
249+
250+
If you are using end-to-end encryption, do not upload other encrypted files simultaneously to avoid race conditions.
251+
204252
.. _api:
205253

206254

0 commit comments

Comments
 (0)