pm/meta.go

48 lines
1002 B
Go
Raw Permalink Normal View History

2018-03-02 23:22:57 -08:00
package pm
2018-03-02 23:23:11 -08:00
import (
"errors"
2018-03-03 22:32:30 -08:00
"fmt"
2018-03-02 23:23:11 -08:00
"net/url"
)
2018-03-02 23:22:57 -08:00
2018-03-02 23:23:08 -08:00
// Meta tracks metadata for a package
2018-03-02 23:22:57 -08:00
type Meta struct {
Name Name `json:"name"`
Version Version `json:"version"`
Description string `json:"description"`
2018-03-02 23:23:11 -08:00
Remote url.URL `json:"remote"`
2018-03-02 23:22:57 -08:00
}
2018-03-02 23:23:08 -08:00
// Valid validates the contents of a Meta for requires fields.
2018-03-03 22:32:12 -08:00
func (m Meta) Valid() (bool, error) {
2018-03-02 23:22:57 -08:00
if m.Name == "" {
return false, errors.New("name cannot be empty")
}
if m.Version == "" {
return false, errors.New("version cannot be empty")
}
if m.Description == "" {
return false, errors.New("description cannot be empty")
}
return true, nil
}
2018-03-03 22:52:01 -08:00
// Pkg returns the string name the .pkg should have on disk.
func (m Meta) Pkg() string {
return fmt.Sprintf("%s-%s.pkg", m.Name, m.Version)
}
2018-03-03 22:32:30 -08:00
// URL returns the http location of this package.
func (m Meta) URL() string {
2018-03-03 22:52:01 -08:00
return fmt.Sprintf("%s/%s", m.Remote.String(), m.Pkg())
2018-03-03 22:32:30 -08:00
}
func (m Meta) String() string {
return m.URL()
}
// Metas is a slice of Meta
type Metas []Meta