Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
nemre committed Nov 16, 2023
1 parent d94baa4 commit d81ac46
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 93 deletions.
97 changes: 44 additions & 53 deletions README.md

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
connectTimeout = 5 * time.Second
executionTimeout = 3 * time.Minute
processTimeout = 10 * time.Second
codeAuditComment = "// Created by %s on %s via github.com/trendyol/cbef.\n\n%s"
)
Expand All @@ -25,20 +25,20 @@ func main() {

env, err := model.NewEnvironment()
if err != nil {
log.Fatal(err.Error())
log.Fatal("failed to load environment", "error", err.Error())
}

f, err := config.Parse(env.ConfigFile)
if err != nil {
log.Fatal(err.Error())
log.Fatal("failed to parse config file", "error", err.Error())
}

ctx, cancel := context.WithTimeout(context.Background(), env.ExecutionTimeout)
ctx, cancel := context.WithTimeout(context.Background(), executionTimeout)
defer cancel()

cluster, err := couchbase.Connect(ctx, connectTimeout, f.Cluster)
cluster, err := couchbase.Connect(ctx, processTimeout, f.Cluster)
if err != nil {
log.Fatal(err.Error()) //nolint:gocritic
log.Fatal("failed to connect couchbase", "error", err.Error()) //nolint:gocritic
}

code, err := os.ReadFile(filepath.Join(filepath.Dir(env.ConfigFile), f.Name+".js"))
Expand All @@ -62,6 +62,6 @@ func main() {
}

if err = act.Upsert(ctx, f); err != nil {
log.Fatal("failed to upsert function", "error", err.Error(), "function", f.Name)
log.Fatal("failed to upsert function", "error", err.Error())
}
}
1 change: 0 additions & 1 deletion examples/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Prepare:
script:
- ln -s /cbef \${CI_PROJECT_DIR}/cbef
- export CONFIG_FILE=./functions/\${FUNCTION}.json
- export EXECUTION_TIMEOUT=3m
- ./cbef
EOF
Expand Down
7 changes: 7 additions & 0 deletions examples/functions/advanced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function OnUpdate(doc, meta) {
log("Doc created/updated", meta.id);
}

function OnDelete(meta, options) {
log("Doc deleted/expired", meta.id);
}
85 changes: 85 additions & 0 deletions examples/functions/advanced.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"name": "advanced",
"cluster": {
"connection_string": "localhost",
"user": "foo",
"pass": "bar"
},
"source_keyspace": {
"bucket": "foo",
"scope": "_default",
"collection": "_default"
},
"metadata_keyspace": {
"bucket": "bar",
"scope": "_default",
"collection": "_default"
},
"bucket_bindings": [
{
"alias": "foobucket",
"bucket": "foobucket",
"scope": "fooscope",
"collection": "foocollection",
"access": "rw"
},
{
"alias": "barbucket",
"bucket": "barbucket",
"scope": "barscope",
"collection": "barcollection",
"access": "r"
}
],
"url_bindings": [
{
"hostname": "https://www.trendyol.com",
"alias": "trendyol",
"allow_cookies": true,
"validate_ssl_certificate": true,
"auth": {
"type": "basic",
"user": "foo",
"pass": "bar"
}
},
{
"hostname": "https://github.com",
"alias": "github",
"allow_cookies": true,
"validate_ssl_certificate": true,
"auth": {
"type": "digest",
"user": "foo",
"pass": "bar"
}
},
{
"hostname": "https://www.google.com",
"alias": "google",
"allow_cookies": true,
"validate_ssl_certificate": true,
"auth": {
"type": "bearer",
"token": "foo",
"pass": "bar"
}
}
],
"constant_bindings": [
{
"alias": "foo",
"literal": "bar"
}
],
"settings": {
"dcp_stream_boundary": "from_now",
"description": "foo",
"log_level": "TRACE",
"query_consistency": 2,
"worker_count": 5,
"language_compatibility": "6.6.2",
"execution_timeout": 60,
"timer_context_size": 1024
}
}
2 changes: 1 addition & 1 deletion examples/functions/basic.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "basic",
"cluster": {
"connection_string": "couchbase://localhost",
"connection_string": "localhost",
"user": "foo",
"pass": "bar"
},
Expand Down
7 changes: 7 additions & 0 deletions examples/functions/withenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function OnUpdate(doc, meta) {
log("Doc created/updated", meta.id);
}

