From 0c265713fdc95e67f2a63c4d2c9994696847f9bf Mon Sep 17 00:00:00 2001 From: Saksham Khanna Date: Thu, 17 Oct 2019 19:34:22 +0530 Subject: [PATCH] rc: added command core/quit --- fs/rc/internal.go | 36 ++++++++++++++++++++++++++++++++++++ fs/rc/internal_test.go | 16 ++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/fs/rc/internal.go b/fs/rc/internal.go index 26a86c008..31dc89ed8 100644 --- a/fs/rc/internal.go +++ b/fs/rc/internal.go @@ -6,11 +6,14 @@ import ( "context" "os" "runtime" + "time" "github.com/pkg/errors" + "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/config/obscure" "github.com/rclone/rclone/fs/version" + "github.com/rclone/rclone/lib/atexit" ) func init() { @@ -224,3 +227,36 @@ func rcObscure(ctx context.Context, in Params) (out Params, err error) { } return out, nil } + +func init() { + Add(Call{ + Path: "core/quit", + Fn: rcQuit, + Title: "Terminates the app.", + Help: ` +(optional) Pass an exit code to be used for terminating the app: +- exitCode - int +`, + }) +} + +// Terminates app +func rcQuit(ctx context.Context, in Params) (out Params, err error) { + code, err := in.GetInt64("exitCode") + + if IsErrParamInvalid(err) { + return nil, err + } + if IsErrParamNotFound(err) { + code = 0 + } + exitCode := int(code) + + go func(exitCode int) { + time.Sleep(time.Millisecond * 1500) + atexit.Run() + os.Exit(exitCode) + }(exitCode) + + return nil, nil +} diff --git a/fs/rc/internal_test.go b/fs/rc/internal_test.go index 7abda3ff7..118215301 100644 --- a/fs/rc/internal_test.go +++ b/fs/rc/internal_test.go @@ -5,11 +5,12 @@ import ( "runtime" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/config/obscure" "github.com/rclone/rclone/fs/version" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestInternalNoop(t *testing.T) { @@ -107,3 +108,14 @@ func TestCoreObscure(t *testing.T) { require.NotNil(t, out) assert.Equal(t, in["clear"], obscure.MustReveal(out["obscured"].(string))) } + +func TestCoreQuit(t *testing.T) { + //The call should return an error if param exitCode is not parsed to int + call := Calls.Get("core/quit") + assert.NotNil(t, call) + in := Params{ + "exitCode": "potato", + } + _, err := call.Fn(context.Background(), in) + require.Error(t, err) +}