github.com/Azure/draft@v0.16.0/pkg/draft/pack/pack.go (about)

     1  package pack
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"k8s.io/helm/pkg/chartutil"
    10  	"k8s.io/helm/pkg/proto/hapi/chart"
    11  
    12  	"github.com/Azure/draft/pkg/osutil"
    13  )
    14  
    15  const (
    16  	// ChartfileName is the default Chart file name.
    17  	ChartfileName = "Chart.yaml"
    18  	// ValuesfileName is the default values file name.
    19  	ValuesfileName = "values.yaml"
    20  	// IgnorefileName is the name of the Helm ignore file.
    21  	IgnorefileName = ".helmignore"
    22  	// DeploymentName is the name of the deployment file.
    23  	DeploymentName = "deployment.yaml"
    24  	// ServiceName is the name of the service file.
    25  	ServiceName = "service.yaml"
    26  	// IngressName is the name of the ingress file.
    27  	IngressName = "ingress.yaml"
    28  	// NotesName is the name of the NOTES.txt file.
    29  	NotesName = "NOTES.txt"
    30  	// HelpersName is the name of the helpers file.
    31  	HelpersName = "_helpers.tpl"
    32  	// TemplatesDir is the relative directory name for templates.
    33  	TemplatesDir = "templates"
    34  	// ChartsDir is the directory name for the packaged chart.
    35  	// This also doubles as the directory name for chart dependencies.
    36  	ChartsDir = "charts"
    37  	//TasksFileName is the name of the tasks file in a draft pack
    38  	TasksFileName = "tasks.toml"
    39  	//TargetTasksFileName is the name of the file where the tasks file from the
    40  	//  draft pack will be copied to
    41  	TargetTasksFileName = ".draft-tasks.toml"
    42  )
    43  
    44  // Pack defines a Draft Starter Pack.
    45  type Pack struct {
    46  	// Chart is the Helm chart to be installed with the Pack.
    47  	Chart *chart.Chart
    48  	// Files are the files inside the Pack that will be installed.
    49  	Files map[string]io.ReadCloser
    50  }
    51  
    52  // SaveDir saves a pack as files in a directory.
    53  func (p *Pack) SaveDir(dest string) error {
    54  	// Create the chart directory
    55  	chartPath := filepath.Join(dest, ChartsDir)
    56  	if err := os.Mkdir(chartPath, 0755); err != nil {
    57  		return fmt.Errorf("Could not create %s: %s", chartPath, err)
    58  	}
    59  	if err := chartutil.SaveDir(p.Chart, chartPath); err != nil {
    60  		return err
    61  	}
    62  
    63  	// create a tasks file
    64  	tasksFilePath := filepath.Join(dest, TargetTasksFileName)
    65  	exists, err := osutil.Exists(tasksFilePath)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if !exists {
    70  		f, ok := p.Files[TasksFileName]
    71  		if ok {
    72  			newfile, err := os.Create(tasksFilePath)
    73  			if err != nil {
    74  				return err
    75  			}
    76  			defer newfile.Close()
    77  			defer f.Close()
    78  			io.Copy(newfile, f)
    79  		} else {
    80  			tasksFile, err := os.Create(tasksFilePath)
    81  			if err != nil {
    82  				return err
    83  			}
    84  			tasksFile.Close()
    85  		}
    86  	}
    87  
    88  	delete(p.Files, TasksFileName)
    89  
    90  	// save the rest of the files
    91  	for relPath, f := range p.Files {
    92  		path := filepath.Join(dest, relPath)
    93  		exists, err := osutil.Exists(path)
    94  		if err != nil {
    95  			return err
    96  		}
    97  		if !exists {
    98  			baseDir := filepath.Dir(path)
    99  			if os.MkdirAll(baseDir, 0755) != nil {
   100  				return fmt.Errorf("Error creating directory %v: %v", baseDir, err)
   101  			}
   102  			newfile, err := os.Create(path)
   103  			if err != nil {
   104  				return err
   105  			}
   106  			defer newfile.Close()
   107  			defer f.Close()
   108  			io.Copy(newfile, f)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }