Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Queries []QueryRef `yaml:"queries,omitempty"`
QueryFiles []string `yaml:"queryFiles,omitempty"`
Docs []DocRef `yaml:"docs,omitempty"`
Videos []string `yaml:"videos,omitempty"`
Maps []MapRef `yaml:"maps,omitempty"`
ML []MLProjectRef `yaml:"ml,omitempty"`
CostTags []CostTag `yaml:"costTags,omitempty"`
Expand Down Expand Up @@ -416,6 +417,20 @@ func Load(path string) (*Config, error) {
cfg.Queries = append(cfg.Queries, f.Queries...)
}

videos, err := videoDocs(filepath.Dir(path), cfg.Videos)
if err != nil {
return nil, err
}
listed := map[string]bool{}
for _, d := range cfg.Docs {
listed[filepath.Clean(d.Path)] = true
}
for _, d := range videos {
if !listed[filepath.Clean(d.Path)] {
cfg.Docs = append(cfg.Docs, d)
}
}

return &cfg, nil
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/config/videos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"fmt"
"path/filepath"
"strings"
)

// WalkthroughFileType is the doc file type a video's YAML is stored under. A repo does not
// write it: videos have their own top-level section. ("video" is an uploaded video file.)
const WalkthroughFileType = "walkthrough"

// videoDocs turns the videos section into docs so videos sync through the same path as other
// files. Each entry is the path of a video's YAML, relative to the config file, like the paths of
// diagrams and docs. A video is named by its title, or by its file name when it has none.
func videoDocs(root string, entries []string) ([]DocRef, error) {
var docs []DocRef
for i, entry := range entries {
if strings.TrimSpace(entry) == "" {
return nil, fmt.Errorf("videos[%d] is empty", i)
}
path := entry
if !filepath.IsAbs(path) {
path = filepath.Join(root, path)
}
var head struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
}
if err := readYAML(path, fmt.Sprintf("videos[%d] file", i), &head); err != nil {
return nil, err
}
name := head.Title
if name == "" {
base := filepath.Base(path)
name = strings.TrimSuffix(strings.TrimSuffix(base, filepath.Ext(base)), ".video")
}
docs = append(docs, DocRef{Name: name, Path: path, FileType: WalkthroughFileType, Description: head.Description})
}
return docs, nil
}
69 changes: 69 additions & 0 deletions pkg/config/videos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package config

import (
"os"
"path/filepath"
"strings"
"testing"
)

func videoRepo(t *testing.T, config string, files map[string]string) string {
t.Helper()
root := t.TempDir()
for name, body := range files {
p := filepath.Join(root, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
cfg := filepath.Join(root, ".uigraph.yaml")
if err := os.WriteFile(cfg, []byte("version: 1\nservice:\n name: Svc\n"+config), 0o644); err != nil {
t.Fatal(err)
}
return cfg
}

func TestVideosSectionListsPaths(t *testing.T) {
cfg, err := Load(videoRepo(t, "videos:\n - tours/checkout.yaml\n - login.video.yaml\n - plain.yml\n", map[string]string{
"tours/checkout.yaml": "title: How checkout works\ndescription: Cart to receipt.\nscenes: []\n",
"login.video.yaml": "scenes: []\n",
"plain.yml": "title: Plain\n",
"other.video.yaml": "title: Not listed\n", // not listed, so not synced
}))
if err != nil {
t.Fatal(err)
}
var names []string
for _, d := range cfg.Docs {
if d.FileType != WalkthroughFileType {
t.Errorf("%s has file type %q", d.Name, d.FileType)
}
names = append(names, d.Name)
}
if got := strings.Join(names, ","); got != "How checkout works,login,Plain" {
t.Fatalf("docs = %s", got)
}
if cfg.Docs[0].Description != "Cart to receipt." {
t.Errorf("description = %q", cfg.Docs[0].Description)
}
}

func TestNoVideosSectionSyncsNoVideos(t *testing.T) {
cfg, err := Load(videoRepo(t, "", map[string]string{".uigraph/videos/login.video.yaml": "title: Login\n"}))
if err != nil {
t.Fatal(err)
}
if len(cfg.Docs) != 0 {
t.Fatalf("docs: %+v", cfg.Docs)
}
}

func TestVideoThatDoesNotExistIsAnError(t *testing.T) {
_, err := Load(videoRepo(t, "videos:\n - nope.yaml\n", nil))
if err == nil || !strings.Contains(err.Error(), "videos[0] file not found") {
t.Fatalf("err = %v", err)
}
}
Loading