Skip to content

Commit 4f03415

Browse files
committedJun 8, 2022
chore: Switch to github.com/pkg/diff for diffing
1 parent b7f2b3d commit 4f03415

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed
 

‎cli/configssh.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"io"
88
"io/fs"
99
"os"
10-
"os/exec"
1110
"path/filepath"
1211
"runtime"
1312
"sort"
1413
"strings"
1514

1615
"github.com/cli/safeexec"
16+
"github.com/pkg/diff"
17+
"github.com/pkg/diff/write"
1718
"github.com/spf13/cobra"
1819
"golang.org/x/exp/slices"
1920
"golang.org/x/sync/errgroup"
@@ -383,40 +384,27 @@ func currentBinPath(cmd *cobra.Command) (string, error) {
383384

384385
// diffBytes two byte slices as if they were in a file named name.
385386
// Does best-effort cleanup ignoring non-critical errors.
386-
func diffBytes(name string, b1, b2 []byte) (data []byte, err error) {
387-
f1, err := os.CreateTemp("", "coder_config-ssh.")
388-
if err != nil {
389-
return nil, xerrors.Errorf("create temp 1 file failed: %w", err)
390-
}
391-
defer os.Remove(f1.Name())
392-
defer f1.Close()
393-
394-
f2, err := os.CreateTemp("", "coder_config-ssh.")
395-
if err != nil {
396-
return nil, xerrors.Errorf("create temp 2 file failed: %w", err)
387+
func diffBytes(name string, b1, b2 []byte) ([]byte, error) {
388+
var buf bytes.Buffer
389+
var opts []write.Option
390+
// TODO(mafredri): Toggle color on/off
391+
if false {
392+
opts = append(opts, write.TerminalColor())
397393
}
398-
defer os.Remove(f2.Name())
399-
defer f2.Close()
400-
401-
_, err = f1.Write(b1)
394+
err := diff.Text(name, name+".new", b1, b2, &buf, opts...)
402395
if err != nil {
403-
return nil, xerrors.Errorf("write temp 1 file failed: %w", err)
404-
}
405-
_, err = f2.Write(b2)
406-
if err != nil {
407-
return nil, xerrors.Errorf("write temp 2 file failed: %w", err)
408-
}
409-
410-
// TODO(mafredri): Ensure diff binary exists, or return useful error when missing.
411-
data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).Output() // #nosec
412-
if len(data) == 0 && err != nil { // Ignore non-zero exit when files differ.
413396
return nil, err
414397
}
415-
// Replace temp file names with friendly names.
416-
data = bytes.Replace(data, []byte(f1.Name()), []byte(name), 1)
417-
data = bytes.Replace(data, []byte(f2.Name()), []byte(name+".new"), 1)
418-
419-
return data, err
398+
b := buf.Bytes()
399+
// Check if diff only output two lines, if yes, there's no diff.
400+
//
401+
// Example:
402+
// --- ~/.ssh/config
403+
// +++ ~/.ssh/config.new
404+
if bytes.Count(b, []byte{'\n'}) == 2 {
405+
b = nil
406+
}
407+
return b, nil
420408
}
421409

422410
// stripOldConfigBlock is here to migrate users from old config block

‎go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ require (
9393
github.com/pion/udp v0.1.1
9494
github.com/pion/webrtc/v3 v3.1.41
9595
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
96+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
9697
github.com/pkg/sftp v1.13.4
9798
github.com/prometheus/client_golang v1.12.2
9899
github.com/quasilyte/go-ruleguard/dsl v0.3.21

‎go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9G
13361336
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
13371337
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
13381338
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
1339+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
13391340
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
13401341
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
13411342
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 commit comments

Comments
 (0)