Skip to content

Commit

Permalink
wip: use local relative paths in state files
Browse files Browse the repository at this point in the history
Use local relative paths for Document objects in the state file. When
comparing against and syncing to Konnect state, strip these local paths
and use only the base filename with a prepended slash to match the path
value format created by the Konnect GUI.
  • Loading branch information
Travis Raines committed May 11, 2021
1 parent 3ec5788 commit 19ddb57
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func syncKonnect(ctx context.Context,
return err
}

targetContent.StripLocalDocumentPath()

// get Konnect client
konnectClient, err := utils.GetKonnectClient(httpClient, konnectConfig.Debug)
if err != nil {
Expand Down
26 changes: 21 additions & 5 deletions file/konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ func (c Content) PopulateDocumentContent(filenames []string) error {
return errors.New("cannot populate documents without a location")
}
// TODO decK actually allows you to use _multiple_ state files
// How should we choose which to use as the search path for documents?
// We currently choose the first arbitrarily and assume document content is under its directory
// Future plans are to rework the multiple state file functionality to require all state files
// be in the same directory.
root := filepath.Dir(filenames[0])
for _, sp := range c.ServicePackages {
spPath := utils.NameToFilename(*sp.Name)
if sp.Document != nil {
path := filepath.Join(root, spPath, utils.NameToFilename(*sp.Document.Path))
path := filepath.Join(root, utils.FilenameToName(*sp.Document.Path))
content, err := os.ReadFile(path)
if err != nil {
return errors.Wrap(err, "error reading document file")
Expand All @@ -31,8 +32,7 @@ func (c Content) PopulateDocumentContent(filenames []string) error {
}
for _, sv := range sp.Versions {
if sv.Document != nil {
path := filepath.Join(root, spPath, utils.NameToFilename(*sv.Version),
utils.NameToFilename(*sv.Document.Path))
path := filepath.Join(root, utils.FilenameToName(*sv.Document.Path))
content, err := os.ReadFile(path)
if err != nil {
return errors.Wrap(err, "error reading document file")
Expand All @@ -43,3 +43,19 @@ func (c Content) PopulateDocumentContent(filenames []string) error {
}
return nil
}

// StripLocalDocum
func (c Content) StripLocalDocumentPath() {
for _, sp := range c.ServicePackages {
if sp.Document != nil {
trunc := "/" + filepath.Base(utils.FilenameToName(*sp.Document.Path))
sp.Document.Path = &trunc
}
for _, sv := range sp.Versions {
if sv.Document != nil {
trunc := "/" + filepath.Base(utils.FilenameToName(*sv.Document.Path))
sv.Document.Path = &trunc
}
}
}
}
21 changes: 11 additions & 10 deletions file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
ghodss "github.com/ghodss/yaml"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -132,6 +133,7 @@ func populateServicePackages(kongState *state.KongState, file *Content,
}

for _, sp := range packages {
safePackageName := utils.NameToFilename(*sp.Name)
p := FServicePackage{
ID: sp.ID,
Name: sp.Name,
Expand All @@ -147,9 +149,10 @@ func populateServicePackages(kongState *state.KongState, file *Content,
}

for _, d := range documents {
safeDocPath := utils.NameToFilename(*d.Path)
fDocument := FDocument{
ID: d.ID,
Path: d.Path,
Path: kong.String(filepath.Join(safePackageName, safeDocPath)),
Published: d.Published,
Content: d.Content,
}
Expand All @@ -161,6 +164,7 @@ func populateServicePackages(kongState *state.KongState, file *Content,
}

for _, v := range versions {
safeVersionName := utils.NameToFilename(*v.Version)
fVersion := FServiceVersion{
ID: v.ID,
Version: v.Version,
Expand All @@ -186,9 +190,10 @@ func populateServicePackages(kongState *state.KongState, file *Content,
}

for _, d := range documents {
safeDocPath := utils.NameToFilename(*d.Path)
fDocument := FDocument{
ID: d.ID,
Path: d.Path,
Path: kong.String(filepath.Join(safePackageName, safeVersionName, safeDocPath)),
Published: d.Published,
Content: d.Content,
}
Expand Down Expand Up @@ -627,25 +632,21 @@ func writeFile(content *Content, filename string, format Format) error {
return errors.Wrap(err, "writing file")
}
for _, sp := range content.ServicePackages {
safePackageName := utils.NameToFilename(*sp.Name)
if sp.Document != nil {
if err := os.MkdirAll(filepath.Join(prefix, safePackageName), 0700); err != nil {
if err := os.MkdirAll(filepath.Join(prefix, filepath.Dir(*sp.Document.Path)), 0700); err != nil {
return errors.Wrap(err, "creating document directory")
}
safeDocPath := utils.NameToFilename(*sp.Document.Path)
if err := os.WriteFile(filepath.Join(prefix, safePackageName, safeDocPath),
if err := os.WriteFile(filepath.Join(prefix, *sp.Document.Path),
[]byte(*sp.Document.Content), 0600); err != nil {
return errors.Wrap(err, "writing document file")
}
}
for _, v := range sp.Versions {
if v.Document != nil {
safeVersionName := utils.NameToFilename(*v.Version)
if err := os.MkdirAll(filepath.Join(prefix, safePackageName, safeVersionName), 0700); err != nil {
if err := os.MkdirAll(filepath.Join(prefix, filepath.Dir(*v.Document.Path)), 0700); err != nil {
return errors.Wrap(err, "creating document directory")
}
safeDocPath := utils.NameToFilename(*v.Document.Path)
if err := os.WriteFile(filepath.Join(prefix, safePackageName, safeVersionName, safeDocPath),
if err := os.WriteFile(filepath.Join(prefix, *v.Document.Path),
[]byte(*v.Document.Content), 0600); err != nil {
return errors.Wrap(err, "writing document file")
}
Expand Down
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ func NameToFilename(name string) string {
s = strings.ReplaceAll(s, string(os.PathSeparator), url.PathEscape(string(os.PathSeparator)))
return s
}

// FilenameToName (partially) reverses NameToFilename, replacing all URL-encoded path separator characters
// with the path separator character. It does not re-add a leading separator, because there is no way to know
// if that separator was included originally, and only some names (document paths) typically include one.
func FilenameToName(filename string) string {
return strings.ReplaceAll(filename, url.PathEscape(string(os.PathSeparator)), string(os.PathSeparator))
}

0 comments on commit 19ddb57

Please sign in to comment.