LCOV - code coverage report
Current view: top level - cmd - designdoc.go Coverage Total Hit
Test: coverage.lcov Lines: 70.2 % 57 40
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package cmd
       2              : 
       3              : import (
       4              :         "context"
       5              :         "errors"
       6              :         "fmt"
       7              :         "io"
       8              :         "slices"
       9              :         "strings"
      10              :         "text/tabwriter"
      11              : 
      12              :         "github.com/kijimaD/ruins/internal/designdoc"
      13              :         "github.com/urfave/cli/v3"
      14              : )
      15              : 
      16              : // errValidation は frontmatter 検証で問題が見つかったことを表す。
      17              : var errValidation = errors.New("design document validation failed")
      18              : 
      19              : // CmdDesignDoc は設計ドキュメントの frontmatter を扱うサブコマンド
      20              : var CmdDesignDoc = &cli.Command{
      21              :         Name:        "designdoc",
      22              :         Usage:       "designdoc [gen|validate|list]",
      23              :         Description: "generate, validate, and list frontmatter under docs/design",
      24              :         Commands: []*cli.Command{
      25              :                 {
      26              :                         Name:   "gen",
      27              :                         Usage:  "deterministically add default frontmatter to documents that lack it",
      28              :                         Action: runDesignDocGen,
      29              :                 },
      30              :                 {
      31              :                         Name:   "validate",
      32              :                         Usage:  "validate frontmatter presence, validity, and consistency with progress",
      33              :                         Action: runDesignDocValidate,
      34              :                 },
      35              :                 {
      36              :                         Name:  "list",
      37              :                         Usage: "list filtered by frontmatter; assumes validate has passed; --open treats invalid status as closed and excludes it",
      38              :                         Flags: []cli.Flag{
      39              :                                 &cli.StringFlag{Name: "status", Usage: "only the specified status"},
      40              :                                 &cli.StringFlag{Name: "auto", Usage: "only the specified auto"},
      41              :                                 &cli.StringFlag{Name: "tag", Usage: "only documents containing the specified tag"},
      42              :                                 &cli.BoolFlag{Name: "open", Usage: "only actionable, that is open, status"},
      43              :                         },
      44              :                         Action: runDesignDocList,
      45              :                 },
      46              :         },
      47              : }
      48              : 
      49              : // designDocFilter は list の絞り込み条件。空文字と false は無指定を表す。
      50              : type designDocFilter struct {
      51              :         status string
      52              :         auto   string
      53              :         tag    string
      54              :         open   bool
      55              : }
      56              : 
      57              : func runDesignDocValidate(_ context.Context, cmd *cli.Command) error {
      58            0 :         return designDocValidate(cmd.Writer, designdoc.DefaultDir)
      59            0 : }
      60              : 
      61              : // designDocValidate は dir 下の frontmatter を検証し、問題を out へ書く。問題があればエラーを返す。
      62              : func designDocValidate(out io.Writer, dir string) error {
      63            1 :         docs, err := designdoc.LoadDir(dir)
      64            1 :         if err != nil {
      65            1 :                 return err
      66            1 :         }
      67              : 
      68            1 :         problems := designdoc.Validate(docs)
      69            1 :         for _, p := range problems {
      70            1 :                 _, _ = fmt.Fprintf(out, "%s: %s\n", p.Path, p.Message)
      71            1 :         }
      72              : 
      73            1 :         if len(problems) > 0 {
      74            1 :                 return errValidation
      75            1 :         }
      76            1 :         _, _ = fmt.Fprintf(out, "OK: validated %d documents\n", len(docs))
      77            1 : 
      78            1 :         return nil
      79              : }
      80              : 
      81              : func runDesignDocGen(_ context.Context, cmd *cli.Command) error {
      82            0 :         return designDocGen(cmd.Writer, designdoc.DefaultDir)
      83            0 : }
      84              : 
      85              : // designDocGen は dir 下の frontmatter を持たないドキュメントへ既定値を付与し、付与先を out へ書く。
      86              : func designDocGen(out io.Writer, dir string) error {
      87            1 :         changed, err := designdoc.BackfillDir(dir)
      88            1 :         if err != nil {
      89            0 :                 return err
      90            0 :         }
      91              : 
      92            1 :         for _, path := range changed {
      93            1 :                 _, _ = fmt.Fprintf(out, "added: %s\n", path)
      94            1 :         }
      95            1 :         _, _ = fmt.Fprintf(out, "added frontmatter to %d documents\n", len(changed))
      96            1 : 
      97            1 :         return nil
      98              : }
      99              : 
     100              : func runDesignDocList(_ context.Context, cmd *cli.Command) error {
     101            0 :         return designDocList(cmd.Writer, designdoc.DefaultDir, designDocFilter{
     102            0 :                 status: cmd.String("status"),
     103            0 :                 auto:   cmd.String("auto"),
     104            0 :                 tag:    cmd.String("tag"),
     105            0 :                 open:   cmd.Bool("open"),
     106            0 :         })
     107            0 : }
     108              : 
     109              : // designDocList は dir 下のドキュメントを filter で絞り、表として out へ書く。
     110              : func designDocList(out io.Writer, dir string, filter designDocFilter) error {
     111            1 :         docs, err := designdoc.LoadDir(dir)
     112            1 :         if err != nil {
     113            0 :                 return err
     114            0 :         }
     115              : 
     116            1 :         w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0)
     117            1 :         _, _ = fmt.Fprintln(w, "PATH\tSTATUS\tAUTO\tPROGRESS\tTAGS")
     118            1 :         for _, doc := range docs {
     119            1 :                 f := doc.Front
     120            1 :                 if filter.status != "" && string(f.Status) != filter.status {
     121            1 :                         continue
     122              :                 }
     123            1 :                 if filter.auto != "" && string(f.Auto) != filter.auto {
     124            1 :                         continue
     125              :                 }
     126            1 :                 if filter.tag != "" && !slices.Contains(f.Tags, filter.tag) {
     127            1 :                         continue
     128              :                 }
     129            1 :                 if filter.open && !f.Status.IsOpen() {
     130            1 :                         continue
     131              :                 }
     132              : 
     133            1 :                 progress := "-"
     134            1 :                 if doc.HasProgress {
     135            0 :                         progress = fmt.Sprintf("%d/%d", doc.DoneTasks, doc.DoneTasks+doc.OpenTasks)
     136            0 :                 }
     137            1 :                 _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", doc.Path, f.Status, f.Auto, progress, strings.Join(f.Tags, ","))
     138              :         }
     139              : 
     140            1 :         return w.Flush()
     141              : }
        

Generated by: LCOV version 2.0-1