Deployr 0.1.0: cross-platform application deployment CLI
Replaces Ninite for endpoint software: one catalog, one command line, and the same behaviour on Windows, macOS and Linux. Single static binary, no server and no agent, driven from an RMM script runner or by hand. - catalog of ~50 apps compiled into the binary, extendable with YAML overlays in catalog.d or via --catalog, no rebuild required - providers: winget, choco, brew, cask, apt, dnf/yum, zypper, pacman, flatpak, snap, plus direct vendor downloads (msi/exe/msu/dmg/pkg/deb/rpm/zip/tar.gz/ AppImage) - sources are tried in order and fall through, so one catalog entry covers winget on Windows, a cask on macOS and the vendor .deb on Debian - provider-independent detection (registry, paths, commands) makes runs idempotent even for software installed by hand - unattended by construction: stdin closed, silent flags, per-command timeouts - handles the traps that break naive wrappers: winget missing under SYSTEM, Homebrew refusing root, 1641/3010 reboot exits, "already installed" reported as failure - --json summary on stdout with progress on stderr, and RMM-friendly exit codes (0 ok, 1 failed, 2 usage, 3 privileges) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
commit
4909edee2d
40 files changed
+6714
No files matched your search
@@ -0,0 +1,245 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/platform"
|
||||
)
|
||||
|
||||
//go:embed data/apps.yaml
|
||||
var embeddedYAML []byte
|
||||
|
||||
// Parse reads a catalog document.
|
||||
func Parse(data []byte, origin string) (*Catalog, error) {
|
||||
var c Catalog
|
||||
dec := yaml.NewDecoder(strings.NewReader(string(data)))
|
||||
dec.KnownFields(true)
|
||||
if err := dec.Decode(&c); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", origin, err)
|
||||
}
|
||||
if err := c.validate(origin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// Embedded returns the catalog compiled into the binary.
|
||||
func Embedded() (*Catalog, error) {
|
||||
c, err := Parse(embeddedYAML, "embedded catalog")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.reindex()
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Load returns the embedded catalog with overlays merged over it. Overlays are
|
||||
// read from the well-known catalog.d directories and then from extra (files or
|
||||
// directories), so an operator can add or override apps without rebuilding.
|
||||
func Load(extra []string) (*Catalog, error) {
|
||||
base, err := Embedded()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var files []string
|
||||
for _, dir := range platform.CatalogDirs() {
|
||||
files = append(files, yamlFilesIn(dir)...)
|
||||
}
|
||||
for _, p := range extra {
|
||||
p = platform.ExpandPath(p)
|
||||
info, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("catalog %q: %w", p, err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
files = append(files, yamlFilesIn(p)...)
|
||||
} else {
|
||||
files = append(files, p)
|
||||
}
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
data, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("catalog %q: %w", f, err)
|
||||
}
|
||||
overlay, err := Parse(data, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base.merge(overlay)
|
||||
}
|
||||
|
||||
base.reindex()
|
||||
return base, nil
|
||||
}
|
||||
|
||||
func yamlFilesIn(dir string) []string {
|
||||
var out []string
|
||||
for _, pattern := range []string{"*.yaml", "*.yml"} {
|
||||
matches, err := filepath.Glob(filepath.Join(dir, pattern))
|
||||
if err == nil {
|
||||
out = append(out, matches...)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// merge applies an overlay: apps are replaced whole by ID, profiles by name.
|
||||
// Replacing rather than deep-merging keeps overrides predictable — what you
|
||||
// read in the overlay file is exactly what will run.
|
||||
func (c *Catalog) merge(overlay *Catalog) {
|
||||
for _, app := range overlay.Apps {
|
||||
replaced := false
|
||||
for i, existing := range c.Apps {
|
||||
if strings.EqualFold(existing.ID, app.ID) {
|
||||
c.Apps[i] = app
|
||||
replaced = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
c.Apps = append(c.Apps, app)
|
||||
}
|
||||
}
|
||||
if len(overlay.Profiles) > 0 && c.Profiles == nil {
|
||||
c.Profiles = map[string][]string{}
|
||||
}
|
||||
for name, apps := range overlay.Profiles {
|
||||
c.Profiles[name] = apps
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Catalog) validate(origin string) error {
|
||||
seen := map[string]bool{}
|
||||
for _, app := range c.Apps {
|
||||
switch {
|
||||
case app.ID == "":
|
||||
return fmt.Errorf("%s: an app entry is missing an id", origin)
|
||||
case seen[strings.ToLower(app.ID)]:
|
||||
return fmt.Errorf("%s: duplicate app id %q", origin, app.ID)
|
||||
case app.Name == "":
|
||||
return fmt.Errorf("%s: app %q is missing a name", origin, app.ID)
|
||||
case len(app.Platforms()) == 0:
|
||||
return fmt.Errorf("%s: app %q defines no platform targets", origin, app.ID)
|
||||
}
|
||||
seen[strings.ToLower(app.ID)] = true
|
||||
|
||||
for _, goos := range app.Platforms() {
|
||||
t := app.TargetFor(goos)
|
||||
if len(t.Sources) == 0 {
|
||||
return fmt.Errorf("%s: app %q has no %s sources", origin, app.ID, goos)
|
||||
}
|
||||
for i, s := range t.Sources {
|
||||
if s.Provider == "" {
|
||||
return fmt.Errorf("%s: app %q %s source %d has no provider", origin, app.ID, goos, i+1)
|
||||
}
|
||||
if s.Provider == "direct" && s.URL == "" && len(s.URLs) == 0 {
|
||||
return fmt.Errorf("%s: app %q %s direct source has no url", origin, app.ID, goos)
|
||||
}
|
||||
if s.Provider != "direct" && s.ID == "" {
|
||||
return fmt.Errorf("%s: app %q %s %s source has no id", origin, app.ID, goos, s.Provider)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Catalog) reindex() {
|
||||
c.index = make(map[string]*App, len(c.Apps)*2)
|
||||
for _, app := range c.Apps {
|
||||
c.index[strings.ToLower(app.ID)] = app
|
||||
for _, alias := range app.Aliases {
|
||||
c.index[strings.ToLower(alias)] = app
|
||||
}
|
||||
}
|
||||
sort.Slice(c.Apps, func(i, j int) bool {
|
||||
return strings.ToLower(c.Apps[i].ID) < strings.ToLower(c.Apps[j].ID)
|
||||
})
|
||||
}
|
||||
|
||||
// Get looks up an app by ID or alias.
|
||||
func (c *Catalog) Get(name string) (*App, bool) {
|
||||
app, ok := c.index[strings.ToLower(strings.TrimSpace(name))]
|
||||
return app, ok
|
||||
}
|
||||
|
||||
// Resolve expands a list of command-line names into apps. A name prefixed with
|
||||
// "@" refers to a profile. Unknown names are returned separately so the caller
|
||||
// can report all of them at once rather than dying on the first.
|
||||
func (c *Catalog) Resolve(names []string) (apps []*App, unknown []string) {
|
||||
seen := map[string]bool{}
|
||||
add := func(app *App) {
|
||||
if !seen[app.ID] {
|
||||
seen[app.ID] = true
|
||||
apps = append(apps, app)
|
||||
}
|
||||
}
|
||||
|
||||
for _, raw := range names {
|
||||
name := strings.TrimSpace(raw)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(name, "@") {
|
||||
members, ok := c.Profiles[strings.TrimPrefix(name, "@")]
|
||||
if !ok {
|
||||
unknown = append(unknown, name)
|
||||
continue
|
||||
}
|
||||
for _, m := range members {
|
||||
if app, ok := c.Get(m); ok {
|
||||
add(app)
|
||||
} else {
|
||||
unknown = append(unknown, m)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if app, ok := c.Get(name); ok {
|
||||
add(app)
|
||||
continue
|
||||
}
|
||||
unknown = append(unknown, name)
|
||||
}
|
||||
return apps, unknown
|
||||
}
|
||||
|
||||
// ForPlatform returns every app installable on the given GOOS.
|
||||
func (c *Catalog) ForPlatform(goos string) []*App {
|
||||
var out []*App
|
||||
for _, app := range c.Apps {
|
||||
if app.TargetFor(goos) != nil {
|
||||
out = append(out, app)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Suggest returns catalog IDs similar to name, for "did you mean" output.
|
||||
func (c *Catalog) Suggest(name string) []string {
|
||||
name = strings.ToLower(strings.TrimPrefix(name, "@"))
|
||||
var out []string
|
||||
for _, app := range c.Apps {
|
||||
id := strings.ToLower(app.ID)
|
||||
if strings.Contains(id, name) || strings.Contains(name, id) ||
|
||||
strings.Contains(strings.ToLower(app.Name), name) {
|
||||
out = append(out, app.ID)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
if len(out) > 5 {
|
||||
out = out[:5]
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user