예제 #1
0
func (c *FlagsConfig) Parse(readerIn io.Reader) error {
	reader := stripper.NewCommentStripper(readerIn)

	decoder, err := control.NewDecoder(bufio.NewReader(reader), nil)
	if err != nil {
		return err
	}

	for {
		entry := FlagsConfigEntry{}

		err := decoder.Decode(&entry)
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		if len(entry.Commands) == 0 {
			entry.Commands = []string{""}
		}

		for _, command := range entry.Commands {
			targetEntry := (*c)[command]
			targetEntry.Apply(entry)
			(*c)[command] = targetEntry
		}
	}

	return nil
}
예제 #2
0
func Parse2822(readerIn io.Reader) (*Manifest2822, error) {
	reader := stripper.NewCommentStripper(readerIn)

	realDecoder, err := control.NewDecoder(bufio.NewReader(reader), nil)
	if err != nil {
		return nil, err
	}
	decoder := decoderWrapper{realDecoder}

	manifest := Manifest2822{
		Global: DefaultManifestEntry.Clone(),
	}

	if err := decoder.Decode(&manifest.Global); err != nil {
		return nil, err
	}
	if len(manifest.Global.Maintainers) < 1 {
		return nil, fmt.Errorf("missing Maintainers")
	}
	if invalidMaintainers := manifest.Global.InvalidMaintainers(); len(invalidMaintainers) > 0 {
		return nil, fmt.Errorf("invalid Maintainers: %q (expected format %q)", strings.Join(invalidMaintainers, ", "), MaintainersFormat)
	}
	if len(manifest.Global.Tags) > 0 {
		return nil, fmt.Errorf("global Tags not permitted")
	}

	for {
		entry := manifest.Global.Clone()

		err := decoder.Decode(&entry)
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}

		if len(entry.Tags) < 1 {
			return nil, fmt.Errorf("missing Tags")
		}
		if entry.GitRepo == "" || entry.GitFetch == "" || entry.GitCommit == "" {
			return nil, fmt.Errorf("Tags %q missing one of GitRepo, GitFetch, or GitCommit", entry.TagsString())
		}
		if !GitFetchRegex.MatchString(entry.GitFetch) {
			return nil, fmt.Errorf(`Tags %q has invalid GitFetch (must be "refs/heads/..." or "refs/tags/..."): %q`, entry.TagsString(), entry.GitFetch)
		}
		if !GitCommitRegex.MatchString(entry.GitCommit) {
			return nil, fmt.Errorf(`Tags %q has invalid GitCommit (must be a commit, not a tag or ref): %q`, entry.TagsString(), entry.GitCommit)
		}

		err = manifest.AddEntry(entry)
		if err != nil {
			return nil, err
		}
	}

	return &manifest, nil
}