2019-02-12 06:58:24 +00:00
// Copyright (C) 2019 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2021-02-15 17:50:53 +00:00
package cli
2019-02-12 06:58:24 +00:00
import (
2022-01-13 23:38:21 +01:00
"bufio"
2022-04-27 20:30:13 +02:00
"errors"
2019-02-12 06:58:24 +00:00
"fmt"
2022-01-13 23:38:21 +01:00
"path/filepath"
2019-02-12 06:58:24 +00:00
2023-12-11 18:35:57 +08:00
"github.com/alecthomas/kong"
2022-01-13 23:38:21 +01:00
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/fs"
2019-02-12 06:58:24 +00:00
)
2023-12-11 18:35:57 +08:00
type folderOverrideCommand struct {
FolderID string `arg:""`
2019-02-12 06:58:24 +00:00
}
2023-12-11 18:35:57 +08:00
type defaultIgnoresCommand struct {
Path string `arg:""`
}
type operationCommand struct {
Restart struct {} `cmd:"" help:"Restart syncthing"`
Shutdown struct {} `cmd:"" help:"Shutdown syncthing"`
Upgrade struct {} `cmd:"" help:"Upgrade syncthing (if a newer version is available)"`
FolderOverride folderOverrideCommand `cmd:"" help:"Override changes on folder (remote for sendonly, local for receiveonly). WARNING: Destructive - deletes/changes your data"`
DefaultIgnores defaultIgnoresCommand `cmd:"" help:"Set the default ignores (config) from a file"`
}
func ( * operationCommand ) Run ( ctx Context , kongCtx * kong . Context ) error {
f := ctx . clientFactory
switch kongCtx . Selected (). Name {
case "restart" :
return emptyPost ( "system/restart" , f )
case "shutdown" :
return emptyPost ( "system/shutdown" , f )
case "upgrade" :
return emptyPost ( "system/upgrade" , f )
}
return nil
}
func ( f * folderOverrideCommand ) Run ( ctx Context ) error {
client , err := ctx . clientFactory . getClient ()
2021-04-25 21:48:17 +02:00
if err != nil {
return err
}
2019-02-12 06:58:24 +00:00
cfg , err := getConfig ( client )
if err != nil {
return err
}
2023-12-11 18:35:57 +08:00
rid := f . FolderID
2019-02-12 06:58:24 +00:00
for _ , folder := range cfg . Folders {
if folder . ID == rid {
response , err := client . Post ( "db/override" , "" )
if err != nil {
return err
}
if response . StatusCode != 200 {
errStr := fmt . Sprint ( "Failed to override changes\nStatus code: " , response . StatusCode )
bytes , err := responseToBArray ( response )
if err != nil {
return err
}
body := string ( bytes )
if body != "" {
errStr += "\nBody: " + body
}
2022-04-27 20:30:13 +02:00
return errors . New ( errStr )
2019-02-12 06:58:24 +00:00
}
return nil
}
}
2022-04-27 20:30:13 +02:00
return fmt . Errorf ( "Folder %q not found" , rid )
2019-02-12 06:58:24 +00:00
}
2022-01-13 23:38:21 +01:00
2023-12-11 18:35:57 +08:00
func ( d * defaultIgnoresCommand ) Run ( ctx Context ) error {
client , err := ctx . clientFactory . getClient ()
2022-01-13 23:38:21 +01:00
if err != nil {
return err
}
2023-12-11 18:35:57 +08:00
dir , file := filepath . Split ( d . Path )
2022-01-13 23:38:21 +01:00
filesystem := fs . NewFilesystem ( fs . FilesystemTypeBasic , dir )
fd , err := filesystem . Open ( file )
if err != nil {
return err
}
scanner := bufio . NewScanner ( fd )
var lines [] string
for scanner . Scan () {
lines = append ( lines , scanner . Text ())
}
fd . Close ()
if err := scanner . Err (); err != nil {
return err
}
_ , err = client . PutJSON ( "config/defaults/ignores" , config . Ignores { Lines : lines })
return err
}