Skip to content

Commit

Permalink
refactor docs to stop comparison errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Jun 24, 2024
1 parent 9aaf133 commit 6698e64
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/docs/docs.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ resource "chapter" "introduction" {
title = "Introduction"

page "introduction" {
content = "Some more content"
content = file("./docs/index.mdx")
}
}
3 changes: 3 additions & 0 deletions examples/docs/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

In this workshop you will learn the fundamentals Terraform.

Test


## Contents

### Understanding Terraform basics
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ replace github.com/charmbracelet/log => github.com/jumppad-labs/log v0.0.0-20230

replace github.com/zclconf/go-cty => github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6

//replace github.com/jumppad-labs/hclconfig => ../hclconfig/
replace github.com/jumppad-labs/hclconfig => ../hclconfig/

//replace github.com/jumppad-labs/gohup => ../gohup/
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,6 @@ github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.5.0/go.mod h1:a7z7NPPfNQpJWcn4rSWFtdrSldqLdLPEF3d8nFMsSLM=
github.com/hashicorp/go-getter v1.7.3 h1:bN2+Fw9XPFvOCjB0UOevFIMICZ7G2XSQHzfvLUyOM5E=
github.com/hashicorp/go-getter v1.7.3/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0=
github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-hclog v1.1.0 h1:QsGcniKx5/LuX2eYoeL+Np3UKYPNaN7YKpTh29h8rbw=
Expand Down Expand Up @@ -685,10 +683,6 @@ github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6 h1:1ADItCWr5pr
github.com/jumppad-labs/go-cty v0.0.0-20230804061424-9e985cb751f6/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/jumppad-labs/gohup v0.4.0 h1:0OplHvnKnOLkqWm417sRHLSiJ4xGeb8LiSSAJ51QrYg=
github.com/jumppad-labs/gohup v0.4.0/go.mod h1:JYvZnemxJlWDyx8RbDNcCBLZSvIrYlYLnkQqR1BKFW4=
github.com/jumppad-labs/hclconfig v0.22.0 h1:0vamTWU7Fv43QVhwKQTfmBsxBtTmF7qPaHY17zbGtMA=
github.com/jumppad-labs/hclconfig v0.22.0/go.mod h1:MAxvvgP9S8IBc0QKr9V8VJ8I0PplVuhViTy9Cih1LwQ=
github.com/jumppad-labs/hclconfig v0.22.1 h1:l+BYtDjK2kqDCfXMeGSjfMw2n0bqWFHuaPijlsqUxeU=
github.com/jumppad-labs/hclconfig v0.22.1/go.mod h1:1S/JRGGwOHAUWIQN/4HoEDysW55fFMr2sfDPh0pIMqM=
github.com/jumppad-labs/log v0.0.0-20230711151418-55bbc87954b7 h1:jxF+Sxei1/7c7tLcV3Juu+rCIAhBv2jdicokriFohYE=
github.com/jumppad-labs/log v0.0.0-20230711151418-55bbc87954b7/go.mod h1:ZApwwzDbbETVTIRTk7724yQRJAXIktt98yGVMMaa3y8=
github.com/jumppad-labs/plugin-sdk v0.1.1-0.20240306185853-c09f71f81b8a h1:fqPvAO1o/6bUIm0fwEbXcUzUCng+GDlLmSMT/5oUJUE=
Expand Down
41 changes: 33 additions & 8 deletions pkg/config/resources/docs/provider_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"github.com/jumppad-labs/jumppad/pkg/clients/container/types"
"github.com/jumppad-labs/jumppad/pkg/utils"
sdk "github.com/jumppad-labs/plugin-sdk"
"github.com/mohae/deepcopy"
)

const docsImageName = "ghcr.io/jumppad-labs/docs"
const docsVersion = "v0.4.0"
const docsVersion = "v0.4.1"

type DocsConfig struct {
DefaultPath string `json:"defaultPath"`
Expand Down Expand Up @@ -194,11 +195,9 @@ func (p *DocsProvider) Refresh(ctx context.Context) error {
os.Chmod(chapterPath, 0755)

for _, page := range chapter.Pages {
pageFile := fmt.Sprintf("%s.mdx", page.Name)
pagePath := filepath.Join(chapterPath, pageFile)
err := os.WriteFile(pagePath, []byte(page.Content), 0755)
err := p.processPage(chapterPath, page)
if err != nil {
return fmt.Errorf("unable to write page %s to disk at %s", page.Name, pagePath)
return err
}
}
}
Expand All @@ -212,6 +211,18 @@ func (p *DocsProvider) Refresh(ctx context.Context) error {

p.config.ContentChecksum = cs

p.log.Debug("Content written", "ref", p.config.Meta.ID, "checksum", p.config.ContentChecksum)
return nil
}

func (p *DocsProvider) processPage(chapterPath string, page Page) error {
pageFile := fmt.Sprintf("%s.mdx", page.Name)
pagePath := filepath.Join(chapterPath, pageFile)
err := os.WriteFile(pagePath, []byte(page.Content), 0755)
if err != nil {
return fmt.Errorf("unable to write page %s to disk at %s", page.Name, pagePath)
}

return nil
}

Expand All @@ -232,13 +243,27 @@ func (p *DocsProvider) checkChanged() (bool, error) {
return true, fmt.Errorf("unable to generate checksum for content: %s", err)
}

p.log.Info("Generate checksum", "ref", p.config.Meta.ID, "checksum", cs, "old", p.config.ContentChecksum)
if cs != p.config.ContentChecksum {
p.log.Debug("Content changed", "ref", p.config.Meta.ID, "checksum", cs, "old", p.config.ContentChecksum)
return true, nil
}

return cs != p.config.ContentChecksum, nil
return false, nil
}

func (p *DocsProvider) generateContentChecksum() (string, error) {
cs, err := utils.ChecksumFromInterface(p.config.Content)
books := deepcopy.Copy(p.config.Content).([]Book)

// replace the processed checksum as this will cause the content
// to always be different
for i := range books {
books[i].Meta.Checksum.Processed = ""
for c := range books[i].Chapters {
books[i].Chapters[c].Meta.Checksum.Processed = ""
}
}

cs, err := utils.ChecksumFromInterface(books)
if err != nil {
return "", fmt.Errorf("unable to generate checksum for content: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/jumppad/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (e *EngineImpl) Diff(path string, variables map[string]string, variablesFil
res, parseErr := e.ParseConfigWithVariables(path, variables, variablesFile)

if parseErr != nil {
fmt.Println("Error parsing config", parseErr)
// cast the error to a config error
ce := parseErr.(*hclerrors.ConfigError)

Expand Down
6 changes: 5 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ func ChecksumFromInterface(i interface{}) (string, error) {
return "", fmt.Errorf("unable to marshal interface: %w", err)
}

return HashString(string(json))
str, err := HashString(string(json))
fmt.Println("hash:", str)
fmt.Println(string(json))

return str, err
}

// RandomAvailablePort returns a random free port in the given range
Expand Down

0 comments on commit 6698e64

Please sign in to comment.