Skip to content

Commit

Permalink
Merge pull request #261 from Tsingxiao/master
Browse files Browse the repository at this point in the history
Add baseUrl flag for links
  • Loading branch information
k1LoW authored Nov 9, 2020
2 parents 92053f7 + 66548ff commit 61d84a3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ testdb.sqlite3
.tbls.yml
*client_secrets.json*
.DS_Store
.idea
2 changes: 2 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func loadDocArgs(args []string) ([]config.Option, error) {
if withoutER {
options = append(options, config.ERSkip(withoutER))
}
options = append(options, config.BaseUrl(baseUrl))
if len(args) == 2 {
options = append(options, config.DSNURL(args[0]))
options = append(options, config.DocPath(args[1]))
Expand Down Expand Up @@ -197,6 +198,7 @@ func init() {
docCmd.Flags().BoolVarP(&withoutER, "without-er", "", false, "no generate ER diagrams")
docCmd.Flags().BoolVarP(&adjust, "adjust-table", "j", false, "adjust column width of table")
docCmd.Flags().StringVarP(&when, "when", "", "", "command execute condition")
docCmd.Flags().StringVarP(&baseUrl, "base-url", "b", "", "base url for links")
if err := docCmd.MarkZshCompPositionalArgumentFile(2); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var erFormat string
// when is a option that command execute condition
var when string

var baseUrl string

const rootUsageTemplate = `Usage:{{if .Runnable}}{{if ne .UseLine "tbls [flags]" }}
{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Expand Down
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Config struct {
MergedDict dict.Dict `yaml:"-"`
Path string `yaml:"-"`
root string `yaml:"-"`
BaseUrl string `yaml:"baseUrl,omitempty"`
}

type DSN struct {
Expand Down Expand Up @@ -155,6 +156,16 @@ func Distance(distance int) Option {
}
}

// BaseUrl return Option set Config.BaseUrl
func BaseUrl(baseUrl string) Option {
return func(c *Config) error {
if baseUrl != "" {
c.BaseUrl = baseUrl
}
return nil
}
}

// New return Config
func New() (*Config, error) {
c := Config{}
Expand Down
8 changes: 5 additions & 3 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (m *Md) OutputSchema(wr io.Writer, s *schema.Schema) error {
templateData := m.makeSchemaTemplateData(s, m.config.Format.Adjust)
templateData["er"] = m.er
templateData["erFormat"] = m.config.ER.Format
templateData["baseUrl"] = m.config.BaseUrl
err = tmpl.Execute(wr, templateData)
if err != nil {
return errors.WithStack(err)
Expand All @@ -94,6 +95,7 @@ func (m *Md) OutputTable(wr io.Writer, t *schema.Table) error {
templateData := m.makeTableTemplateData(t, m.config.Format.Adjust)
templateData["er"] = m.er
templateData["erFormat"] = m.config.ER.Format
templateData["baseUrl"] = m.config.BaseUrl

err = tmpl.Execute(wr, templateData)
if err != nil {
Expand Down Expand Up @@ -295,7 +297,7 @@ func (m *Md) makeSchemaTemplateData(s *schema.Schema, adjust bool) map[string]in
}
for _, t := range s.Tables {
data := []string{
fmt.Sprintf("[%s](%s.md)", t.Name, t.Name),
fmt.Sprintf("[%s](%s%s.md)", t.Name, m.config.BaseUrl, t.Name),
fmt.Sprintf("%d", len(t.Columns)),
t.Comment,
t.Type,
Expand Down Expand Up @@ -337,7 +339,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table, adjust bool) map[string]inte
if _, ok := cEncountered[r.Table.Name]; ok {
continue
}
childRelations = append(childRelations, fmt.Sprintf("[%s](%s.md)", r.Table.Name, r.Table.Name))
childRelations = append(childRelations, fmt.Sprintf("[%s](%s%s.md)", r.Table.Name, m.config.BaseUrl, r.Table.Name))
cEncountered[r.Table.Name] = true
}
parentRelations := []string{}
Expand All @@ -346,7 +348,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table, adjust bool) map[string]inte
if _, ok := pEncountered[r.ParentTable.Name]; ok {
continue
}
parentRelations = append(parentRelations, fmt.Sprintf("[%s](%s.md)", r.ParentTable.Name, r.ParentTable.Name))
parentRelations = append(parentRelations, fmt.Sprintf("[%s](%s%s.md)", r.ParentTable.Name, m.config.BaseUrl, r.ParentTable.Name))
pEncountered[r.ParentTable.Name] = true
}
data := []string{
Expand Down
2 changes: 1 addition & 1 deletion output/md/templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

## {{ "Relations" | lookup }}

![er](schema.{{ .erFormat }})
![er]({{ .baseUrl }}schema.{{ .erFormat }})
{{- end }}

---
Expand Down
2 changes: 1 addition & 1 deletion output/md/templates/table.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{{- if .er -}}
## {{ "Relations" | lookup }}

![er]({{ .Table.Name }}.{{ .erFormat }})
![er]({{ .baseUrl }}{{ .Table.Name }}.{{ .erFormat }})

{{ end -}}
---
Expand Down

0 comments on commit 61d84a3

Please sign in to comment.