graphql-engine/cli/util/template.go
Shahidh K Muhammed f6a43fe3ba
fix various version-related issues with cli (close #3706) (#3709)
- do not quit on server-cli version mismatch, show warning
- fix a bug in update-cli command which prevets updates to pre-release
versions
- if a console template for a particular version is not found, use the
latest template
2020-01-16 09:23:18 +05:30

69 lines
1.4 KiB
Go

package util
import (
"html/template"
"net/http"
"strings"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gin-gonic/contrib/renders/multitemplate"
"github.com/hasura/graphql-engine/cli/assets"
)
type binaryFileSystem struct {
fs http.FileSystem
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
return b.fs.Open(name)
}
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
if _, err := b.fs.Open(p); err != nil {
return false
}
return true
}
return false
}
// BinaryFileSystem creates a binary file system at root from the assets
func BinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{assets.Asset, assets.AssetDir, assets.AssetInfo, root}
return &binaryFileSystem{
fs,
}
}
// LoadTemplates loads templates from path for the given list
func LoadTemplates(path string, list ...string) (multitemplate.Render, error) {
r := multitemplate.New()
for _, x := range list {
templateString, err := assets.Asset(path + x)
if err != nil {
return nil, err
}
tmplMessage, err := template.New(x).Parse(string(templateString))
if err != nil {
return nil, err
}
r.Add(x, tmplMessage)
}
return r, nil
}
// DoAssetExist returns true if an asset exists at pathk
func DoAssetExist(path string) bool {
_, err := assets.AssetInfo(path)
if err != nil {
return false
}
return true
}