-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
102 lines (90 loc) · 2.97 KB
/
Copy pathdiff.go
File metadata and controls
102 lines (90 loc) · 2.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/vista-cloud-dev/m-iris/clikit"
"github.com/vista-cloud-dev/m-iris/internal/atelier"
"github.com/vista-cloud-dev/m-iris/internal/config"
"github.com/vista-cloud-dev/m-iris/internal/udiff"
)
// syncDiffCmd shows a unified diff of one routine: the instance copy (over
// Atelier) versus the local mirror — or a --from directory. It is read-only on
// both sides (driver-contract §5.2: `{ unified }`). A side absent on the
// instance or on disk is treated as empty, so the diff renders a pure addition
// or deletion rather than erroring.
type syncDiffCmd struct {
Name string `arg:"" help:"Routine to diff (bare name or NAME.mac)."`
From string `help:"Compare the instance against this directory instead of the mirror." placeholder:"DIR"`
}
type syncDiffResult struct {
Unified string `json:"unified"`
}
func (c *syncDiffCmd) Run(cc *clikit.Context, conn *config.Conn) error {
if err := conn.Validate(config.Need{Network: true}); err != nil {
return usageErr(err)
}
name := routineFile(c.Name, conn.Type)
ctx := context.Background()
acfg, err := conn.Atelier()
if err != nil {
return usageErr(err)
}
client, err := atelier.New(acfg)
if err != nil {
return runtimeErr(err)
}
// Instance side: fetch only if the routine exists (Stat distinguishes
// not-found cleanly), so an absent routine diffs as empty rather than error.
var instLines []string
if _, exists, sErr := client.Stat(ctx, name); sErr != nil {
return runtimeErr(sErr)
} else if exists {
doc, dErr := client.GetDoc(ctx, name)
if dErr != nil {
return runtimeErr(dErr)
}
instLines = normalizeLines(doc.Content)
}
// Local side: the mirror file, or a file under --from.
localPath := conn.Layout().RoutinePath(name)
bLabel := "mirror/" + name
if c.From != "" {
localPath = filepath.Join(c.From, name)
bLabel = filepath.Join(c.From, name)
}
localBytes, lErr := os.ReadFile(localPath)
if lErr != nil && !os.IsNotExist(lErr) {
return runtimeErr(lErr)
}
u := udiff.Unified("instance/"+name, bLabel, instLines, udiff.SplitLines(string(localBytes)))
return cc.Result(syncDiffResult{Unified: u}, func() {
if u == "" {
fmt.Fprintln(cc.Stdout, cc.Success(name+": no differences"))
return
}
fmt.Fprint(cc.Stdout, u)
})
}
// normalizeLines strips any stray CR a server line carries, so line-ending
// differences don't show up as diffs (parity with mirror.WriteRoutine).
func normalizeLines(lines []string) []string {
out := make([]string, len(lines))
for i, l := range lines {
out[i] = strings.TrimRight(l, "\r\n")
}
return out
}
// routineFile normalizes a routine argument to its docname: a bare "DGREG"
// becomes "DGREG.<type>", while an argument that already carries a routine
// extension (.mac/.int/.inc) is used verbatim.
func routineFile(name, typ string) string {
for _, ext := range []string{".mac", ".int", ".inc"} {
if strings.HasSuffix(name, ext) {
return name
}
}
return name + "." + typ
}