Skip to content

Commit

Permalink
feat: improve scaffold list command (#77)
Browse files Browse the repository at this point in the history
* improve scaffold list command

* restrict to .scaffold

* prevent error when file doesn't exist

* fmt
  • Loading branch information
hay-kot authored Sep 28, 2023
1 parent de06257 commit f5480c1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/commands/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Flags struct {
Cache string
OutputDir string
ScaffoldDirs []string
Cwd string
}

type Controller struct {
Expand Down
37 changes: 34 additions & 3 deletions app/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,50 @@ package commands
import (
"fmt"
"os"
"strings"

"github.com/charmbracelet/glamour"
"github.com/hay-kot/scaffold/app/scaffold/pkgs"
"github.com/urfave/cli/v2"
)

func (ctrl *Controller) List(ctx *cli.Context) error {
scaffolds, err := pkgs.List(os.DirFS(ctrl.Flags.Cache))
systemScaffolds, err := pkgs.ListSystem(os.DirFS(ctrl.Flags.Cache))
if err != nil {
return err
}

for _, s := range scaffolds {
fmt.Println(s)
localScaffolds, err := pkgs.ListLocal(os.DirFS(ctrl.Flags.OutputDir))
if err != nil {
return err
}

bldr := strings.Builder{}

if len(localScaffolds) > 0 {
bldr.WriteString("## Local Scaffolds\n")

for _, s := range localScaffolds {
bldr.WriteString(fmt.Sprintf(" - %s\n", s))
}

bldr.WriteString("\n")
}

if len(systemScaffolds) > 0 {
bldr.WriteString("## System Scaffolds\n")

for _, s := range systemScaffolds {
bldr.WriteString(fmt.Sprintf(" - %s\n", s))
}
}

our, err := glamour.RenderWithEnvironmentConfig(bldr.String())
if err != nil {
return err
}

fmt.Println(our)

return nil
}
2 changes: 1 addition & 1 deletion app/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (ctrl *Controller) Update(ctx *cli.Context) error {
ctrl.ready()

scaffolds, err := pkgs.List(os.DirFS(ctrl.Flags.Cache))
scaffolds, err := pkgs.ListSystem(os.DirFS(ctrl.Flags.Cache))
if err != nil {
return err
}
Expand Down
41 changes: 38 additions & 3 deletions app/scaffold/pkgs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,44 @@ import (
"path/filepath"
)

// List traverses the filesystem and returns a list of all the package paths
// and references
func List(f fs.FS) ([]string, error) {
func ListLocal(f fs.FS) ([]string, error) {
// .scaffold
// └── model
// └── scaffold.yaml
// └── controller
// └── scaffold.yaml

outpaths := []string{}

// Ensure that the .scaffold directory exists
// If it doesn't, return an empty slice
if _, err := fs.Stat(f, ".scaffold"); err != nil {
return outpaths, nil
}

err := fs.WalkDir(f, ".scaffold", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.Name() == "scaffold.yaml" || d.Name() == "scaffold.yml" {
outpaths = append(outpaths, filepath.Base(filepath.Dir(path)))
return filepath.SkipDir
}

return nil
})
if err != nil {
return nil, err
}

return outpaths, nil
}

// ListSystem traverses the filesystem and returns a list of all the package paths
// and references. This lists only the system scaffolds, and not the ones in the local
// .scaffold directory.
func ListSystem(f fs.FS) ([]string, error) {
// Example Structure
// Root
// └── github.com
Expand Down

0 comments on commit f5480c1

Please sign in to comment.