-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoutputter.go
More file actions
58 lines (54 loc) · 1.5 KB
/
outputter.go
File metadata and controls
58 lines (54 loc) · 1.5 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
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/sei-protocol/seictl/internal/patch"
"github.com/urfave/cli/v3"
)
var outputterFlags = cli.MutuallyExclusiveFlags{
Required: false,
Flags: [][]cli.Flag{
{
&cli.StringFlag{
Name: "output",
DefaultText: "STDOUT",
Usage: "The destination to which to write the modified configuration file.",
Destination: &destinations.outputter.output,
Aliases: []string{"o"},
TakesFile: true,
Action: func(ctx context.Context, command *cli.Command, s string) error {
return command.Set("output", filepath.Clean(s))
},
OnlyOnce: true,
},
},
{
&cli.BoolFlag{
Name: "in-place-rewrite",
Usage: "Weather to directly replace the resulting changes in the current file. Cannot be set in conjunction with output.",
Destination: &destinations.outputter.inPlace,
Aliases: []string{"i"},
OnlyOnce: true,
},
},
},
}
func output(originalPath string, content bytes.Buffer) error {
switch {
case destinations.outputter.inPlace:
stat, err := os.Stat(originalPath)
if err != nil {
return fmt.Errorf("failed to get config file stat %s: %w", originalPath, err)
}
return patch.WriteFileAtomic(originalPath, content.Bytes(), stat.Mode().Perm())
case destinations.outputter.output != "":
return patch.WriteFileAtomic(destinations.outputter.output, content.Bytes(), 0600)
default:
_, err := io.Copy(os.Stdout, &content)
return err
}
}