-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexport.go
More file actions
60 lines (52 loc) · 1.86 KB
/
Copy pathexport.go
File metadata and controls
60 lines (52 loc) · 1.86 KB
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
package cli
import (
"encoding/json"
"fmt"
"os"
"github.com/glincker/stacklit/internal/renderer"
"github.com/glincker/stacklit/internal/schema"
"github.com/spf13/cobra"
)
func newExportCmd() *cobra.Command {
var format, source, output string
cmd := &cobra.Command{
Use: "export",
Short: "Export the index in a human-readable format",
Long: `Renders stacklit.json in a different format for sharing.
Currently supports markdown, which produces a readable project overview
suitable for pasting into PR descriptions, GitHub issues, or chat.
Examples:
stacklit export Print markdown to stdout
stacklit export --format md Same as above
stacklit export -o stacklit.md Write markdown to a file
stacklit export --source ./other.json Read from a non-default index`,
RunE: func(cmd *cobra.Command, args []string) error {
data, err := os.ReadFile(source)
if err != nil {
return fmt.Errorf("could not read %s: %w (run 'stacklit generate' first, or 'stacklit init')", source, err)
}
var idx schema.Index
if err := json.Unmarshal(data, &idx); err != nil {
return fmt.Errorf("could not parse %s: %w", source, err)
}
switch format {
case "md", "markdown":
if output == "" {
fmt.Print(renderer.RenderMarkdown(&idx))
return nil
}
if err := renderer.WriteMarkdown(&idx, output); err != nil {
return fmt.Errorf("could not write %s: %w", output, err)
}
fmt.Fprintf(cmd.OutOrStderr(), "Wrote %s\n", output)
return nil
default:
return fmt.Errorf("unknown format %q (supported: md, markdown)", format)
}
},
}
cmd.Flags().StringVar(&format, "format", "md", "Output format (md)")
cmd.Flags().StringVar(&source, "source", "stacklit.json", "Path to the index JSON")
cmd.Flags().StringVarP(&output, "output", "o", "", "Write to file instead of stdout")
return cmd
}