@ -36,6 +36,7 @@ const (
preTemplates string = "templates/"
preTemplates string = "templates/"
preHooks string = "hooks/"
preHooks string = "hooks/"
preDocs string = "docs/"
preDocs string = "docs/"
preIcon string = "icon.svg"
)
)
// Chart represents a complete chart.
// Chart represents a complete chart.
@ -54,47 +55,93 @@ const (
//
//
// Optionally, a chart might also locate a provenance (.prov) file that it
// Optionally, a chart might also locate a provenance (.prov) file that it
// can use for cryptographic signing.
// can use for cryptographic signing.
type Chart interface {
type Chart struct {
loader chartLoader
}
// Close the chart.
//
// Charts should always be closed when no longer needed.
func ( c * Chart ) Close ( ) error {
return c . loader . close ( )
}
// Chartfile gets the Chartfile (Chart.yaml) for this chart.
func ( c * Chart ) Chartfile ( ) * Chartfile {
return c . loader . chartfile ( )
}
// Dir() returns the directory where the charts are located.
func ( c * Chart ) Dir ( ) string {
return c . loader . dir ( )
}
// DocsDir returns the directory where the chart's documentation is stored.
func ( c * Chart ) DocsDir ( ) string {
return filepath . Join ( c . loader . dir ( ) , preDocs )
}
// HooksDir returns the directory where the hooks are stored.
func ( c * Chart ) HooksDir ( ) string {
return filepath . Join ( c . loader . dir ( ) , preHooks )
}
// TemplatesDir returns the directory where the templates are stored.
func ( c * Chart ) TemplatesDir ( ) string {
return filepath . Join ( c . loader . dir ( ) , preTemplates )
}
// Icon returns the path to the icon.svg file.
//
// If an icon is not found in the chart, this will return an error.
func ( c * Chart ) Icon ( ) ( string , error ) {
i := filepath . Join ( c . Dir ( ) , preIcon )
_ , err := os . Stat ( i )
return i , err
}
// chartLoader provides load, close, and save implementations for a chart.
type chartLoader interface {
// Chartfile resturns a *Chartfile for this chart.
// Chartfile resturns a *Chartfile for this chart.
Chartfile ( ) * Chartfile
c hartfile( ) * Chartfile
// Dir returns a directory where the chart can be accessed.
// Dir returns a directory where the chart can be accessed.
Dir ( ) string
d ir( ) string
// Close cleans up a chart.
// Close cleans up a chart.
Close ( ) error
c lose( ) error
}
}
type dirChart struct {
type dirChart struct {
chartfile * Chartfile
chart yaml * Chartfile
dir string
chart dir string
}
}
func ( d * dirChart ) Chartfile ( ) * Chartfile {
func ( d * dirChart ) c hartfile( ) * Chartfile {
return d . chartfile
return d . chart yaml
}
}
func ( d * dirChart ) Dir ( ) string {
func ( d * dirChart ) d ir( ) string {
return "."
return d . chartdir
}
}
func ( d * dirChart ) Close ( ) error {
func ( d * dirChart ) c lose( ) error {
return nil
return nil
}
}
type tarChart struct {
type tarChart struct {
chartfile * Chartfile
chart yaml * Chartfile
tmpDir string
tmpDir string
}
}
func ( t * tarChart ) Chartfile ( ) * Chartfile {
func ( t * tarChart ) c hartfile( ) * Chartfile {
return t . chartfile
return t . chart yaml
}
}
func ( t * tarChart ) Dir ( ) string {
func ( t * tarChart ) d ir( ) string {
return "."
return t . tmpDir
}
}
func ( t * tarChart ) Close ( ) error {
func ( t * tarChart ) c lose( ) error {
// Remove the temp directory.
// Remove the temp directory.
return os . RemoveAll ( t . tmpDir )
return os . RemoveAll ( t . tmpDir )
}
}
@ -105,7 +152,7 @@ func (t *tarChart) Close() error {
//
//
// If you are just reading the Chart.yaml file, it is substantially more
// If you are just reading the Chart.yaml file, it is substantially more
// performant to use LoadChartfile.
// performant to use LoadChartfile.
func LoadDir ( chart string ) ( Chart , error ) {
func LoadDir ( chart string ) ( * Chart , error ) {
if fi , err := os . Stat ( chart ) ; err != nil {
if fi , err := os . Stat ( chart ) ; err != nil {
return nil , err
return nil , err
} else if ! fi . IsDir ( ) {
} else if ! fi . IsDir ( ) {
@ -117,19 +164,21 @@ func LoadDir(chart string) (Chart, error) {
return nil , err
return nil , err
}
}
c := & dirChart {
c l := & dirChart {
chart file : cf ,
chart yaml : cf ,
dir: chart ,
chart dir: chart ,
}
}
return c , nil
return & Chart {
loader : cl ,
} , nil
}
}
// Load loads a chart from a chart archive.
// Load loads a chart from a chart archive.
//
//
// A chart archive is a gzipped tar archive that follows the Chart format
// A chart archive is a gzipped tar archive that follows the Chart format
// specification.
// specification.
func Load ( archive string ) ( Chart , error ) {
func Load ( archive string ) ( * Chart , error ) {
if fi , err := os . Stat ( archive ) ; err != nil {
if fi , err := os . Stat ( archive ) ; err != nil {
return nil , err
return nil , err
} else if fi . IsDir ( ) {
} else if fi . IsDir ( ) {
@ -151,15 +200,15 @@ func Load(archive string) (Chart, error) {
untarred := tar . NewReader ( unzipped )
untarred := tar . NewReader ( unzipped )
c , err := loadTar ( untarred )
c , err := loadTar ( untarred )
if err != nil {
if err != nil {
return c , err
return nil , err
}
}
cf , err := LoadChartfile ( filepath . Join ( c . tmpDir , ChartfileName ) )
cf , err := LoadChartfile ( filepath . Join ( c . tmpDir , ChartfileName ) )
if err != nil {
if err != nil {
return c , err
return nil , err
}
}
c . chart file = cf
c . chart yaml = cf
return c , nil
return & Chart { loader : c } , nil
}
}
func loadTar ( r * tar . Reader ) ( * tarChart , error ) {
func loadTar ( r * tar . Reader ) ( * tarChart , error ) {
@ -168,7 +217,7 @@ func loadTar(r *tar.Reader) (*tarChart, error) {
return nil , err
return nil , err
}
}
c := & tarChart {
c := & tarChart {
chart file : & Chartfile { } ,
chart yaml : & Chartfile { } ,
tmpDir : td ,
tmpDir : td ,
}
}
@ -221,7 +270,7 @@ func loadTar(r *tar.Reader) (*tarChart, error) {
if err != nil && err != io . EOF {
if err != nil && err != io . EOF {
log . Warn ( "Unexpected error reading tar: %s" , err )
log . Warn ( "Unexpected error reading tar: %s" , err )
c . C lose( )
c . c lose( )
return c , err
return c , err
}
}
log . Info ( "Reached end of Tar file" )
log . Info ( "Reached end of Tar file" )