github.com/fitzoh/maven-push-plugin@v0.4.0/manifest_validation.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"gopkg.in/yaml.v2"
     6  	"io/ioutil"
     7  )
     8  
     9  var (
    10  	ValidKeys = map[string]bool{
    11  		"repo-url":      true,
    12  		"group-id":      true,
    13  		"artifact-id":   true,
    14  		"version":       true,
    15  		"extension":     true,
    16  		"classifier":    true,
    17  		"repo-username": true,
    18  		"repo-password": true,
    19  	}
    20  )
    21  
    22  type ValidationManifest struct {
    23  	Applications []ValidationApplication `yaml:"applications"`
    24  }
    25  
    26  type ValidationApplication struct {
    27  	MavenConfig map[string]string `yaml:"maven"`
    28  }
    29  
    30  func ValidateManifest(f string) error {
    31  	manifest, err := parseValidationManifest(f)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	if numApplications := len(manifest.Applications); numApplications != 1 {
    36  		return fmt.Errorf("single application manifest required, %d found", numApplications)
    37  	}
    38  	config := manifest.Applications[0].MavenConfig
    39  	for k := range config {
    40  		if !ValidKeys[k] {
    41  			return fmt.Errorf("invalid key found in maven configuration, %s", k)
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func parseValidationManifest(f string) (ValidationManifest, error) {
    49  	manifest := ValidationManifest{}
    50  	raw, err := ioutil.ReadFile(f)
    51  	if err != nil {
    52  		return manifest, fmt.Errorf("failed to open manifest file %s, %+v", f, err)
    53  	}
    54  	err = yaml.Unmarshal(raw, &manifest)
    55  	if err != nil {
    56  		return manifest, fmt.Errorf("failed to parse manifest file %s, %+v", f, err)
    57  	}
    58  	return manifest, nil
    59  }