예제 #1
0
func LoadConfig(configFile string) *Config {
	// expand tilde to users home dir
	if strings.HasPrefix(configFile, "~") {
		u, _ := user.Current()
		configFile = strings.Replace(configFile, "~", u.HomeDir, 1)
	}

	log.Printf("config file is %s", configFile)

	// make sure the config file actually exists
	if _, err := os.Stat(configFile); os.IsNotExist(err) {
		utils.Fatalf("Error: couldn't load config from %s\n\n"+
			"If you haven't configured r53dyndns yet, place a "+
			"file that looks like this in ~/.r53dyndns:\n\n%s",
			configFile, SampleConfig())
	}

	// load config
	file, _ := os.Open(configFile)
	decoder := json.NewDecoder(file)
	config := Config{}
	if err := decoder.Decode(&config); err != nil {
		utils.Fatalf("error: %s", err)
	}

	// pre-flight check
	utils.CheckErr(config.PreFlight())

	return &config
}
예제 #2
0
파일: main.go 프로젝트: gregarmer/r53dyndns
func main() {
	start_time := time.Now()
	var ip string

	flag.Parse()

	if *domain == "" {
		utils.Fatalf("Error: please supply the domain to update. Use -h for help.")
	}

	if !*verbose {
		log.SetOutput(ioutil.Discard)
	}

	log.Printf("starting Route 53 DNS update for %s", *domain)

	conf := config.LoadConfig(*configFile)
	r53 := dyndns.Dyndns{Config: conf}

	switch {
	case *staticIp != "":
		ip = *staticIp
		if !utils.ValidIP(ip) {
			utils.Fatalf("Error: %s is not a valid IP", ip)
		}
	case *interfaceIp != "":
		var err error
		ip, err = utils.GetInterfaceIP(*interfaceIp)
		utils.CheckErr(err)
	case *autoDetect:
		var err error
		ip, err = utils.GetExternalIP()
		utils.CheckErr(err)
	default:
		utils.Fatalf("Error: please supply one of -s, -a, -i. Use -h for help.")
	}

	// update record
	// XXX: only if it changed from the last run
	r53.UpsertDomain(*domain, ip)

	log.Printf("done - took %s", time.Since(start_time))
}