Esempio n. 1
0
func parseCfg(configPath string) (cfg Config) {
	var r io.Reader
	r, err := os.OpenFile(configPath, os.O_RDONLY, 0644)
	if err != nil {
		if os.IsNotExist(err) {
			exitf("Config %s doesn't exist or is unspecified.\nTo specify with flag -c (e.g. -c harp.json)", configPath)
		}
		exitf("failed to read config: %s", err)
	}
	if err := json.NewDecoder(JsonConfigReader.New(r)).Decode(&cfg); err != nil {
		exitf("failed to parse config: %s", err)
	}

	if cfg.App.KillSig == "" {
		cfg.App.KillSig = "KILL"
	}

	for k, set := range cfg.Servers {
		for _, s := range set {
			s.Set = k
		}
	}

	if cfg.RollbackCount == 0 {
		cfg.RollbackCount = 3
	}

	cfg.App.DefaultExcludeds = append(cfg.App.DefaultExcludeds, ".harp/")

	if cfg.App.FileWarningSize == 0 {
		cfg.App.FileWarningSize = 1 << 20
	}

	return
}
Esempio n. 2
0
// Loads the json config out of io.Reader, eg other sources than file, maybe over http
func NewCgrJsonCfgFromReader(r io.Reader) (*CgrJsonCfg, error) {
	var cgrJsonCfg CgrJsonCfg
	jr := JsonConfigReader.New(r)
	if err := json.NewDecoder(jr).Decode(&cgrJsonCfg); err != nil {
		return nil, err
	}
	return &cgrJsonCfg, nil
}
Esempio n. 3
0
func loadJson(path string, into interface{}) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	return json.NewDecoder(JsonConfigReader.New(f)).Decode(into)
}
Esempio n. 4
0
func LoadConfig() {
	file, err := os.Open(filename)
	if err != nil {
		log.Fatal("Failed to read config:\n", err)
	}
	reader := JsonConfigReader.New(file)
	json.NewDecoder(reader).Decode(&Config)
	fmt.Println(Config)
}
Esempio n. 5
0
func loadConfig() interface{} {
	// Read the config file.
	var v interface{}
	f, _ := os.Open("~/.fsmonitor.rc")
	// wrap our reader before passing it to the json decoder
	r := JsonConfigReader.New(f)
	json.NewDecoder(r).Decode(&v)
	return v
}
Esempio n. 6
0
// NewUploadersFromFile loads Uploaders structue from .json file
func NewUploadersFromFile(path string) (*Uploaders, error) {
	uploaders := &Uploaders{}
	f, err := os.Open(path)
	if err != nil {
		return nil, fmt.Errorf("error loading uploaders file: %s", err)
	}
	defer f.Close()

	err = json.NewDecoder(JsonConfigReader.New(f)).Decode(&uploaders)
	if err != nil {
		return nil, fmt.Errorf("error loading uploaders file: %s", err)
	}

	return uploaders, nil
}
Esempio n. 7
0
func parseCfg(configPath string) (cfg Config) {
	var r io.Reader
	r, err := os.OpenFile(configPath, os.O_RDONLY, 0644)
	if err != nil {
		if os.IsNotExist(err) {
			fmt.Printf("Config %s doesn't exist or is unspecified.\nTo specify with flag -c (e.g. -c harp.json)\n", configPath)
			os.Exit(1)
			return
		}
		exitf("failed to read config: %s", err)
	}
	r = JsonConfigReader.New(r)
	err = json.NewDecoder(r).Decode(&cfg)
	if err != nil {
		exitf("failed to parse config: %s", err)
	}

	if cfg.App.KillSig == "" {
		cfg.App.KillSig = "KILL"
	}

	for k, set := range cfg.Servers {
		for _, s := range set {
			s.Set = k
			if s.User == "" {
				fmt.Printf("%s contains server with empty user name\n", k)
				os.Exit(1)
			} else if s.Host == "" {
				fmt.Printf("%s contains server with empty host\n", k)
				os.Exit(1)
			}
			if s.Port == "" {
				s.Port = ":22"
			}
		}
	}

	if cfg.RollbackCount == 0 {
		cfg.RollbackCount = 3
	}

	cfg.App.DefaultExcludeds = append(cfg.App.DefaultExcludeds, ".harp/")

	return
}
Esempio n. 8
0
func loadConfigBytes(path string, preprocessCmd []string) (bs []byte, err error) {
	in, err := os.Open(path)
	if err != nil {
		return
	}
	defer in.Close() // not writing the file, so we can ignore Close() errors here

	var jsonInput io.Reader = in
	if preprocessCmd != nil {
		var tmpBs []byte
		tmpBs, err = ioutil.ReadAll(in)
		if err != nil {
			return
		}
		jsonInput, err = extPreprocess(preprocessCmd, tmpBs)
		if err != nil {
			return
		}
	}

	reader := JsonConfigReader.New(jsonInput)
	return ioutil.ReadAll(reader)
}
Esempio n. 9
0
func main() {
	var files, templates []string
	jf := ""
	e := ".go"
	jnext := false
	enext := false
	data := make(map[string]interface{})
	verbosity := 2
	which := 0
	for _, opt := range os.Args[1:] {
		switch {
		case jnext:
			jf = opt
			jnext = false
		case enext:
			e = opt
			enext = false
		case opt == "-q":
			verbosity--
		case opt == "-j":
			jnext = true
		case opt == "-e":
			enext = true
		case opt == "-t":
			which = 1
		case opt == "-f":
			which = 2
		case which == 1:
			templates = append(templates, opt)
		case which == 2:
			files = append(files, opt)
		default:
			log.Fatal("[-q [-q]] [-e] [-j <json>] [-t <templates>] -f <files>")
		}
	}
	if verbosity > 0 {
		fatal = log.Fatal
	} else {
		fatal = func(v ...interface{}) { os.Exit(1) }
	}
	if jnext {
		fatal("json file not given")
	}
	if enext {
		fatal("ending not given")
	}
	if len(files) == 0 {
		fatal("no files given")
	}
	if jf != "" {
		j, err := os.Open(jf)
		if err != nil {
			fatal("unable to open ", jf, ": ", err)
		}
		err = json.NewDecoder(JsonConfigReader.New(j)).Decode(&data)
		if err != nil {
			fatal("unable to load ", jf, ": ", err)
		}
	}
	for _, file := range files {
		var new string
		if i := strings.LastIndex(file, "."); i > 0 {
			new = file[:i] + e
		} else {
			new = file + e
		}
		if verbosity >= 2 {
			fmt.Printf("%s -> %s\n", file, new)
		}
		if err := Gen(file, new, templates, data); err != nil {
			fatal("error generating ", file, ": ", err)
		}
	}
}