Beispiel #1
0
func setup(name string) error {
	var (
		conf *globalconf.GlobalConf
		file string
		err  error
	)

	// Check for environment variable override, and return error if override is set but file is
	// inaccessible. Otherwise, try for the default global location (in the "/etc" directory).
	if file := os.Getenv(strings.ToUpper(name) + "_CONFIG"); file != "" {
		if _, err = os.Stat(file); err != nil {
			return err
		}
	} else {
		file = "/etc/" + name + "/" + name + ".conf"
		if _, err = os.Stat(file); err != nil {
			file = ""
		}
	}

	// Load from specific configuration file if set, or use local configuration file as a fallback.
	if file != "" {
		options := &globalconf.Options{Filename: file, EnvPrefix: ""}
		if conf, err = globalconf.NewWithOptions(options); err != nil {
			return err
		}
	} else if conf, err = globalconf.New(name); err != nil {
		return err
	}

	conf.EnvPrefix = strings.ToUpper(name) + "_"
	conf.ParseAll()

	return nil
}
Beispiel #2
0
Datei: myqt.go Projekt: wfd3/myq
func main() {
	var m myq.MyQ
	var d myq.Device
	var err error
	var username, password string
	var debug, machine_parsable bool
	var conf *globalconf.GlobalConf

	flag.Usage = usage

	flag.StringVar(&username, "user", "", "Username")
	flag.StringVar(&password, "password", "", "Password")
	flag.BoolVar(&debug, "D", false, "Debugging enabled")
	flag.BoolVar(&machine_parsable, "M", false, "Machine parsable output")

	flag.Parse()
	if flag.NArg() < 1 {
		fmt.Println("No command(s)")
		usage()
		os.Exit(0)
	}

	if flag.Arg(0) == "help" {
		usage()
		os.Exit(0)
	}

	// read confg
	if conf, err = globalconf.New("myqt"); err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}
	conf.ParseAll()

	if err := m.New(username, password, debug, machine_parsable); err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}

	command := flag.Arg(0)
	door := flag.Arg(1)
	switch command {
	case "help":
		usage()
	case "state":
		if d, err = m.FindDoorByName(door); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
		m.GetState(d)
	case "details":
		if d, err = m.FindDoorByName(door); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
		m.DoorDetails(d)
	case "open":
		if d, err = m.FindDoorByName(door); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
		if err = m.Open(d); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
	case "close":
		if d, err = m.FindDoorByName(door); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
		if err = m.Close(d); err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
	case "list":
		m.ShowDoors()
	case "locations":
		m.ShowLocations()
	case "listopen":
		m.ShowByState("Open")
	case "listclosed":
		m.ShowByState("Closed")
	default:
		fmt.Printf("unknown command '%s'\n", command)
		os.Exit(1)
	}
}