Skip to content

Commit 64b9c1e

Browse files
committed
add natefinch/atomic
1 parent daea696 commit 64b9c1e

File tree

9 files changed

+13
-72
lines changed

9 files changed

+13
-72
lines changed

completion/all.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"text/template"
1515

1616
"github.com/coder/serpent"
17+
18+
"github.com/natefinch/atomic"
1719
)
1820

1921
const (
@@ -85,7 +87,7 @@ func DetectUserShell(programName string) (Shell, error) {
8587
return nil, fmt.Errorf("default shell not found")
8688
}
8789

88-
func configTemplateWriter(
90+
func writeConfig(
8991
w io.Writer,
9092
cfgTemplate string,
9193
programName string,
@@ -114,13 +116,13 @@ func InstallShellCompletion(shell Shell) error {
114116
return fmt.Errorf("get install path: %w", err)
115117
}
116118
var headerBuf bytes.Buffer
117-
err = configTemplateWriter(&headerBuf, completionStartTemplate, shell.ProgramName())
119+
err = writeConfig(&headerBuf, completionStartTemplate, shell.ProgramName())
118120
if err != nil {
119121
return fmt.Errorf("generate header: %w", err)
120122
}
121123

122124
var footerBytes bytes.Buffer
123-
err = configTemplateWriter(&footerBytes, completionEndTemplate, shell.ProgramName())
125+
err = writeConfig(&footerBytes, completionEndTemplate, shell.ProgramName())
124126
if err != nil {
125127
return fmt.Errorf("generate footer: %w", err)
126128
}
@@ -154,7 +156,7 @@ func InstallShellCompletion(shell Shell) error {
154156
_, _ = outBuf.Write([]byte("\n"))
155157
_, _ = outBuf.Write(after)
156158

157-
err = writeWithTempFileAndMove(path, &outBuf)
159+
err = atomic.WriteFile(path, &outBuf)
158160
if err != nil {
159161
return fmt.Errorf("write completion: %w", err)
160162
}
@@ -194,46 +196,3 @@ func templateConfigSplit(header, footer, data []byte) (before, after []byte, err
194196
}
195197
return data, nil, nil
196198
}
197-
198-
// writeWithTempFileAndMove writes to a temporary file in the same
199-
// directory as path and renames the temp file to the file provided in
200-
// path. This ensure we avoid trashing the file we are writing due to
201-
// unforeseen circumstance like filesystem full, command killed, etc.
202-
func writeWithTempFileAndMove(path string, r io.Reader) (err error) {
203-
dir := filepath.Dir(path)
204-
name := filepath.Base(path)
205-
206-
if err = os.MkdirAll(dir, 0o700); err != nil {
207-
return fmt.Errorf("create directory: %w", err)
208-
}
209-
210-
// Create a tempfile in the same directory for ensuring write
211-
// operation does not fail.
212-
f, err := os.CreateTemp(dir, fmt.Sprintf(".%s.", name))
213-
if err != nil {
214-
return fmt.Errorf("create temp file failed: %w", err)
215-
}
216-
defer func() {
217-
if err != nil {
218-
_ = os.Remove(f.Name()) // Cleanup in case a step failed.
219-
}
220-
}()
221-
222-
_, err = io.Copy(f, r)
223-
if err != nil {
224-
_ = f.Close()
225-
return fmt.Errorf("write temp file failed: %w", err)
226-
}
227-
228-
err = f.Close()
229-
if err != nil {
230-
return fmt.Errorf("close temp file failed: %w", err)
231-
}
232-
233-
err = os.Rename(f.Name(), path)
234-
if err != nil {
235-
return fmt.Errorf("rename temp file failed: %w", err)
236-
}
237-
238-
return nil
239-
}

completion/bash.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ func Bash(goos string, programName string) Shell {
1818
return &bash{goos: goos, programName: programName}
1919
}
2020

21-
// Name implements Shell.
2221
func (b *bash) Name() string {
2322
return "bash"
2423
}
2524

26-
// InstallPath implements Shell.
2725
func (b *bash) InstallPath() (string, error) {
2826
homeDir, err := home.Dir()
2927
if err != nil {
@@ -35,12 +33,10 @@ func (b *bash) InstallPath() (string, error) {
3533
return filepath.Join(homeDir, ".bashrc"), nil
3634
}
3735

38-
// WriteCompletion implements Shell.
3936
func (b *bash) WriteCompletion(w io.Writer) error {
40-
return configTemplateWriter(w, bashCompletionTemplate, b.programName)
37+
return writeConfig(w, bashCompletionTemplate, b.programName)
4138
}
4239

43-
// ProgramName implements Shell.
4440
func (b *bash) ProgramName() string {
4541
return b.programName
4642
}

completion/fish.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ func Fish(goos string, programName string) Shell {
1818
return &fish{goos: goos, programName: programName}
1919
}
2020

21-
// Name implements Shell.
2221
func (f *fish) Name() string {
2322
return "fish"
2423
}
2524

26-
// InstallPath implements Shell.
2725
func (f *fish) InstallPath() (string, error) {
2826
homeDir, err := home.Dir()
2927
if err != nil {
@@ -32,12 +30,10 @@ func (f *fish) InstallPath() (string, error) {
3230
return filepath.Join(homeDir, ".config/fish/completions/", f.programName+".fish"), nil
3331
}
3432

35-
// WriteCompletion implements Shell.
3633
func (f *fish) WriteCompletion(w io.Writer) error {
37-
return configTemplateWriter(w, fishCompletionTemplate, f.programName)
34+
return writeConfig(w, fishCompletionTemplate, f.programName)
3835
}
3936

40-
// ProgramName implements Shell.
4137
func (f *fish) ProgramName() string {
4238
return f.programName
4339
}

completion/powershell.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type powershell struct {
1313

1414
var _ Shell = &powershell{}
1515

16-
// Name implements Shell.
1716
func (p *powershell) Name() string {
1817
return "powershell"
1918
}
@@ -22,7 +21,6 @@ func Powershell(goos string, programName string) Shell {
2221
return &powershell{goos: goos, programName: programName}
2322
}
2423

25-
// InstallPath implements Shell.
2624
func (p *powershell) InstallPath() (string, error) {
2725
var (
2826
path []byte
@@ -40,12 +38,10 @@ func (p *powershell) InstallPath() (string, error) {
4038
return strings.TrimSpace(string(path)), nil
4139
}
4240

43-
// WriteCompletion implements Shell.
4441
func (p *powershell) WriteCompletion(w io.Writer) error {
45-
return configTemplateWriter(w, pshCompletionTemplate, p.programName)
42+
return writeConfig(w, pshCompletionTemplate, p.programName)
4643
}
4744

48-
// ProgramName implements Shell.
4945
func (p *powershell) ProgramName() string {
5046
return p.programName
5147
}

completion/zsh.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ func Zsh(goos string, programName string) Shell {
1818
return &zsh{goos: goos, programName: programName}
1919
}
2020

21-
// Name implements Shell.
2221
func (z *zsh) Name() string {
2322
return "zsh"
2423
}
2524

26-
// InstallPath implements Shell.
2725
func (z *zsh) InstallPath() (string, error) {
2826
homeDir, err := home.Dir()
2927
if err != nil {
@@ -32,12 +30,10 @@ func (z *zsh) InstallPath() (string, error) {
3230
return filepath.Join(homeDir, ".zshrc"), nil
3331
}
3432

35-
// WriteCompletion implements Shell.
3633
func (z *zsh) WriteCompletion(w io.Writer) error {
37-
return configTemplateWriter(w, zshCompletionTemplate, z.programName)
34+
return writeConfig(w, zshCompletionTemplate, z.programName)
3835
}
3936

40-
// ProgramName implements Shell.
4137
func (z *zsh) ProgramName() string {
4238
return z.programName
4339
}

completion_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,20 @@ type fakeShell struct {
321321
programName string
322322
}
323323

324-
// ProgramName implements completion.Shell.
325324
func (f *fakeShell) ProgramName() string {
326325
return f.programName
327326
}
328327

329328
var _ completion.Shell = &fakeShell{}
330329

331-
// InstallPath implements completion.Shell.
332330
func (f *fakeShell) InstallPath() (string, error) {
333331
return filepath.Join(f.baseInstallDir, "fake.sh"), nil
334332
}
335333

336-
// Name implements completion.Shell.
337334
func (f *fakeShell) Name() string {
338335
return "Fake"
339336
}
340337

341-
// WriteCompletion implements completion.Shell.
342338
func (f *fakeShell) WriteCompletion(w io.Writer) error {
343339
_, err := w.Write([]byte("\nFAKE_COMPLETION\n"))
344340
return err

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/mitchellh/go-homedir v1.1.0
1010
github.com/mitchellh/go-wordwrap v1.0.1
1111
github.com/muesli/termenv v0.15.2
12+
github.com/natefinch/atomic v1.0.1
1213
github.com/pion/udp v0.1.4
1314
github.com/spf13/pflag v1.0.5
1415
github.com/stretchr/testify v1.8.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
5454
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
5555
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
5656
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
57+
github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
58+
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
5759
github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms=
5860
github.com/pion/transport/v2 v2.0.0 h1:bsMYyqHCbkvHwj+eNCFBuxtlKndKfyGI2vaQmM3fIE4=
5961
github.com/pion/transport/v2 v2.0.0/go.mod h1:HS2MEBJTwD+1ZI2eSXSvHJx/HnzQqRy2/LXxt6eVMHc=

values.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ type EnumArray struct {
640640
Value *[]string
641641
}
642642

643-
// Append implements pflag.SliceValue.
644643
func (e *EnumArray) Append(s string) error {
645644
for _, c := range e.Choices {
646645
if s == c {

0 commit comments

Comments
 (0)