github.com/simonferquel/app@v0.6.1-0.20181012141724-68b7cccf26ac/types/metadata/metadata.go (about)

     1  package metadata
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // Maintainer represents one of the apps's maintainers
     8  type Maintainer struct {
     9  	Name  string `json:"name"`
    10  	Email string `json:"email,omitempty"`
    11  }
    12  
    13  // Maintainers is a list of maintainers
    14  type Maintainers []Maintainer
    15  
    16  // String gives a string representation of a list of maintainers
    17  func (ms Maintainers) String() string {
    18  	res := make([]string, len(ms))
    19  	for i, m := range ms {
    20  		res[i] = m.String()
    21  	}
    22  	return strings.Join(res, ", ")
    23  }
    24  
    25  // String gives a string representation of a maintainer
    26  func (m Maintainer) String() string {
    27  	s := m.Name
    28  	if m.Email != "" {
    29  		s += " <" + m.Email + ">"
    30  	}
    31  	return s
    32  }
    33  
    34  // AppMetadata is the format of the data found inside the metadata.yml file
    35  type AppMetadata struct {
    36  	Version     string      `json:"version"`
    37  	Name        string      `json:"name"`
    38  	Description string      `json:"description,omitempty"`
    39  	Namespace   string      `json:"namespace,omitempty"`
    40  	Maintainers Maintainers `json:"maintainers,omitempty"`
    41  	Parents     Parents     `yaml:",omitempty" json:"parents,omitempty"`
    42  }
    43  
    44  // Parents is a list of ParentMetadata items
    45  type Parents []ParentMetadata
    46  
    47  // ParentMetadata contains historical data of forked packages
    48  type ParentMetadata struct {
    49  	Name        string      `json:"name"`
    50  	Namespace   string      `json:"namespace,omitempty"`
    51  	Version     string      `json:"version"`
    52  	Maintainers Maintainers `json:"maintainers,omitempty"`
    53  }
    54  
    55  // Modifier is a function signature that takes and returns an AppMetadata object
    56  type Modifier func(AppMetadata) AppMetadata
    57  
    58  // From returns an AppMetadata instance based on the provided AppMetadata
    59  // and applicable modifier functions
    60  func From(orig AppMetadata, modifiers ...Modifier) AppMetadata {
    61  	parent := ParentMetadata{
    62  		Name:        orig.Name,
    63  		Namespace:   orig.Namespace,
    64  		Version:     orig.Version,
    65  		Maintainers: orig.Maintainers,
    66  	}
    67  
    68  	result := AppMetadata{
    69  		Version:     orig.Version,
    70  		Name:        orig.Name,
    71  		Namespace:   orig.Namespace,
    72  		Description: orig.Description,
    73  		Maintainers: orig.Maintainers,
    74  		Parents:     append(orig.Parents, parent),
    75  	}
    76  	for _, f := range modifiers {
    77  		result = f(result)
    78  	}
    79  	return result
    80  }
    81  
    82  // WithMaintainers returns a modified AppMetadata with updated maintainers field
    83  func WithMaintainers(maintainers Maintainers) Modifier {
    84  	return func(parent AppMetadata) AppMetadata {
    85  		parent.Maintainers = maintainers
    86  		return parent
    87  	}
    88  }
    89  
    90  // WithName returns a modified AppMetadata with updated name field
    91  func WithName(name string) Modifier {
    92  	return func(parent AppMetadata) AppMetadata {
    93  		parent.Name = name
    94  		return parent
    95  	}
    96  }
    97  
    98  // WithNamespace returns a modified AppMetadata with updated namespace field
    99  func WithNamespace(namespace string) Modifier {
   100  	return func(parent AppMetadata) AppMetadata {
   101  		parent.Namespace = namespace
   102  		return parent
   103  	}
   104  }