Save creates an archived chart to the given directory. This takes an existing chart and a destination directory. If the directory is /foo, and the chart is named bar, with version 1.0.0, this will generate /foo/bar-1.0.0.tgz. This returns the absolute path to the chart archive file.
(c *chart.Chart, outDir string)
| 106 | // |
| 107 | // This returns the absolute path to the chart archive file. |
| 108 | func Save(c *chart.Chart, outDir string) (string, error) { |
| 109 | if err := c.Validate(); err != nil { |
| 110 | return "", fmt.Errorf("chart validation: %w", err) |
| 111 | } |
| 112 | |
| 113 | filename := fmt.Sprintf("%s-%s.tgz", c.Name(), c.Metadata.Version) |
| 114 | filename = filepath.Join(outDir, filename) |
| 115 | dir := filepath.Dir(filename) |
| 116 | if stat, err := os.Stat(dir); err != nil { |
| 117 | if errors.Is(err, fs.ErrNotExist) { |
| 118 | if err2 := os.MkdirAll(dir, 0755); err2 != nil { |
| 119 | return "", err2 |
| 120 | } |
| 121 | } else { |
| 122 | return "", fmt.Errorf("stat %s: %w", dir, err) |
| 123 | } |
| 124 | } else if !stat.IsDir() { |
| 125 | return "", fmt.Errorf("is not a directory: %s", dir) |
| 126 | } |
| 127 | |
| 128 | f, err := os.Create(filename) |
| 129 | if err != nil { |
| 130 | return "", err |
| 131 | } |
| 132 | |
| 133 | // Wrap in gzip writer |
| 134 | zipper := gzip.NewWriter(f) |
| 135 | zipper.Extra = headerBytes |
| 136 | zipper.Comment = "Helm" |
| 137 | |
| 138 | // Wrap in tar writer |
| 139 | twriter := tar.NewWriter(zipper) |
| 140 | rollback := false |
| 141 | defer func() { |
| 142 | twriter.Close() |
| 143 | zipper.Close() |
| 144 | f.Close() |
| 145 | if rollback { |
| 146 | os.Remove(filename) |
| 147 | } |
| 148 | }() |
| 149 | |
| 150 | if err := writeTarContents(twriter, c, ""); err != nil { |
| 151 | rollback = true |
| 152 | return filename, err |
| 153 | } |
| 154 | return filename, nil |
| 155 | } |
| 156 | |
| 157 | func writeTarContents(out *tar.Writer, c *chart.Chart, prefix string) error { |
| 158 | err := validateName(c.Name()) |
searching dependent graphs…