Skip to content

Commit

Permalink
feat: support running generators via path name (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs authored Nov 19, 2023
1 parent 2cd3de6 commit e902b0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
15 changes: 12 additions & 3 deletions internal/cmd/new.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -99,11 +100,19 @@ func (a *NewAction) Run() error {
}
}

// Load the generator.
generator, err := a.Store.Load(a.Name)
if err != nil {
// The name may be a direct path to a generator on the filesystem.
generator, err := stamp.NewGeneratorFromPath(a.Store, a.Name)
if err != nil && !errors.Is(err, stamp.ErrNotFound) {
return err
}
// If that doesn't return a result, then it must be a named
// generator in the store...
if generator == nil {
generator, err = a.Store.Load(a.Name)
if err != nil {
return err
}
}

// Update usage text w/ info from generator.
a.setUsage(generator)
Expand Down
18 changes: 18 additions & 0 deletions internal/stamp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

"github.com/twelvelabs/termite/render"

"github.com/twelvelabs/stamp/internal/fsutil"
"github.com/twelvelabs/stamp/internal/pkg"
"github.com/twelvelabs/stamp/internal/value"
)

var (
ErrNilPackage = errors.New("nil package")
ErrNilStore = errors.New("nil store")
ErrNotFound = errors.New("generator not found")

metaFileName = "generator.yaml"
)
Expand Down Expand Up @@ -75,6 +77,22 @@ func GeneratorNameForCreate(path string) string {
return strings.Join(segments[idx:], ":")
}

// NewGeneratorFromPath returns the generator located at path (or ErrNotFound).
func NewGeneratorFromPath(store *Store, path string) (*Generator, error) {
if !fsutil.PathIsDir(path) {
return nil, ErrNotFound
}
if fsutil.NoPathExists(filepath.Join(path, store.MetaFile)) {
return nil, ErrNotFound
}

p, err := pkg.LoadPackage(path, store.MetaFile)
if err != nil {
return nil, err
}
return NewGenerator(store, p)
}

func NewGenerator(store *Store, pkg *pkg.Package) (*Generator, error) {
if store == nil {
return nil, ErrNilStore
Expand Down
8 changes: 5 additions & 3 deletions internal/stamp/update_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ func (t *UpdateTask) Execute(ctx *TaskContext, values map[string]any) error {
ctx.Logger.Failure("fail", t.Dst.Path())
return ErrPathNotFound
case MissingConfigTouch:
if err := os.WriteFile(t.Dst.Path(), []byte{}, DstFileMode); err != nil {
ctx.Logger.Failure("fail", t.Dst.Path())
return err
if !ctx.DryRun {
if err := os.WriteFile(t.Dst.Path(), []byte{}, DstFileMode); err != nil {
ctx.Logger.Failure("fail", t.Dst.Path())
return err
}
}
default: // MissingConfigIgnore:
return nil
Expand Down

0 comments on commit e902b0b

Please sign in to comment.