File: main.go

package info (click to toggle)
trillian 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,600 kB
  • sloc: sh: 1,181; javascript: 474; sql: 330; makefile: 39
file content (98 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2023 Trillian Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// render is a command to take a Claimant Model specified as yaml and
// output markdown representations of it.
package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	claimant "github.com/google/trillian/docs/claimantmodel/experimental/cmd/render/internal"
	"gopkg.in/yaml.v2"
	"k8s.io/klog/v2"
)

var (
	dmf = flag.String("domain_model_file", "", "path to domain model yaml file")
	fmf = flag.String("full_model_file", "", "path to full model yaml file")
)

func main() {
	flag.Parse()

	if (len(*dmf) == 0) == (len(*fmf) == 0) {
		klog.Exitf("--domain_model_file OR --full_model_file are required")
	}

	if len(*dmf) > 0 {
		saved, err := os.ReadFile(*dmf)
		if err != nil {
			klog.Exitf("failed to read model: %v", err)
		}

		domain := claimant.Model{}
		if err := yaml.Unmarshal(saved, &domain); err != nil {
			klog.Exitf("failed to parse Model: %v", err)
		}
		handleSingleModel(domain)
	} else {
		saved, err := os.ReadFile(*fmf)
		if err != nil {
			klog.Exitf("failed to read model: %v", err)
		}

		models := claimant.Models{}
		if err := yaml.Unmarshal(saved, &models); err != nil {
			klog.Exitf("failed to parse Models: %v", err)
		}
		handleMultiModels(models)
	}
}

func handleSingleModel(domain claimant.Model) {
	fmt.Printf("Domain Model as markdown:\n%s\n\n", domain.Markdown())

	models := claimant.Models{
		Domain: domain,
		Log:    claimant.LogModelForDomain(domain),
	}

	mbs, err := yaml.Marshal(models)
	if err != nil {
		klog.Exitf("failed to marshal models: %v", err)
	}
	fmt.Printf("Complete model template as yaml:\n%s\n\n", string(mbs))
}

func handleMultiModels(models claimant.Models) {
	generateCommand := getGenerateDocs()
	fmt.Printf("All actors:\n%s\n\n", strings.Join(models.Actors(), "\n"))
	fmt.Printf("Models as markdown:\n%s\n%s\n\n", generateCommand, models.Markdown())
	fmt.Printf("Sequence diagrams:\n%s\n%s\n\n", generateCommand, models.SequenceDiagram())
}

func getGenerateDocs() string {
	builder := strings.Builder{}
	builder.WriteString("<!--- This content generated with:\n")
	builder.WriteString("go run github.com/google/trillian/docs/claimantmodel/experimental/cmd/render@master")
	for _, a := range os.Args[1:] {
		builder.WriteString(fmt.Sprintf(" %s", a))
	}
	builder.WriteString("\n-->")
	return builder.String()
}