From 2eee847205842a414080632f6fcdff3753b91053 Mon Sep 17 00:00:00 2001 From: David Allen Date: Wed, 3 Sep 2025 17:40:07 -0600 Subject: [PATCH] feat: added special case for --profile=all --- pkg/service/routes.go | 20 ++++++++++++++++++++ pkg/service/service.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/pkg/service/routes.go b/pkg/service/routes.go index 555c115..5f552d1 100644 --- a/pkg/service/routes.go +++ b/pkg/service/routes.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "slices" "strings" "git.towk2.me/towk/makeshift/internal/archive" @@ -315,6 +316,25 @@ func (s *Service) GetStatus(w http.ResponseWriter, r *http.Request) { } func (s *Service) loadProfiles(profileIDs []string, store storage.KVStore, errs []error) []error { + // check for special case profile ID (e.g. '*' or 'all') + useAll := slices.ContainsFunc(profileIDs, func(profileID string) bool { + return profileID == "*" || profileID == "all" + }) + if useAll { + var ( + dirpath = s.PathForProfiles() + profiles []*makeshift.Profile + err error + ) + profiles, err = LoadProfilesFromDir(dirpath) + if err != nil { + errs = append(errs, err) + return errs + } + store.Set("profiles", profiles) + return nil + } + // load data from profiles into the data store var profiles = make(makeshift.ProfileMap, len(profileIDs)) for i, profileID := range profileIDs { diff --git a/pkg/service/service.go b/pkg/service/service.go index 7197876..f0e0bae 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -136,6 +136,42 @@ func LoadProfileFromFile(path string) (*makeshift.Profile, error) { return loadFromJSONFile[makeshift.Profile](path) } +func LoadProfilesFromDir(dirpath string) ([]*makeshift.Profile, error) { + var ( + profiles []*makeshift.Profile + err error + ) + + // walk profiles directory to load all profiles + err = filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error { + if err != nil { + return err + } + + // skip directories + if info.IsDir() { + return nil + } + + // read file contents + var profile *makeshift.Profile + profile, err = LoadProfileFromFile(path) + if err != nil { + return err + } + + profiles = append(profiles, profile) + + fmt.Println(path, info.Size()) + return nil + }) + if err != nil { + return nil, fmt.Errorf("failed to walk directory '%s': %v", dirpath, err) + } + + return profiles, nil +} + // LoadPluginFromFile loads a single plugin given a single file path func LoadPluginFromFile(path string) (makeshift.Plugin, error) { var (