Skip to content

Commit 8e1969e

Browse files
verdvermTony Worm
and
Tony Worm
authored
cmd/dm: add suffix flag for checkpoint to support human readable portion of identifier (#365)
Co-authored-by: Tony Worm <[email protected]>
1 parent 1c755d0 commit 8e1969e

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

.hof/shadow/cli/cmd/hof/flags/datamodel__checkpoint.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var _ *pflag.FlagSet
99
var Datamodel__CheckpointFlagSet *pflag.FlagSet
1010

1111
type Datamodel__CheckpointFlagpole struct {
12+
Suffix string
1213
Message string
1314
}
1415

@@ -17,6 +18,7 @@ var Datamodel__CheckpointFlags Datamodel__CheckpointFlagpole
1718
func SetupDatamodel__CheckpointFlags(fset *pflag.FlagSet, fpole *Datamodel__CheckpointFlagpole) {
1819
// flags
1920

21+
fset.StringVarP(&(fpole.Suffix), "suffix", "s", "", "human readable suffix for the checkpoint identifier")
2022
fset.StringVarP(&(fpole.Message), "message", "m", "", "message describing the checkpoint")
2123
}
2224

cmd/hof/flags/datamodel__checkpoint.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var _ *pflag.FlagSet
99
var Datamodel__CheckpointFlagSet *pflag.FlagSet
1010

1111
type Datamodel__CheckpointFlagpole struct {
12+
Suffix string
1213
Message string
1314
}
1415

@@ -17,6 +18,7 @@ var Datamodel__CheckpointFlags Datamodel__CheckpointFlagpole
1718
func SetupDatamodel__CheckpointFlags(fset *pflag.FlagSet, fpole *Datamodel__CheckpointFlagpole) {
1819
// flags
1920

21+
fset.StringVarP(&(fpole.Suffix), "suffix", "s", "", "human readable suffix for the checkpoint identifier")
2022
fset.StringVarP(&(fpole.Message), "message", "m", "", "message describing the checkpoint")
2123
}
2224

design/cmds/datamodel.cue

+8-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ DatamodelCommand: schema.Command & {
7171
//Type: "string"
7272
//Default: "\"patch\""
7373
//Help: "type of version bump in [major,minor,patch,<semver>]"
74-
//}, {
74+
// }, {
75+
Name: "suffix"
76+
Long: "suffix"
77+
Short: "s"
78+
Type: "string"
79+
Default: "\"\""
80+
Help: "human readable suffix for the checkpoint identifier"
81+
}, {
7582
Name: "message"
7683
Long: "message"
7784
Short: "m"

lib/datamodel/cmd/checkpoint.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"regexp"
56
"time"
67

78
"github.com/hofstadter-io/hof/cmd/hof/flags"
@@ -10,8 +11,15 @@ import (
1011
)
1112

1213
func checkpoint(R *runtime.Runtime, dflags flags.DatamodelPflagpole, cflags flags.Datamodel__CheckpointFlagpole) error {
14+
// check suffix
15+
suffix := cflags.Suffix
16+
matched, _ := regexp.MatchString("^[a-z_]+$", suffix)
17+
if !matched {
18+
return fmt.Errorf("suffix must contain only lowercase and underscores")
19+
}
20+
1321
timestamp := time.Now().UTC().Format(datamodel.CheckpointTimeFmt)
14-
fmt.Printf("creating checkpoint: %s %q\n", timestamp, cflags.Message)
22+
fmt.Printf("creating checkpoint: %s_%s %q\n", timestamp, suffix, cflags.Message)
1523

1624
for _, dm := range R.Datamodels {
1725
err := dm.MakeSnapshot(timestamp, dflags, cflags)

lib/datamodel/snapshot.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ func loadSnapshot(dir, fpath string, ctx *cue.Context) (*Snapshot, error) {
8989

9090
// we probably want this to be closer to `cue def`
9191
// with imports, schemas, and such... indep eval'able
92-
func writeSnapshot(dir, fname, message, pkgId string, V cue.Value) error {
93-
ver := fmt.Sprintf("ver_%s", fname)
94-
msg := fmt.Sprintf("msg_%s", fname)
92+
func writeSnapshot(dir, fname, suffix, message, pkgId string, V cue.Value) error {
93+
sname := fmt.Sprintf("%s_%s", fname, suffix)
94+
ver := fmt.Sprintf("ver_%s", sname)
95+
msg := fmt.Sprintf("msg_%s", sname)
9596

9697
err := V.Validate(cue.Concrete(true))
9798
if err != nil {
@@ -142,7 +143,7 @@ func writeSnapshot(dir, fname, message, pkgId string, V cue.Value) error {
142143

143144
// write file
144145
str := string(bytes)
145-
fn := filepath.Join(dir, fmt.Sprintf("%s.cue", fname))
146+
fn := filepath.Join(dir, fmt.Sprintf("%s.cue", sname))
146147
err = os.WriteFile(fn, []byte(str), 0666)
147148
if err != nil {
148149
return err
@@ -153,27 +154,27 @@ func writeSnapshot(dir, fname, message, pkgId string, V cue.Value) error {
153154

154155
// MakeSnapshot creates a new snapshot for each history annotation in the Datamodel tree
155156
func (dm *Datamodel) MakeSnapshot(timestamp string, dflags flags.DatamodelPflagpole, cflags flags.Datamodel__CheckpointFlagpole) error {
156-
err := dm.T.makeSnapshotR(timestamp, cflags.Message)
157+
err := dm.T.makeSnapshotR(timestamp, cflags.Suffix, cflags.Message)
157158
if err != nil {
158159
return err
159160
}
160161

161162
return nil
162163
}
163164

164-
func (V *Value) makeSnapshotR(timestamp, message string) error {
165+
func (V *Value) makeSnapshotR(timestamp, suffix, message string) error {
165166

166167
// load own history
167168
if V.Hof.Datamodel.History {
168-
err := V.makeSnapshot(timestamp, message)
169+
err := V.makeSnapshot(timestamp, suffix, message)
169170
if err != nil {
170171
return err
171172
}
172173
}
173174

174175
// recurse if children to load any nested histories
175176
for _, c := range V.Children {
176-
err := c.T.makeSnapshotR(timestamp, message)
177+
err := c.T.makeSnapshotR(timestamp, suffix, message)
177178
if err != nil {
178179
return err
179180
}
@@ -182,7 +183,7 @@ func (V *Value) makeSnapshotR(timestamp, message string) error {
182183
return nil
183184
}
184185

185-
func (V *Value) makeSnapshot(timestamp, message string) error {
186+
func (V *Value) makeSnapshot(timestamp, suffix, message string) error {
186187

187188
// if no diff, no snapshot
188189
if len(V.history) > 0 && !V.hasDiff() {
@@ -205,7 +206,7 @@ func (V *Value) makeSnapshot(timestamp, message string) error {
205206
pkg = V.Hof.Metadata.Name
206207
}
207208
pkg = strings.Replace(pkg, "-", "_", -1)
208-
err = writeSnapshot(dir, timestamp, message, pkg, val)
209+
err = writeSnapshot(dir, timestamp, suffix, message, pkg, val)
209210
if err != nil {
210211
return err
211212
}

lib/datamodel/test/testdata/multiple-versions.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exec hof datamodel list dm-v0.cue
77
cmp stdout golden-v0d.stdout -trim-space
88

99
# create checkpoint
10-
exec hof datamodel checkpoint dm-v0.cue
10+
exec hof datamodel checkpoint dm-v0.cue -s init
1111

1212
# check v0 clean
1313
exec hof datamodel list dm-v0.cue

test/userfiles/main.cue

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ hello: "world"
55
files: {
66
@userfiles(content,trim=content)
77
@userfiles(other,trim=other)
8-
}
8+
}
9+
10+
l: [1,2,3]

0 commit comments

Comments
 (0)