github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/rc/internal.go (about)

     1  // Define the internal rc functions
     2  
     3  package rc
     4  
     5  import (
     6  	"context"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/ncw/rclone/fs"
    11  	"github.com/ncw/rclone/fs/config/obscure"
    12  	"github.com/ncw/rclone/fs/version"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func init() {
    17  	Add(Call{
    18  		Path:         "rc/noopauth",
    19  		AuthRequired: true,
    20  		Fn:           rcNoop,
    21  		Title:        "Echo the input to the output parameters requiring auth",
    22  		Help: `
    23  This echoes the input parameters to the output parameters for testing
    24  purposes.  It can be used to check that rclone is still alive and to
    25  check that parameter passing is working properly.`,
    26  	})
    27  	Add(Call{
    28  		Path:  "rc/noop",
    29  		Fn:    rcNoop,
    30  		Title: "Echo the input to the output parameters",
    31  		Help: `
    32  This echoes the input parameters to the output parameters for testing
    33  purposes.  It can be used to check that rclone is still alive and to
    34  check that parameter passing is working properly.`,
    35  	})
    36  }
    37  
    38  // Echo the input to the output parameters
    39  func rcNoop(ctx context.Context, in Params) (out Params, err error) {
    40  	return in, nil
    41  }
    42  
    43  func init() {
    44  	Add(Call{
    45  		Path:  "rc/error",
    46  		Fn:    rcError,
    47  		Title: "This returns an error",
    48  		Help: `
    49  This returns an error with the input as part of its error string.
    50  Useful for testing error handling.`,
    51  	})
    52  }
    53  
    54  // Return an error regardless
    55  func rcError(ctx context.Context, in Params) (out Params, err error) {
    56  	return nil, errors.Errorf("arbitrary error on input %+v", in)
    57  }
    58  
    59  func init() {
    60  	Add(Call{
    61  		Path:  "rc/list",
    62  		Fn:    rcList,
    63  		Title: "List all the registered remote control commands",
    64  		Help: `
    65  This lists all the registered remote control commands as a JSON map in
    66  the commands response.`,
    67  	})
    68  }
    69  
    70  // List the registered commands
    71  func rcList(ctx context.Context, in Params) (out Params, err error) {
    72  	out = make(Params)
    73  	out["commands"] = Calls.List()
    74  	return out, nil
    75  }
    76  
    77  func init() {
    78  	Add(Call{
    79  		Path:  "core/pid",
    80  		Fn:    rcPid,
    81  		Title: "Return PID of current process",
    82  		Help: `
    83  This returns PID of current process.
    84  Useful for stopping rclone process.`,
    85  	})
    86  }
    87  
    88  // Return PID of current process
    89  func rcPid(ctx context.Context, in Params) (out Params, err error) {
    90  	out = make(Params)
    91  	out["pid"] = os.Getpid()
    92  	return out, nil
    93  }
    94  
    95  func init() {
    96  	Add(Call{
    97  		Path:  "core/memstats",
    98  		Fn:    rcMemStats,
    99  		Title: "Returns the memory statistics",
   100  		Help: `
   101  This returns the memory statistics of the running program.  What the values mean
   102  are explained in the go docs: https://golang.org/pkg/runtime/#MemStats
   103  
   104  The most interesting values for most people are:
   105  
   106  * HeapAlloc: This is the amount of memory rclone is actually using
   107  * HeapSys: This is the amount of memory rclone has obtained from the OS
   108  * Sys: this is the total amount of memory requested from the OS
   109    * It is virtual memory so may include unused memory
   110  `,
   111  	})
   112  }
   113  
   114  // Return the memory statistics
   115  func rcMemStats(ctx context.Context, in Params) (out Params, err error) {
   116  	out = make(Params)
   117  	var m runtime.MemStats
   118  	runtime.ReadMemStats(&m)
   119  	out["Alloc"] = m.Alloc
   120  	out["TotalAlloc"] = m.TotalAlloc
   121  	out["Sys"] = m.Sys
   122  	out["Mallocs"] = m.Mallocs
   123  	out["Frees"] = m.Frees
   124  	out["HeapAlloc"] = m.HeapAlloc
   125  	out["HeapSys"] = m.HeapSys
   126  	out["HeapIdle"] = m.HeapIdle
   127  	out["HeapInuse"] = m.HeapInuse
   128  	out["HeapReleased"] = m.HeapReleased
   129  	out["HeapObjects"] = m.HeapObjects
   130  	out["StackInuse"] = m.StackInuse
   131  	out["StackSys"] = m.StackSys
   132  	out["MSpanInuse"] = m.MSpanInuse
   133  	out["MSpanSys"] = m.MSpanSys
   134  	out["MCacheInuse"] = m.MCacheInuse
   135  	out["MCacheSys"] = m.MCacheSys
   136  	out["BuckHashSys"] = m.BuckHashSys
   137  	out["GCSys"] = m.GCSys
   138  	out["OtherSys"] = m.OtherSys
   139  	return out, nil
   140  }
   141  
   142  func init() {
   143  	Add(Call{
   144  		Path:  "core/gc",
   145  		Fn:    rcGc,
   146  		Title: "Runs a garbage collection.",
   147  		Help: `
   148  This tells the go runtime to do a garbage collection run.  It isn't
   149  necessary to call this normally, but it can be useful for debugging
   150  memory problems.
   151  `,
   152  	})
   153  }
   154  
   155  // Do a garbage collection run
   156  func rcGc(ctx context.Context, in Params) (out Params, err error) {
   157  	runtime.GC()
   158  	return nil, nil
   159  }
   160  
   161  func init() {
   162  	Add(Call{
   163  		Path:  "core/version",
   164  		Fn:    rcVersion,
   165  		Title: "Shows the current version of rclone and the go runtime.",
   166  		Help: `
   167  This shows the current version of go and the go runtime
   168  - version - rclone version, eg "v1.44"
   169  - decomposed - version number as [major, minor, patch, subpatch]
   170      - note patch and subpatch will be 999 for a git compiled version
   171  - isGit - boolean - true if this was compiled from the git version
   172  - os - OS in use as according to Go
   173  - arch - cpu architecture in use according to Go
   174  - goVersion - version of Go runtime in use
   175  
   176  `,
   177  	})
   178  }
   179  
   180  // Return version info
   181  func rcVersion(ctx context.Context, in Params) (out Params, err error) {
   182  	decomposed, err := version.New(fs.Version)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	out = Params{
   187  		"version":    fs.Version,
   188  		"decomposed": decomposed,
   189  		"isGit":      decomposed.IsGit(),
   190  		"os":         runtime.GOOS,
   191  		"arch":       runtime.GOARCH,
   192  		"goVersion":  runtime.Version(),
   193  	}
   194  	return out, nil
   195  }
   196  
   197  func init() {
   198  	Add(Call{
   199  		Path:  "core/obscure",
   200  		Fn:    rcObscure,
   201  		Title: "Obscures a string passed in.",
   202  		Help: `
   203  Pass a clear string and rclone will obscure it for the config file:
   204  - clear - string
   205  
   206  Returns
   207  - obscured - string
   208  `,
   209  	})
   210  }
   211  
   212  // Return obscured string
   213  func rcObscure(ctx context.Context, in Params) (out Params, err error) {
   214  	clear, err := in.GetString("clear")
   215  	if err != nil {
   216  		return nil, err
   217  	}
   218  	obscured, err := obscure.Obscure(clear)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  	out = Params{
   223  		"obscured": obscured,
   224  	}
   225  	return out, nil
   226  }