Skip to content

Commit fd4f0ba

Browse files
committed
enable configuration from a json file
1 parent 3450516 commit fd4f0ba

5 files changed

Lines changed: 79 additions & 4 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ You can download the latest release from [here](https://github.com/tomcz/s3backu
2525
```
2626
Usage: s3backup <command>
2727
28-
S3 backup script in a single binary
28+
S3 backup script in a single binary.
29+
30+
NOTE: Command line flag values can optionally be retrieved from a JSON
31+
configuration file. The path to this configuration file is provided at runtime
32+
by the S3BACKUP_JSON environment variable. The JSON file should contain an
33+
object whose keys must match the command line flags they are meant to configure.
2934
3035
Flags:
3136
-h, --help Show context-sensitive help.

cmd/s3backup/main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ var (
2727
tag string
2828
)
2929

30+
const description = `
31+
S3 backup script in a single binary.
32+
33+
NOTE: Command flag values can optionally be retrieved from a JSON configuration file.
34+
The path to this configuration file is provided at runtime by the S3BACKUP_JSON environment variable.
35+
The JSON file should contain an object whose keys must match the command line flags they are meant to configure.
36+
`
37+
3038
type putFlags struct {
3139
LocalPath string `arg:"" help:"Required local file path"`
3240
RemotePath string `arg:"" help:"Required s3 path (s3://bucket/objectkey)"`
@@ -129,10 +137,17 @@ func main() {
129137
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
130138
defer stop()
131139

132-
description := kong.Description("S3 backup script in a single binary")
133-
app := kong.Parse(&appCfg{}, description, kong.HelpOptions{Compact: true})
134-
app.BindTo(ctx, (*context.Context)(nil))
140+
opts := []kong.Option{
141+
kong.Description(description),
142+
kong.HelpOptions{Compact: true},
143+
}
144+
if file := os.Getenv("S3BACKUP_JSON"); file != "" {
145+
log.Println("Using configuration from", file)
146+
opts = append(opts, kong.Configuration(kong.JSON, file))
147+
}
135148

149+
app := kong.Parse(&appCfg{}, opts...)
150+
app.BindTo(ctx, (*context.Context)(nil))
136151
if err := app.Run(); err != nil {
137152
log.Fatalln(err)
138153
}

cmd/s3backup/main_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"testing"
55

6+
"github.com/alecthomas/kong"
67
"gotest.tools/v3/assert"
78
)
89

@@ -31,3 +32,39 @@ func TestCheckPaths(t *testing.T) {
3132
assert.Equal(t, inLocal, outRemote)
3233
assert.Equal(t, inRemote, outLocal)
3334
}
35+
36+
func TestJsonConfig_Get(t *testing.T) {
37+
var app appCfg
38+
parser, err := kong.New(&app, kong.Configuration(kong.JSON, "testdata/get.json"))
39+
assert.NilError(t, err)
40+
_, err = parser.Parse([]string{"get", "remote", "local"})
41+
assert.NilError(t, err)
42+
assert.Equal(t, app.Get.LocalPath, "local")
43+
assert.Equal(t, app.Get.RemotePath, "remote")
44+
assert.Equal(t, app.Get.SkipHash, true)
45+
assert.Equal(t, app.Get.SymKey, "wibble")
46+
assert.Equal(t, app.Get.PemKey, "")
47+
assert.Equal(t, app.Get.AccessKey, "test_accessKey")
48+
assert.Equal(t, app.Get.SecretKey, "test_secretKey")
49+
assert.Equal(t, app.Get.Token, "test_token")
50+
assert.Equal(t, app.Get.Region, "test_region")
51+
assert.Equal(t, app.Get.Endpoint, "test_endpoint")
52+
}
53+
54+
func TestJsonConfig_Put(t *testing.T) {
55+
var app appCfg
56+
parser, err := kong.New(&app, kong.Configuration(kong.JSON, "testdata/put.json"))
57+
assert.NilError(t, err)
58+
_, err = parser.Parse([]string{"put", "local", "remote"})
59+
assert.NilError(t, err)
60+
assert.Equal(t, app.Put.LocalPath, "local")
61+
assert.Equal(t, app.Put.RemotePath, "remote")
62+
assert.Equal(t, app.Put.SkipHash, false)
63+
assert.Equal(t, app.Put.SymKey, "")
64+
assert.Equal(t, app.Put.PemKey, "wobble")
65+
assert.Equal(t, app.Put.AccessKey, "test_accessKey")
66+
assert.Equal(t, app.Put.SecretKey, "test_secretKey")
67+
assert.Equal(t, app.Put.Token, "test_token")
68+
assert.Equal(t, app.Put.Region, "test_region")
69+
assert.Equal(t, app.Put.Endpoint, "test_endpoint")
70+
}

cmd/s3backup/testdata/get.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"nocheck": true,
3+
"symKey": "wibble",
4+
"accessKey": "test_accessKey",
5+
"secretKey": "test_secretKey",
6+
"token": "test_token",
7+
"region": "test_region",
8+
"endpoint": "test_endpoint"
9+
}

cmd/s3backup/testdata/put.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"nocheck": false,
3+
"pemKey": "wobble",
4+
"accessKey": "test_accessKey",
5+
"secretKey": "test_secretKey",
6+
"token": "test_token",
7+
"region": "test_region",
8+
"endpoint": "test_endpoint"
9+
}

0 commit comments

Comments
 (0)