cmd

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2019 License: GPL-3.0 Imports: 25 Imported by: 1

Documentation

Overview

Package cmd registers cobra commands for CLI tool. Some commands use an "app" build tag to speed up compilation while developing by ignoring UX specific dependencies (systray, webview)

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cobra.Command{
	Use:   "add",
	Short: "Add a new sync",
	Long: `Define a new sync task using two URI and a direction.

Endpoint URI support the following schemes: 
 - router: Direct connexion to Cells server running on the same machine
 - fs:     Path to a local folder
 - s3:     S3 compliant
 - memdb:  In-memory DB for testing purposes

Direction can be:
 - Bi:     Bidirectionnal sync between two endpoints
 - Left:   Changes are only propagated from right to left
 - Right:  Changes are only propagated from left to right

Example
 - LeftUri : "router:///personal/admin/folder"
 - RightUri: "fs:///Users/name/Pydio/folder"
 - Direction: "Bi"

`,
	Run: func(cmd *cobra.Command, args []string) {

		t := &config.Task{
			Uuid: uuid.New(),
		}
		var e error
		l := &promptui.Prompt{Label: "Left endpoint URI"}
		r := &promptui.Prompt{Label: "Right endpoint URI"}
		s := promptui.Select{Label: "Sync Direction", Items: []string{"Bi", "Left", "Right"}}
		t.LeftURI, e = l.Run()
		if e != nil {
			exit(e)
		}
		t.RightURI, e = r.Run()
		if e != nil {
			exit(e)
		}
		_, t.Direction, e = s.Run()
		if e != nil {
			exit(e)
		}

		config.Default().Tasks = append(config.Default().Tasks, t)
		er := config.Save()
		if er != nil {
			exit(er)
		}

	},
}
View Source
var AutoTestCmd = &cobra.Command{
	Use:  "autotest",
	Long: "Performs a simple test of a unidirectional sync between two local folders (created inside TmpDir)",
	Run: func(cmd *cobra.Command, args []string) {

		tmpDir := os.TempDir()
		syncId := uuid.New()
		leftDir := filepath.Join(tmpDir, syncId, "left")
		os.MkdirAll(leftDir, 0777)
		rightDir := filepath.Join(tmpDir, syncId, "right")
		os.MkdirAll(rightDir, 0777)

		if !autoTestSkipClean {
			defer func() {
				fmt.Println("Cleaning resources after test (" + filepath.Join(tmpDir, syncId) + ")")
				os.RemoveAll(filepath.Join(tmpDir, syncId))
			}()
		}

		left, e := filesystem.NewFSClient(leftDir, model.EndpointOptions{})
		if e != nil {
			log.Fatal("cannot init left dir " + leftDir)
		}

		if e := ioutil.WriteFile(filepath.Join(leftDir, "basic-file.txt"), []byte("hello world"), 0755); e != nil {
			log.Fatal("cannot write a file for testing on left " + leftDir)
		}

		right, e := filesystem.NewFSClient(rightDir, model.EndpointOptions{})
		if e != nil {
			log.Fatal("cannot init right dir " + rightDir)
		}

		statusChan := make(chan model.Status)
		doneChan := make(chan interface{})
		waitChan := make(chan bool, 1)
		var err error

		syncTask := task.NewSync(left, right, model.DirectionRight)
		syncTask.SkipTargetChecks = true
		syncTask.Start(context.Background(), false)
		syncTask.SetupEventsChan(statusChan, doneChan, nil)

		go func() {
			defer func() {
				close(waitChan)
			}()
			for {
				select {
				case status := <-statusChan:
					fmt.Println("Task Status Received: ", status)
				case p := <-doneChan:
					if patch, ok := p.(merger.Patch); ok {
						fmt.Println("Patch Processing Finished")
						fmt.Println(patch.Stats())

						if patch.Size() == 0 {
							err = fmt.Errorf("processed patch has size 0, this is not normal")
							return
						}
						if data, e := ioutil.ReadFile(filepath.Join(rightDir, "basic-file.txt")); e != nil {
							err = fmt.Errorf("cannot read right file %v", e)
						} else if string(data) != "hello world" {
							err = fmt.Errorf("right file does not have proper content: %v", string(data))
						}
					} else {
						err = fmt.Errorf("doneChan did not send a patch")
					}
					return
				case <-time.After(30 * time.Second):
					err = fmt.Errorf("breaking test after 30s, this is not normal")
					return
				}
			}
		}()
		syncTask.Run(context.Background(), false, true)
		<-waitChan
		syncTask.Shutdown()
		if err != nil {
			log.Fatal(err)
		}
	},
}
View Source
var CaptureCmd = &cobra.Command{
	Use:   "capture",
	Short: "Capture snapshots inside JSON file - do not perform any actual tasks",
	Run: func(cmd *cobra.Command, args []string) {

		if captureTarget == "" {
			log.Fatal("Please provide a target folder for storing JSON files")
		}

		ctx := servicecontext.WithServiceName(context.Background(), "supervisor")
		ctx = servicecontext.WithServiceColor(ctx, servicecontext.ServiceColorRest)

		conf := config.Default()
		if len(conf.Tasks) > 0 {
			for _, t := range conf.Tasks {

				leftEndpoint, err := endpoint.EndpointFromURI(t.LeftURI, t.RightURI)
				if err != nil {
					log.Fatal(err.Error())
				}
				rightEndpoint, err := endpoint.EndpointFromURI(t.RightURI, t.LeftURI)
				if err != nil {
					log.Fatal(err.Error())
				}

				var dir model.DirectionType
				switch t.Direction {
				case "Bi":
					dir = model.DirectionBi
				case "Left":
					dir = model.DirectionLeft
				case "Right":
					dir = model.DirectionRight
				default:
					log.Fatal("unsupported direction type, please use one of Bi, Left, Right")
				}

				syncTask := task.NewSync(leftEndpoint, rightEndpoint, dir)
				syncTask.SetSnapshotFactory(endpoint.NewSnapshotFactory(t.Uuid, leftEndpoint, rightEndpoint))
				e := syncTask.Capture(ctx, captureTarget)
				if e != nil {
					log.Fatal(e)
				} else {
					fmt.Println("Captured snapshots inside " + captureTarget)
				}

			}
		}

	},
}
View Source
var DeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Delete existing sync",
	Run: func(cmd *cobra.Command, args []string) {
		tS := promptui.Select{Label: "Select Sync to Edit", Items: config.Default().Items()}
		i, _, e := tS.Run()
		if e != nil {
			exit(e)
		}
		tasks := config.Default().Tasks
		last := len(tasks) - 1
		lastTask := tasks[last]
		tasks[last] = tasks[i]
		tasks[i] = lastTask
		tasks = tasks[:last]
		config.Default().Tasks = tasks
		er := config.Save()
		if er != nil {
			exit(er)
		}

	},
}
View Source
var EditCmd = &cobra.Command{
	Use:   "edit",
	Short: "Exit existing sync",
	Run: func(cmd *cobra.Command, args []string) {
		tS := promptui.Select{Label: "Select Sync to Edit", Items: config.Default().Items()}
		i, _, e := tS.Run()
		if e != nil {
			exit(e)
		}

		task := config.Default().Tasks[i]
		l := &promptui.Prompt{Label: "Left endpoint URI", Default: task.LeftURI}
		r := &promptui.Prompt{Label: "Right endpoint URI", Default: task.RightURI}
		s := promptui.Select{Label: "Sync Direction", Items: []string{"Bi", "Left", "Right"}}
		task.LeftURI, e = l.Run()
		if e != nil {
			exit(e)
		}
		task.RightURI, e = r.Run()
		if e != nil {
			exit(e)
		}
		_, task.Direction, e = s.Run()
		if e != nil {
			exit(e)
		}
		er := config.Save()
		if er != nil {
			exit(er)
		}
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   os.Args[0],
	Short: "Cells Sync desktop client",
	Long:  `Cells Sync Desktop Client`,
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		handleSignals()
	},
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Usage()
	},
}
View Source
var StartCmd = &cobra.Command{
	Use:   "start",
	Short: "Start sync tasks",
	Run: func(cmd *cobra.Command, args []string) {

		logs := config.Default().Logs
		os.MkdirAll(logs.Folder, 0755)
		log.RegisterWriteSyncer(zapcore.AddSync(&lumberjack.Logger{
			Filename:   filepath.Join(logs.Folder, "sync.log"),
			MaxAge:     logs.MaxAgeDays,
			MaxSize:    logs.MaxFilesSize,
			MaxBackups: logs.MaxFilesNumber,
		}))

		s := control.NewSupervisor()
		s.Serve()

	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Display version",
	Run: func(cmd *cobra.Command, args []string) {
		common.PrintVersion("Cells Sync")
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL