1// @author Couchbase <info@couchbase.com>
2// @copyright 2014 Couchbase, Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16package main
17
18import (
19	"flag"
20	"github.com/couchbase/gometa/log"
21	"github.com/couchbase/gometa/server"
22	"os"
23)
24
25// Temporary dependency until logging moves out. Avoids stub methods.
26import "github.com/couchbase/indexing/secondary/logging"
27
28func stdinWatcher() {
29	b := make([]byte, 1)
30
31	for {
32		_, err := os.Stdin.Read(b)
33		if err != nil {
34			log.Current.Errorf("Got %s when reading stdin. Terminating.", err.Error())
35			break
36		}
37
38		if b[0] == '\n' || b[0] == '\r' {
39			log.Current.Errorf("Got new line on a stdin. Terminating.")
40			break
41		}
42	}
43
44	os.Exit(0)
45}
46
47//
48// main function
49//
50func main() {
51	var isClient bool
52	var isWatcher bool
53	var config string
54	var watchStdin bool
55
56	// Temporary dependency until logging moves out. Avoids stub methods.
57	log.Current = &logging.SystemLogger
58
59	flag.BoolVar(&isClient, "client", false, "run as test client")
60	flag.BoolVar(&isWatcher, "watcher", false, "run as watcher")
61	flag.StringVar(&config, "config", "", "path for configuration file")
62	flag.BoolVar(&watchStdin, "watch-stdin", true,
63		"watch standard input and terminate on EOL or EOF")
64	flag.Parse()
65
66	if isClient {
67		runTestClient(config)
68		os.Exit(0)
69	}
70
71	if isWatcher {
72		runWatcher(config)
73		os.Exit(0)
74	}
75
76	if watchStdin {
77		go stdinWatcher()
78	}
79
80	err := server.RunServer(config)
81	if err != nil {
82		log.Current.Errorf("Encounter Error = %s. Terminate server", err.Error())
83		os.Exit(1)
84	}
85
86	os.Exit(0)
87}
88