Beispiel #1
0
func init() {
	flagset := flag.NewFlagSet("config", flag.ContinueOnError)
	flagset.StringVar(&configFile, "config", "", "Config File")
	flagset.StringVar(&uuid, "uuid", "", "uuid")

	args, _ := SplitFlagsetFromArgs(flagset, os.Args[1:])
	flagset.Parse(args)

	// Ensure we have a UUID
	if uuid == "" {
		uuid = NewUUID()
	}

	if configFile == "" {
		for _, f := range defaultConfigFiles {
			if _, err := os.Stat(f); err == nil {
				configFile = f
				break
			}
		}
	}

	if configFile == "" {
		log.Println(log.ERROR, "Failed to find config file")
		conf = config.NewDefault()
		return
	}

	if _, err := os.Stat(configFile); os.IsNotExist(err) {
		log.Println(log.ERROR, "Config file does not exist", err)
		conf = config.NewDefault()
		return
	}

	var err error
	if conf, err = config.ReadDefault(configFile); err != nil {
		conf = config.NewDefault()
		log.Fatal(err)
	}

	// Set default log level from config, this can be overriden at the service level when the service is created
	if l, err := conf.RawStringDefault("log.level"); err == nil {
		log.SetLogLevel(log.LevelFromString(l))
	}

	// Set default log level from config, this can be overriden at the service level when the service is created
	if lh, err := conf.RawStringDefault("log.sysloghost"); err == nil {
		log.SetSyslogHost(lh)
	}

	// Set syslog port
	if i, err := conf.Int("DEFAULT", "log.syslogport"); err == nil {
		log.SetSyslogPort(i)
	}

	// Set GOMAXPROCS
	if i, err := conf.Int("DEFAULT", "runtime.gomaxprocs"); err == nil {
		runtime.GOMAXPROCS(i)
	}
}
func register(ctx *cli.Context) {

	fn := path.Join(ctx.String("directory"), ".gomr")

	newTags := strings.Split(ctx.String("tag"), ",")

	if ctx.Bool("append") {
		if _, err := os.Stat(fn); err == nil {
			cfg, _ := config.ReadDefault(".gomr")
			curTags, _ := cfg.String("gomr", "tags")

			for _, tag := range strings.Split(curTags, ",") {
				newTags = AppendIfMissing(newTags, tag)
			}
		} else {
			err := "append used, existing file not found."
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			os.Exit(1)
		}
	}

	outTags := strings.Join(newTags, ",")

	outCfg := config.NewDefault()
	outCfg.AddSection("gomr")
	outCfg.AddOption("gomr", "tags", outTags)
	outCfg.WriteFile(fn, 0644, "gomr configuration file")

	return
}
Beispiel #3
0
func NewConfigPlugin(filename string) *ConfigPlugin {
	c, ok := config.ReadDefault(filename)
	if ok != nil {
		c = config.NewDefault()
		c.AddSection("Server")
		c.AddOption("Server", "host", "dpaulus.dyndns.org:6667")
		c.AddOption("Server", "nick", "testbot")
		c.AddOption("Server", "ident", "ident")
		c.AddOption("Server", "realname", "TestBot Client")
		c.AddOption("Server", "trigger", ".")
		c.AddSection("Auth")
		c.AddSection("Info")
		c.WriteFile(filename, 0644, "IRC Bot default config file")
		log.Println("Note: A new default configuration file has been generated in " + filename + ". Please edit it to suit your needs and restart the bot then")
		os.Exit(1)
	}
	for _, x := range []string{"host", "nick", "ident", "realname"} {
		_, err := c.String("Server", x)
		if err != nil {
			log.Fatal("Error while parsing config: " + err.Error())
		}
	}
	trigger, err := c.String("Server", "trigger")
	if err != nil {
		log.Fatal(err)
	}
	if utf8.RuneCountInString(trigger) != 1 {
		log.Fatal("Trigger must be exactly one unicode rune long")
	}
	return &ConfigPlugin{filename: filename, Conf: c}
}
Beispiel #4
0
func UpdateMsg(locale, key, value string) {
	_, region := parseLocale(locale)
	c := config.NewDefault()
	b := c.AddOption(region, key, value)
	if !b {
		logger.Infof("Put Msg faild. Key: %s Value: %s", key, value)
	}
	//
	PutConfig(locale, c)
}
Beispiel #5
0
func ReadConfigFromString(v string) (*config.Config, error) {
	var buf *bufio.Reader = bufio.NewReader(strings.NewReader(v))
	c := config.NewDefault()
	var section, option string
	for {
		l, err := buf.ReadString('\n') // parse line-by-line
		if err == io.EOF {
			break
		} else if err != nil {
			return c, err
		}

		l = strings.TrimSpace(l)

		// Switch written for readability (not performance)
		switch {
		// Empty line and comments
		case len(l) == 0, l[0] == '#', l[0] == ';':
			continue

		// New section
		case l[0] == '[' && l[len(l)-1] == ']':
			option = "" // reset multi-line value
			section = strings.TrimSpace(l[1 : len(l)-1])
			c.AddSection(section)

		// No new section and no section defined so
		//case section == "":
		//return os.NewError("no section defined")

		// Other alternatives
		default:
			i := strings.IndexAny(l, "=:")

			switch {
			// Option and value
			case i > 0:
				i := strings.IndexAny(l, "=:")
				option = strings.TrimSpace(l[0:i])
				value := strings.TrimSpace(stripComments(l[i+1:]))
				c.AddOption(section, option, value)
			// Continuation of multi-line value
			case section != "" && option != "":
				prev, _ := c.RawString(section, option)
				value := strings.TrimSpace(stripComments(l))
				c.AddOption(section, option, prev+"\n"+value)

			default:
				return c, errors.New("could not parse line: " + l)
			}
		}
	}
	return c, nil
}
Beispiel #6
0
func main() {
	flag.Parse()

	conf, err := config.ReadDefault(*configFile)
	if err != nil {
		conf = config.NewDefault()
		fmt.Printf("Error loading config file")
	}

	log.SetFlags(log.Ldate | log.Ltime)
	logDir, _ := conf.String("DEFAULT", "log_dir")
	if logDir == "" {
		logDir = "."
	}

	filePrefix := "twitch_stats-"
	fnTime := time.Now().UTC().Format("200601")

	logFile := fmt.Sprintf("%s/%s%s.log", logDir, filePrefix, fnTime)
	fp, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_WRONLY, 0644)
	if err != nil {
		fmt.Printf("Failed to open log file '%s': %s", logFile, err)
	}

	log.SetOutput(fp)

	keyid, _ := conf.Int("DEFAULT", "KeyID")
	vcode, _ := conf.String("DEFAULT", "vCode")
	listenOn, _ := conf.String("DEFAULT", "listen")

	initDB()

	//Setup the timer. This pulls notifications from the API every 21 minutes. The cache timer is 20 minutes, so it should be ok.
	//Some endeavouring dev could probably modify this so that it gets the cache timer from the API and checks at that time, but :effort:
	t := time.NewTicker(time.Minute * 21).C
	go func() {
		for {
			select {
			case <-t:
				getNotifications(keyid, vcode)
			}
		}
	}()

	//This is the part that listens and handles the incoming http connections.
	http.HandleFunc("/", getStats)

	//This part is so that the static files actually load, mainly the css and js shit.
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))

	http.ListenAndServe(listenOn, nil)

}
Beispiel #7
0
// NewIniConfig supports more than one location.
// returns IniConfig, implements mcore.Config
func NewIniConfig(locations ...string) *IniConfig {
	c := &IniConfig{Locations: locations}
	conf := config.NewDefault()
	for _, location := range locations {
		confItem, err := config.ReadDefault(location)
		if logger.CheckError(err) {
			continue
		}
		conf.Merge(confItem)
	}
	c.Config = conf
	return c
}
Beispiel #8
0
func main() {
	flag.Parse()

	conf, err := config.ReadDefault(*configFile)
	if err != nil {
		conf = config.NewDefault()
		fmt.Printf("Error loading config file")
	}

	dsn, err = conf.String("DEFAULT", "DSN")
	if err != nil {
		log.Printf("Error reading DSN: %s", err)
	}

	log.SetFlags(log.Ldate | log.Ltime)
	logDir, _ := conf.String("DEFAULT", "log_dir")
	if logDir == "" {
		logDir = "."
	}

	streamChannel, _ = conf.String("DEFAULT", "StreamChannel")
	runTest, _ := conf.Bool("DEFAULT", "TestMode")
	authToken, _ = conf.String("DEFAULT", "AuthToken")
	monitorInterval, _ := conf.Int("DEFAULT", "MonitorInterval")
	monInt := time.Duration(monitorInterval)

	filePrefix := "twitch_stats-"
	fnTime := time.Now().UTC().Format("200601")

	logFile := fmt.Sprintf("%s/%s%s.log", logDir, filePrefix, fnTime)
	fp, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_WRONLY, 0644)
	if err != nil {
		fmt.Printf("Failed to open log file '%s': %s", logFile, err)
	}

	log.SetOutput(fp)

	log.Printf("Starting monitoring for '%s' with an interval of '%d' seconds.", streamChannel, monitorInterval)

	initDB()

	if runTest == true {
		test()
	} else {
		run(monInt)
	}
}
Beispiel #9
0
func ItExists() {
	usr, _ := user.Current()
	hubrc = filepath.Join(usr.HomeDir, ".hubrc")

	var err error

	// Load config for application
	conf, err = config.ReadDefault(hubrc)

	if err != nil {
		conf = config.NewDefault()

		conf.AddSection("default")
		conf.AddOption("default", "site", "github.com")
		conf.AddOption("default", "combine", "0")

		conf.WriteFile(hubrc, 0600, "Config for http://github.com/pksunkara/hub")
		conf, _ = config.ReadDefault(hubrc)
	}
}
Beispiel #10
0
func NewEmptyConfig() *MergedConfig {
	return &MergedConfig{config.NewDefault(), ""}
}
Beispiel #11
0
func NewContext() *Context {
	return &Context{config: config.NewDefault()}
}
Beispiel #12
0
func NewConfig() *Config {
	return &Config{ini: config.NewDefault()}
}