function OnDelete(meta, options) {
log("Doc deleted/expired", meta.id);
}
18 changes: 18 additions & 0 deletions examples/functions/withenv.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "withenv",
"cluster": {
"connection_string": "{{PROD_CB_HOST}}",
"user": "{{PROD_CB_USER}}",
"pass": "{{PROD_CB_PASS}}"
},
"source_keyspace": {
"bucket": "foo",
"scope": "_default",
"collection": "_default"
},
"metadata_keyspace": {
"bucket": "bar",
"scope": "_default",
"collection": "_default"
}
}
18 changes: 18 additions & 0 deletions examples/functions/withenv.stage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "withenv",
"cluster": {
"connection_string": "{{STAGE_CB_HOST}}",
"user": "{{STAGE_CB_USER}}",
"pass": "{{STAGE_CB_PASS}}"
},
"source_keyspace": {
"bucket": "foo",
"scope": "_default",
"collection": "_default"
},
"metadata_keyspace": {
"bucket": "bar",
"scope": "_default",
"collection": "_default"
}
}
7 changes: 7 additions & 0 deletions examples/functions/withvariable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function OnUpdate(doc, meta) {
log("Doc created/updated", meta.id);
}

function OnDelete(meta, options) {
log("Doc deleted/expired", meta.id);
}
18 changes: 18 additions & 0 deletions examples/functions/withvariable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "withvariable",
"cluster": {
"connection_string": "{{FOO_ENV_VAR}}",
"user": "{{BAR_ENV_VAR}}",
"pass": "{{ANY_ENV_VAR}}"
},
"source_keyspace": {
"bucket": "foo",
"scope": "_default",
"collection": "_default"
},
"metadata_keyspace": {
"bucket": "bar",
"scope": "_default",
"collection": "_default"
}
}
2 changes: 1 addition & 1 deletion internal/action/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func (a *action) Upsert(ctx context.Context, f *model.Function) error {
DCPStreamBoundary: gocb.EventingFunctionDCPBoundary(f.Settings.DCPStreamBoundary),
Description: f.Settings.Description,
LogLevel: gocb.EventingFunctionLogLevel(f.Settings.LogLevel),
ExecutionTimeout: f.Settings.ExecutionTimeout,
QueryConsistency: gocb.QueryScanConsistency(f.Settings.QueryConsistency),
WorkerCount: int(f.Settings.WorkerCount),
LanguageCompatibility: gocb.EventingFunctionLanguageCompatibility(f.Settings.LanguageCompatibility),
ExecutionTimeout: f.Settings.ExecutionTimeout,
TimerContextSize: int(f.Settings.TimerContextSize),
DeploymentStatus: gocb.EventingFunctionDeploymentStatusDeployed,
ProcessingStatus: gocb.EventingFunctionProcessingStatusRunning,
Expand Down
2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func New() *Logger {

// Fatal logs fatal reasons with exit.
func (l *Logger) Fatal(msg string, args ...any) {
l.Error(msg, args) //nolint:all
l.Error(msg, args...)
os.Exit(1)
}
35 changes: 6 additions & 29 deletions internal/model/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,25 @@ import (
"fmt"
"os"
"strings"
"time"
)

// Environment represents application configurations.
type Environment struct {
ConfigFile string
ExecutionTimeout time.Duration
CommitAuthor string
ConfigFile string
CommitAuthor string
}

// NewEnvironment creates a Environment struct via environment vriables.
func NewEnvironment() (*Environment, error) {
e := &Environment{}
if err := e.fill(); err != nil {
return nil, fmt.Errorf("fill environment variables: %s", err.Error())
}

if err := e.validate(); err != nil {
return nil, fmt.Errorf("validate environment variables: %s", err.Error())
}

return e, nil
}

// fill fills and validates Environment struct via environment variables.
func (e *Environment) fill() error {
var e Environment
e.ConfigFile = os.Getenv("CONFIG_FILE")
e.CommitAuthor = os.Getenv("CI_COMMIT_AUTHOR")
timeout := os.Getenv("EXECUTION_TIMEOUT")

if len(timeout) == 0 {
timeout = "3m"
}

t, err := time.ParseDuration(timeout)
if err != nil {
return fmt.Errorf("failed to parse execution timeout duration: %w", err)
if err := e.validate(); err != nil {
return nil, fmt.Errorf("validate environment variables: %s", err.Error())
}

e.ExecutionTimeout = t

return nil
return &e, nil
}

// validate validates provided environment variable requirements.
Expand Down

0 comments on commit d81ac46

Please sign in to comment.