github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"runtime/pprof"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/codegangsta/cli"
    11  	"github.com/docker/docker/pkg/reexec"
    12  	"github.com/rancher/longhorn/app"
    13  	"github.com/rancher/longhorn/backup"
    14  	"github.com/rancher/sparse-tools/cli/sfold"
    15  	"github.com/rancher/sparse-tools/cli/ssync"
    16  )
    17  
    18  func main() {
    19  	reexec.Register("ssync", ssync.Main)
    20  	reexec.Register("sfold", sfold.Main)
    21  	reexec.Register("sbackup", backup.Main)
    22  
    23  	if !reexec.Init() {
    24  		longhornCli()
    25  	}
    26  }
    27  
    28  func cmdNotFound(c *cli.Context, command string) {
    29  	panic(fmt.Errorf("Unrecognized command: %s", command))
    30  }
    31  
    32  func onUsageError(c *cli.Context, err error, isSubcommand bool) error {
    33  	panic(fmt.Errorf("Usage error, please check your command"))
    34  }
    35  
    36  func longhornCli() {
    37  	pprofFile := os.Getenv("PPROFILE")
    38  	if pprofFile != "" {
    39  		f, err := os.Create(pprofFile)
    40  		if err != nil {
    41  			log.Fatal(err)
    42  		}
    43  		pprof.StartCPUProfile(f)
    44  		defer pprof.StopCPUProfile()
    45  	}
    46  
    47  	a := cli.NewApp()
    48  	a.Before = func(c *cli.Context) error {
    49  		if c.GlobalBool("debug") {
    50  			logrus.SetLevel(logrus.DebugLevel)
    51  		}
    52  		return nil
    53  	}
    54  	a.Flags = []cli.Flag{
    55  		cli.StringFlag{
    56  			Name:  "url",
    57  			Value: "http://localhost:9501",
    58  		},
    59  		cli.BoolFlag{
    60  			Name: "debug",
    61  		},
    62  	}
    63  	a.Commands = []cli.Command{
    64  		app.ControllerCmd(),
    65  		app.ReplicaCmd(),
    66  		app.SyncAgentCmd(),
    67  		app.AddReplicaCmd(),
    68  		app.LsReplicaCmd(),
    69  		app.RmReplicaCmd(),
    70  		app.SnapshotCmd(),
    71  		app.BackupCmd(),
    72  		app.Journal(),
    73  	}
    74  	a.CommandNotFound = cmdNotFound
    75  	a.OnUsageError = onUsageError
    76  
    77  	if err := a.Run(os.Args); err != nil {
    78  		logrus.Fatal(err)
    79  	}
    80  }