func NewConfig(file string) (*Config, error) { c := &Config{} multiconfig.MustLoadWithPath(file, c) if c.PrivateKey == nil { if c.PrivateKeyPath == "" { u, err := user.Current() if err != nil { return nil, err } c.PrivateKeyPath = filepath.Join(u.HomeDir, ".ssh", "koding_rsa") } p, err := ioutil.ReadFile(c.PrivateKeyPath) if err != nil { return nil, err } c.PrivateKey = p } return c, nil }
func realMain() error { conf := new(Config) multiconfig.MustLoadWithPath("config.toml", conf) db := mongodb.NewMongoDB(conf.MongoURL) opts := &amazon.ClientOptions{ Credentials: credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, ""), Regions: amazon.ProductionRegions, Log: logging.NewLogger("userdebug"), } ec2clients, err := amazon.NewClients(opts) if err != nil { panic(err) } w := new(tabwriter.Writer) w.Init(os.Stdout, 10, 8, 0, '\t', 0) defer w.Flush() // search via username, mongodb -> aws if conf.Username != "" { ms, err := machinesFromUsername(db, conf.Username) if err == nil { ms.Print(w) for _, m := range ms.docs { // if the mongodb document has a region and instanceId, display it too if m.Meta.Region != "" && m.Meta.InstanceId != "" { client, err := ec2clients.Region(m.Meta.Region) if err != nil { return err } i, err := awsData(client, m.Meta.InstanceId) if err != nil { return err } i.Print(w) } } } else { fmt.Fprintf(os.Stderr, err.Error()) } } // search via instanceId, aws -> mongodb if conf.InstanceId != "" { for _, client := range ec2clients.Regions() { i, err := awsData(client, conf.InstanceId) if err != nil { continue // if not found continue with next region } // if we find a mongoDB document display it ms, err := machinesFromInstanceId(db, conf.InstanceId) if err == nil { ms.Print(w) } i.Print(w) break } } return nil }