Skip to content

Commit

Permalink
add pages-index-path option (#151)
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Lloyd <caleblloyd@gmail.com>
  • Loading branch information
caleblloyd authored Mar 14, 2022
1 parent d4a3c65 commit 5354f68
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Flags:
-o, --owner string GitHub username or organization
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
--pages-branch string The GitHub pages branch (default "gh-pages")
--pages-index-path string The GitHub pages index path (default "index.yaml")
--pr Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)
--push Push index.yaml to the GitHub Pages branch (must not be set if --pr is set)
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
Expand Down
1 change: 1 addition & 0 deletions cr/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func init() {
flags.StringP("git-base-url", "b", "https://api.github.com/", "GitHub Base URL (only needed for private GitHub)")
flags.StringP("git-upload-url", "u", "https://uploads.github.com/", "GitHub Upload URL (only needed for private GitHub)")
flags.String("pages-branch", "gh-pages", "The GitHub pages branch")
flags.String("pages-index-path", "index.yaml", "The GitHub pages index path")
flags.String("remote", "origin", "The Git remote used when creating a local worktree for the GitHub Pages branch")
flags.Bool("push", false, "Push index.yaml to the GitHub Pages branch (must not be set if --pr is set)")
flags.Bool("pr", false, "Create a pull request for index.yaml against the GitHub Pages branch (must not be set if --push is set)")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Options struct {
GitUploadURL string `mapstructure:"git-upload-url"`
Commit string `mapstructure:"commit"`
PagesBranch string `mapstructure:"pages-branch"`
PagesIndexPath string `mapstructure:"pages-index-path"`
Push bool `mapstructure:"push"`
PR bool `mapstructure:"pr"`
Remote string `mapstructure:"remote"`
Expand Down
20 changes: 16 additions & 4 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ func NewReleaser(config *config.Options, github GitHub, git Git) *Releaser {

// UpdateIndexFile updates the index.yaml file for a given Git repo
func (r *Releaser) UpdateIndexFile() (bool, error) {
// if path doesn't end with index.yaml we can try and fix it
// if index-path doesn't end with index.yaml we can try and fix it
if filepath.Base(r.config.IndexPath) != "index.yaml" {
// if path is a directory then add index.yaml
if stat, err := os.Stat(r.config.IndexPath); err == nil && stat.IsDir() {
r.config.IndexPath = filepath.Join(r.config.IndexPath, "index.yaml")
// otherwise error out
} else {
fmt.Printf("path (%s) should be a directory or a file called index.yaml\n", r.config.IndexPath)
fmt.Printf("index-path (%s) should be a directory or a file called index.yaml\n", r.config.IndexPath)
os.Exit(1)
}
}
Expand All @@ -111,7 +111,19 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
return false, err
}
defer r.git.RemoveWorktree("", worktree) // nolint: errcheck
indexYamlPath := filepath.Join(worktree, "index.yaml")

// if pages-index-path doesn't end with index.yaml we can try and fix it
if filepath.Base(r.config.PagesIndexPath) != "index.yaml" {
// if path is a directory then add index.yaml
if stat, err := os.Stat(filepath.Join(worktree, r.config.PagesIndexPath)); err == nil && stat.IsDir() {
r.config.PagesIndexPath = filepath.Join(r.config.PagesIndexPath, "index.yaml")
// otherwise error out
} else {
fmt.Printf("pages-index-path (%s) should be a directory or a file called index.yaml\n", r.config.PagesIndexPath)
os.Exit(1)
}
}
indexYamlPath := filepath.Join(worktree, r.config.PagesIndexPath)

var indexFile *repo.IndexFile
_, err = os.Stat(indexYamlPath)
Expand Down Expand Up @@ -197,7 +209,7 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
if err := r.git.Add(worktree, indexYamlPath); err != nil {
return false, err
}
if err := r.git.Commit(worktree, "Update index.yaml"); err != nil {
if err := r.git.Commit(worktree, fmt.Sprintf("Update %s", r.config.PagesIndexPath)); err != nil {
return false, err
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/releaser/releaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ func TestReleaser_UpdateIndexFile(t *testing.T) {
git: &FakeGit{"testdata/repo/index.yaml"},
},
},
{
"index-file-exists-pages-index-path",
true,
&Releaser{
config: &config.Options{
IndexPath: "testdata/index/index.yaml",
PackagePath: "testdata/release-packages",
PagesIndexPath: "./",
},
github: fakeGitHub,
git: &FakeGit{"testdata/repo/index.yaml"},
},
},
{
"index-file-does-not-exist",
false,
Expand All @@ -132,6 +145,19 @@ func TestReleaser_UpdateIndexFile(t *testing.T) {
git: &FakeGit{""},
},
},
{
"index-file-does-not-exist-pages-index-path",
false,
&Releaser{
config: &config.Options{
IndexPath: filepath.Join(indexDir, "index.yaml"),
PackagePath: "testdata/release-packages",
PagesIndexPath: "./",
},
github: fakeGitHub,
git: &FakeGit{""},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 5354f68

Please sign in to comment.