Esempio n. 1
0
func (cfg *Config) Load(c *cli.Context) {
	// Enable debug.
	if c.GlobalBool("debug") {
		log.SetDebug()
	}

	// Default path for config file.
	u, _ := user.Current()
	cfgs := []string{
		u.HomeDir + "/.etcdrest.json",
		u.HomeDir + "/.etcdrest.yaml",
		u.HomeDir + "/.etcdrest.yml",
		u.HomeDir + "/.etcdrest.toml",
		u.HomeDir + "/.etcdrest.tml",
		"/etc/etcdrest.json",
		"/etc/etcdrest.yaml",
		"/etc/etcdrest.yml",
		"/etc/etcdrest.toml",
		"/etc/etcdrest.tml",
		"/app/etc/etcdrest.json",
		"/app/etc/etcdrest.yaml",
		"/app/etc/etcdrest.yml",
		"/app/etc/etcdrest.toml",
		"/app/etc/etcdrest.tml",
	}

	// Check if we have an arg. for config file and that it exist's.
	if c.GlobalString("config") != "" {
		if _, err := os.Stat(c.GlobalString("config")); os.IsNotExist(err) {
			log.Fatalf("Config file doesn't exist: %s", c.GlobalString("config"))
		}
		cfgs = append([]string{c.GlobalString("config")}, cfgs...)
	}

	for _, fn := range cfgs {
		if _, err := os.Stat(fn); os.IsNotExist(err) {
			continue
		}

		log.Infof("Using config file: %s", fn)

		// Load config file.
		b, err := ioutil.ReadFile(fn)
		if err != nil {
			log.Fatal(err.Error())
		}

		switch filepath.Ext(fn) {
		case ".json":
			if err := json.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		case ".yaml", ".yml":
			if err := yaml.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		case ".toml", ".tml":
			if err := toml.Unmarshal(b, cfg); err != nil {
				log.Fatal(err.Error())
			}
		default:
			log.Fatal("unsupported data format")
		}

		// Validate config using JSON schema.

		break
	}

	// Override configuration.
	if c.GlobalString("templ-dir") != "" {
		cfg.TemplDir = c.GlobalString("templ-dir")
	}

	if c.GlobalString("schema-uri") != "" {
		cfg.SchemaURI = c.GlobalString("schema-uri")
	}

	if c.GlobalString("bind") != "" {
		cfg.Bind = c.GlobalString("bind")
	}

	if c.GlobalString("server-uri") != "" {
		cfg.ServerURI = c.GlobalString("server-uri")
	}

	if c.GlobalBool("envelope") {
		cfg.Envelope = true
	}

	if c.GlobalBool("no-indent") {
		cfg.Indent = true
	}

	// Override etcd configuration.
	if c.GlobalString("peers") != "" {
		cfg.Etcd.Peers = c.GlobalString("peers")
	}

	if c.GlobalString("cert") != "" {
		cfg.Etcd.Cert = c.GlobalString("cert")
	}

	if c.GlobalString("key") != "" {
		cfg.Etcd.Key = c.GlobalString("key")
	}

	if c.GlobalString("ca") != "" {
		cfg.Etcd.CA = c.GlobalString("ca")
	}

	if c.GlobalString("user") != "" {
		cfg.Etcd.User = c.GlobalString("user")
	}

	if c.GlobalDuration("timeout") != 0 {
		cfg.Etcd.Timeout = c.GlobalDuration("timeout")
	}

	if c.GlobalDuration("command-timeout") != 0 {
		cfg.Etcd.CmdTimeout = c.GlobalDuration("command-timeout")
	}
}
Esempio n. 2
0
func runServer(c *cli.Context, cfg *config.Config) {
	// Set debug.
	if c.GlobalBool("debug") {
		log.SetDebug()
	}

	cfg.Load(c)

	// Print configuration.
	if c.GlobalIsSet("print-config") {
		cfg.Print(c.GlobalString("print-config"))
		os.Exit(0)
	}

	// Create etcd config.
	ec := etcd.New()
	ec.Peers(cfg.Etcd.Peers)
	ec.Cert(cfg.Etcd.Cert)
	ec.Key(cfg.Etcd.Key)
	ec.CA(cfg.Etcd.CA)
	ec.Timeout(cfg.Etcd.Timeout)
	ec.CmdTimeout(cfg.Etcd.CmdTimeout)

	// If user is set ask for password.
	if cfg.Etcd.User != "" {
		ec.User(cfg.Etcd.User)
		pass, err := speakeasy.Ask("Password: "******"api":
			sc.RouteEtcd(route.Collection, route.CollectionPath, route.Resource, route.ResourcePath, route.Schema, route.DirName)
		case "template":
			sc.RouteTemplate(route.Endpoint, route.Template)
		case "static":
			sc.RouteStatic(route.Endpoint, route.Path)
		default:
			log.Fatalf("Unknown type: %s for endpoint: %s", route.Type, route.Endpoint)
		}
	}

	// Check routes.
	if len(cfg.Routes) < 1 {
		log.Fatal("No routes specified.")
	}

	// Start server.
	if err := sc.Run(); err != nil {
		log.Fatal(err.Error())
	}
}