Skip to content

Commit

Permalink
repo: add support for JSON marshalling
Browse files Browse the repository at this point in the history
Signed-off-by: MeurillonGuillaume <[email protected]>
  • Loading branch information
MeurillonGuillaume committed Dec 6, 2023
1 parent 4044841 commit abd614c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pkg/repo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package repo

import (
"encoding/json"
"sync"
"time"

Expand Down Expand Up @@ -51,27 +52,34 @@ type (
Raw []byte `json:"c"`
ChartURL string `json:"d"`
IndexLock sync.RWMutex
outputJSON bool
}
)

// NewIndex creates a new instance of Index
func NewIndex(chartURL string, repo string, serverInfo *ServerInfo) *Index {
func NewIndex(chartURL string, repo string, serverInfo *ServerInfo, outputJSON bool) *Index {
indexFile := &IndexFile{
IndexFile: &helm_repo.IndexFile{},
ServerInfo: serverInfo,
}
index := Index{indexFile, repo, []byte{}, chartURL, sync.RWMutex{}}
index := Index{indexFile, repo, []byte{}, chartURL, sync.RWMutex{}, outputJSON}
index.Entries = map[string]helm_repo.ChartVersions{}
index.APIVersion = helm_repo.APIVersionV1
index.Regenerate()
return &index
}

// Regenerate sorts entries in index file and sets current time for generated key
func (index *Index) Regenerate() error {
func (index *Index) Regenerate() (err error) {
index.SortEntries()
index.Generated = time.Now().Round(time.Second)
raw, err := yaml.Marshal(index.IndexFile)

var raw []byte
if index.outputJSON {
raw, err = json.Marshal(index.IndexFile)
} else {
raw, err = yaml.Marshal(index.IndexFile)
}
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/repo/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func getChartVersion(name string, patch int, created time.Time) *helm_repo.Chart
}

func (suite *IndexTestSuite) SetupSuite() {
suite.Index = NewIndex("", "", &ServerInfo{})
suite.Index = NewIndex("", "", &ServerInfo{}, false)
now := time.Now()
for _, name := range []string{"a", "b", "c"} {
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -94,13 +94,13 @@ func (suite *IndexTestSuite) TestRemove() {
}

func (suite *IndexTestSuite) TestChartURLs() {
index := NewIndex("", "", &ServerInfo{})
index := NewIndex("", "", &ServerInfo{}, false)
chartVersion := getChartVersion("a", 0, time.Now())
index.AddEntry(chartVersion)
suite.Equal("charts/a-1.0.0.tgz",
index.Entries["a"][0].URLs[0], "relative chart url")

index = NewIndex("http://mysite.com:8080", "", &ServerInfo{})
index = NewIndex("http://mysite.com:8080", "", &ServerInfo{}, false)
chartVersion = getChartVersion("a", 0, time.Now())
index.AddEntry(chartVersion)
suite.Equal("http://mysite.com:8080/charts/a-1.0.0.tgz",
Expand All @@ -109,13 +109,13 @@ func (suite *IndexTestSuite) TestChartURLs() {

func (suite *IndexTestSuite) TestServerInfo() {
serverInfo := &ServerInfo{}
index := NewIndex("", "", serverInfo)
index := NewIndex("", "", serverInfo, false)
suite.False(strings.Contains(string(index.Raw), "contextPath: /v1/helm"), "context path not in index")

serverInfo = &ServerInfo{
ContextPath: "/v1/helm",
}
index = NewIndex("", "", serverInfo)
index = NewIndex("", "", serverInfo, false)
suite.True(strings.Contains(string(index.Raw), "contextPath: /v1/helm"), "context path is in index")
}

Expand Down

0 comments on commit abd614c

Please sign in to comment.