func getItems(config yaml.File, key string) ([]*AvailableBlock, error) { lst, err := validateItems(config, key) if err != nil { return nil, errors.New("Invalid items") } items := make([]*AvailableBlock, lst.Len()) for i, e := range lst { itemKey := key + "[" + strconv.Itoa(i) + "]" title, err := config.Get(itemKey + ".title") if err != nil { return nil, errors.New("Missing title") } series, err := config.Get(itemKey + ".series") if err != nil { series = "" } filepathsNode, err := yaml.Child(e, "filepaths") if err != nil { return nil, errors.New("Missing filepaths for " + title) } publish := true if key == "extras" { publish = false } items[i] = new(AvailableBlock).Init(title, series, filepathsNode.(yaml.List), publish) } return items, nil }
func YamlList2Slice(config *yaml.File, text string) []string { count, err := config.Count(text) if err != nil { loge.Error(err) return nil } var result []string for i := 0; i < count; i++ { v, err := config.Get(text + "[" + strconv.Itoa(i) + "]") if err != nil { loge.Error(err) return nil } result = append(result, v) } return result }
func YamlList2String(config *yaml.File, text string) string { count, err := config.Count(text) if err != nil { log.Println(err) return "" } var resultGroup []string for i := 0; i < count; i++ { v, err := config.Get(text + "[" + strconv.Itoa(i) + "]") if err != nil { log.Println(err) return "" } resultGroup = append(resultGroup, v) } result := strings.Join(resultGroup, "\n") result = strings.Replace(result, "\\n", "", -1) return result }
// Creates a new SQLConf and returns a pointer to it. // If it encounters an error during parsing the file, // it will return an error instead. func confFromYamlFile(config *yaml.File, goEnv string) (*SQLConf, error) { // TODO Refactor this into a more generic method of retrieving info // Get driver driver, driveError := config.Get(fmt.Sprintf("%s.driver", goEnv)) if driveError != nil { return nil, driveError } // Get openStr openStr, openStrError := config.Get(fmt.Sprintf("%s.openStr", goEnv)) if openStrError != nil { return nil, openStrError } // Get table table, tableError := config.Get(fmt.Sprintf("%s.table", goEnv)) if tableError != nil { return nil, tableError } // Get latCol latCol, latColError := config.Get(fmt.Sprintf("%s.latCol", goEnv)) if latColError != nil { return nil, latColError } // Get lngCol lngCol, lngColError := config.Get(fmt.Sprintf("%s.lngCol", goEnv)) if lngColError != nil { return nil, lngColError } sqlConf := &SQLConf{driver: driver, openStr: openStr, table: table, latCol: latCol, lngCol: lngCol} return sqlConf, nil }
func ParseConfig(config *yaml.File) (*Config, error) { base, err := config.Get("base") if err != nil { return nil, err } var rpm *RpmPackage = nil rpmBaseNode, err := yaml.Child(config.Root, "rpm-base") if err != nil { return nil, err } if rpmBaseNode != nil { rpmBaseMap := rpmBaseNode.(yaml.Map) scalar := rpmBaseMap["name"].(yaml.Scalar) name := strings.TrimSpace(scalar.String()) scalar = rpmBaseMap["version"].(yaml.Scalar) version := strings.TrimSpace(scalar.String()) scalar = rpmBaseMap["release"].(yaml.Scalar) release := strings.TrimSpace(scalar.String()) scalar = rpmBaseMap["arch"].(yaml.Scalar) arch := strings.TrimSpace(scalar.String()) rpm = &RpmPackage{ Name: name, Version: version, Release: release, Arch: arch, } } cmdline, err := config.Get("cmdline") if err != nil { return nil, err } build, _ := config.Get("build") filesNode, err := yaml.Child(config.Root, "files") if err != nil { return nil, err } files := make(map[string]string) if filesNode != nil { filesMap := filesNode.(yaml.Map) for key, value := range filesMap { scalar := value.(yaml.Scalar) files[key] = strings.TrimSpace(scalar.String()) } } result := &Config{ Base: base, RpmBase: rpm, Cmdline: cmdline, Build: build, Files: files, } return result, nil }
func extractConnectionOptions(config *yaml.File) (connectionOptions pgx.ConnectionParameters, err error) { connectionOptions.Host, _ = config.Get("database.host") connectionOptions.Socket, _ = config.Get("database.socket") if connectionOptions.Host == "" && connectionOptions.Socket == "" { err = errors.New("Config must contain database.host or database.socket but it does not") return } port, _ := config.GetInt("database.port") connectionOptions.Port = uint16(port) if connectionOptions.Database, err = config.Get("database.database"); err != nil { err = errors.New("Config must contain database.database but it does not") return } if connectionOptions.User, err = config.Get("database.user"); err != nil { err = errors.New("Config must contain database.user but it does not") return } connectionOptions.Password, _ = config.Get("database.password") return }
func init() { var err error var yf *yaml.File flag.StringVar(&config.listenAddress, "address", "127.0.0.1", "address to listen on") flag.StringVar(&config.listenPort, "port", "8080", "port to listen on") flag.StringVar(&config.assetPath, "assetpath", "assets", "path to assets") flag.StringVar(&config.configPath, "config", "config.yml", "path to config file") flag.Parse() givenCliArgs := make(map[string]bool) flag.Visit(func(f *flag.Flag) { givenCliArgs[f.Name] = true }) if config.configPath, err = filepath.Abs(config.configPath); err != nil { fmt.Fprintf(os.Stderr, "Invalid config path: %v\n", err) os.Exit(1) } if config.assetPath, err = filepath.Abs(config.assetPath); err != nil { fmt.Fprintf(os.Stderr, "Invalid asset path: %v\n", err) os.Exit(1) } if yf, err = yaml.ReadFile(config.configPath); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } if !givenCliArgs["address"] { if address, err := yf.Get("address"); err == nil { config.listenAddress = address } } if !givenCliArgs["assetpath"] { if assetpath, err := yf.Get("assetpath"); err == nil { config.assetPath = assetpath } } if !givenCliArgs["port"] { if port, err := yf.Get("port"); err == nil { config.listenPort = port } } var connectionParameters pgx.ConnectionParameters if connectionParameters, err = extractConnectionOptions(yf); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } if err = migrate(connectionParameters); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } poolOptions := pgx.ConnectionPoolOptions{MaxConnections: 5, AfterConnect: afterConnect} pool, err = pgx.NewConnectionPool(connectionParameters, poolOptions) if err != nil { fmt.Fprintf(os.Stderr, "Unable to create database connection pool: %v\n", err) os.Exit(1) } }