Skip to content

Commit

Permalink
cmd/dm: add suffix flag for checkpoint to support human readable port…
Browse files Browse the repository at this point in the history
…ion of identifier (#365)

Co-authored-by: Tony Worm <tony@topcialsource.com>
  • Loading branch information
verdverm and Tony Worm authored May 7, 2024
1 parent 1c755d0 commit 8e1969e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .hof/shadow/cli/cmd/hof/flags/datamodel__checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var _ *pflag.FlagSet
var Datamodel__CheckpointFlagSet *pflag.FlagSet

type Datamodel__CheckpointFlagpole struct {
Suffix string
Message string
}

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

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

Expand Down
2 changes: 2 additions & 0 deletions cmd/hof/flags/datamodel__checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var _ *pflag.FlagSet
var Datamodel__CheckpointFlagSet *pflag.FlagSet

type Datamodel__CheckpointFlagpole struct {
Suffix string
Message string
}

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

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

Expand Down
9 changes: 8 additions & 1 deletion design/cmds/datamodel.cue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ DatamodelCommand: schema.Command & {
//Type: "string"
//Default: "\"patch\""
//Help: "type of version bump in [major,minor,patch,<semver>]"
//}, {
// }, {
Name: "suffix"
Long: "suffix"
Short: "s"
Type: "string"
Default: "\"\""
Help: "human readable suffix for the checkpoint identifier"
}, {
Name: "message"
Long: "message"
Short: "m"
Expand Down
10 changes: 9 additions & 1 deletion lib/datamodel/cmd/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"regexp"
"time"

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

func checkpoint(R *runtime.Runtime, dflags flags.DatamodelPflagpole, cflags flags.Datamodel__CheckpointFlagpole) error {
// check suffix
suffix := cflags.Suffix
matched, _ := regexp.MatchString("^[a-z_]+$", suffix)
if !matched {
return fmt.Errorf("suffix must contain only lowercase and underscores")
}

timestamp := time.Now().UTC().Format(datamodel.CheckpointTimeFmt)
fmt.Printf("creating checkpoint: %s %q\n", timestamp, cflags.Message)
fmt.Printf("creating checkpoint: %s_%s %q\n", timestamp, suffix, cflags.Message)

for _, dm := range R.Datamodels {
err := dm.MakeSnapshot(timestamp, dflags, cflags)
Expand Down
21 changes: 11 additions & 10 deletions lib/datamodel/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func loadSnapshot(dir, fpath string, ctx *cue.Context) (*Snapshot, error) {

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

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

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

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

return nil
}

func (V *Value) makeSnapshotR(timestamp, message string) error {
func (V *Value) makeSnapshotR(timestamp, suffix, message string) error {

// load own history
if V.Hof.Datamodel.History {
err := V.makeSnapshot(timestamp, message)
err := V.makeSnapshot(timestamp, suffix, message)
if err != nil {
return err
}
}

// recurse if children to load any nested histories
for _, c := range V.Children {
err := c.T.makeSnapshotR(timestamp, message)
err := c.T.makeSnapshotR(timestamp, suffix, message)
if err != nil {
return err
}
Expand All @@ -182,7 +183,7 @@ func (V *Value) makeSnapshotR(timestamp, message string) error {
return nil
}

func (V *Value) makeSnapshot(timestamp, message string) error {
func (V *Value) makeSnapshot(timestamp, suffix, message string) error {

// if no diff, no snapshot
if len(V.history) > 0 && !V.hasDiff() {
Expand All @@ -205,7 +206,7 @@ func (V *Value) makeSnapshot(timestamp, message string) error {
pkg = V.Hof.Metadata.Name
}
pkg = strings.Replace(pkg, "-", "_", -1)
err = writeSnapshot(dir, timestamp, message, pkg, val)
err = writeSnapshot(dir, timestamp, suffix, message, pkg, val)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/datamodel/test/testdata/multiple-versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exec hof datamodel list dm-v0.cue
cmp stdout golden-v0d.stdout -trim-space

# create checkpoint
exec hof datamodel checkpoint dm-v0.cue
exec hof datamodel checkpoint dm-v0.cue -s init

# check v0 clean
exec hof datamodel list dm-v0.cue
Expand Down
4 changes: 3 additions & 1 deletion test/userfiles/main.cue
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ hello: "world"
files: {
@userfiles(content,trim=content)
@userfiles(other,trim=other)
}
}

l: [1,2,3]

0 comments on commit 8e1969e

Please sign in to comment.