-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.go
More file actions
67 lines (59 loc) · 1.47 KB
/
format.go
File metadata and controls
67 lines (59 loc) · 1.47 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
61
62
63
64
65
66
67
package cli
import (
"context"
"fmt"
"io"
"os"
"github.com/deref/transcript/internal/core"
"github.com/natefinch/atomic"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(formatCmd)
}
var formatCmd = &cobra.Command{
Use: "format [transcripts...]",
Short: "Formats transcript files",
Long: `Formats transcript files by normalizing comments, blank lines,
trailing whitespace (except in command output), trailing newline,
and special directive syntax.
If no files are provided, reads from stdin and writes to stdout.
If files are provided, formats them in-place.
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) == 0 {
// Read from stdin, write to stdout
return formatStdin(ctx)
}
// Format files in-place
for _, filename := range args {
if err := formatFile(ctx, filename); err != nil {
return fmt.Errorf("formatting %q: %w", filename, err)
}
}
return nil
},
}
func formatStdin(ctx context.Context) error {
formatter := &core.Formatter{}
transcript, err := formatter.FormatTranscript(ctx, os.Stdin)
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, transcript)
return err
}
func formatFile(ctx context.Context, filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
formatter := &core.Formatter{}
transcript, err := formatter.FormatTranscript(ctx, f)
if err != nil {
return err
}
return atomic.WriteFile(filename, transcript)
}