-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
downloading icons when running make charts
- Loading branch information
1 parent
df3f147
commit d6192b5
Showing
7 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package icons | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"mime" | ||
"net/http" | ||
|
||
"github.com/rancher/charts-build-scripts/pkg/path" | ||
|
||
"github.com/go-git/go-billy/v5" | ||
"github.com/sirupsen/logrus" | ||
"helm.sh/helm/v3/pkg/chart" | ||
) | ||
|
||
// Download receives a chart metadata and the filesystem pointing to the root of the project. | ||
// From the metadata, gets the icon and name of the chart. | ||
// It downloads the icon, infers the type using the content-type header from the response | ||
// and saves the file locally to path.RepositoryLogosDir using the name of the chart as the file name. | ||
func Download(rootFs billy.Filesystem, metadata *chart.Metadata) (string, error) { | ||
icon, err := http.Get(metadata.Icon) | ||
if err != nil { | ||
logrus.Errorf(err.Error()) | ||
return "", fmt.Errorf("err: %w", err) | ||
} | ||
|
||
byType, err := mime.ExtensionsByType(icon.Header.Get("Content-Type")) | ||
if err != nil || len(byType) == 0 || icon.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("invalid icon") | ||
} | ||
path := fmt.Sprintf("%s/%s%s", path.RepositoryLogosDir, metadata.Name, byType[0]) | ||
create, err := rootFs.Create(path) | ||
if err != nil { | ||
logrus.Errorf(err.Error()) | ||
return "", fmt.Errorf("err: %w", err) | ||
} | ||
defer create.Close() | ||
_, err = io.Copy(create, icon.Body) | ||
defer icon.Body.Close() | ||
if err != nil { | ||
logrus.Errorf(err.Error()) | ||
return "", fmt.Errorf("err: %w", err) | ||
} | ||
return path, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters