-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from gardener/urlmanifestpath
Read manifest from GitHub url or the file system
- Loading branch information
Showing
4 changed files
with
81 additions
and
52 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 was deleted.
Oops, something went wrong.
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,58 @@ | ||
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package app | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"html/template" | ||
"strings" | ||
|
||
"github.com/gardener/docforge/pkg/api" | ||
"github.com/gardener/docforge/pkg/resourcehandlers" | ||
) | ||
|
||
// Manifest reads the resource at uri, resolves it as template applying vars, | ||
// and finally parses it into api.Documentation model | ||
func manifest(ctx context.Context, uri string, resourceHandlers []resourcehandlers.ResourceHandler, vars map[string]string) (*api.Documentation, error) { | ||
var ( | ||
docs *api.Documentation | ||
err error | ||
blob []byte | ||
handler resourcehandlers.ResourceHandler | ||
) | ||
uri = strings.TrimSpace(uri) | ||
registry := resourcehandlers.NewRegistry(resourceHandlers...) | ||
if handler = registry.Get(uri); handler == nil { | ||
return nil, fmt.Errorf("no suitable reader found for %s. Is this path correct?", uri) | ||
} | ||
if blob, err = handler.Read(ctx, uri); err != nil { | ||
return nil, err | ||
} | ||
blob, err = resolveVariables(blob, vars) | ||
if err != nil { | ||
panic(fmt.Sprintf("%v\n", err)) | ||
} | ||
if docs, err = api.Parse(blob); err != nil { | ||
panic(fmt.Sprintf("%v\n", err)) | ||
} | ||
return docs, nil | ||
} | ||
|
||
func resolveVariables(manifestContent []byte, vars map[string]string) ([]byte, error) { | ||
var ( | ||
tmpl *template.Template | ||
err error | ||
b bytes.Buffer | ||
) | ||
if tmpl, err = template.New("").Parse(string(manifestContent)); err != nil { | ||
return nil, err | ||
} | ||
if err := tmpl.Execute(&b, vars); err != nil { | ||
return nil, err | ||
} | ||
return b.Bytes(), 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