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,242 @@
|
||||
// Package cli parses the command line and wires the catalog, providers and
|
||||
// engine together.
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/catalog"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/logx"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/platform"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/version"
|
||||
)
|
||||
|
||||
// Exit codes. These are the contract with whatever script calls deployr.
|
||||
const (
|
||||
// ExitOK means every requested app ended in the desired state.
|
||||
ExitOK = 0
|
||||
// ExitFailure means at least one app could not be handled.
|
||||
ExitFailure = 1
|
||||
// ExitUsage means the command line or catalog was wrong.
|
||||
ExitUsage = 2
|
||||
// ExitPrivilege means deployr does not have the rights to install software.
|
||||
ExitPrivilege = 3
|
||||
)
|
||||
|
||||
// globals are the flags every action-taking command accepts.
|
||||
type globals struct {
|
||||
json bool
|
||||
dryRun bool
|
||||
verbose bool
|
||||
quiet bool
|
||||
force bool
|
||||
strict bool
|
||||
requireChecksum bool
|
||||
keepDownloads bool
|
||||
allowUnelevated bool
|
||||
only string
|
||||
logFile string
|
||||
noLog bool
|
||||
timeout time.Duration
|
||||
catalogs multiFlag
|
||||
}
|
||||
|
||||
type multiFlag []string
|
||||
|
||||
func (m *multiFlag) String() string { return strings.Join(*m, ",") }
|
||||
func (m *multiFlag) Set(v string) error {
|
||||
*m = append(*m, v)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *globals) register(fs *flag.FlagSet) {
|
||||
fs.BoolVar(&g.json, "json", false, "emit a machine-readable summary on stdout")
|
||||
fs.BoolVar(&g.dryRun, "dry-run", false, "show what would happen without changing anything")
|
||||
fs.BoolVar(&g.verbose, "verbose", false, "log every command that is run")
|
||||
fs.BoolVar(&g.quiet, "quiet", false, "only report failures")
|
||||
fs.Var(&g.catalogs, "catalog", "extra catalog file or directory (repeatable)")
|
||||
fs.DurationVar(&g.timeout, "timeout", 30*time.Minute, "per-installer timeout")
|
||||
fs.StringVar(&g.logFile, "log-file", "", "write the run log here (default: the deployr log directory)")
|
||||
fs.BoolVar(&g.noLog, "no-log", false, "do not write a log file")
|
||||
}
|
||||
|
||||
func (g *globals) registerAction(fs *flag.FlagSet) {
|
||||
g.register(fs)
|
||||
fs.BoolVar(&g.force, "force", false, "act even when the app is already in the desired state")
|
||||
fs.BoolVar(&g.strict, "strict", false, "fail instead of falling through to the next source")
|
||||
fs.BoolVar(&g.requireChecksum, "require-checksum", false, "refuse vendor downloads that have no sha256 in the catalog")
|
||||
fs.BoolVar(&g.keepDownloads, "keep-downloads", false, "keep downloaded installers in the cache directory")
|
||||
fs.BoolVar(&g.allowUnelevated, "allow-unelevated", false, "continue without administrator/root rights")
|
||||
fs.StringVar(&g.only, "provider", "", "restrict every app to a single provider (e.g. winget, direct)")
|
||||
}
|
||||
|
||||
func (g *globals) level() logx.Level {
|
||||
switch {
|
||||
case g.verbose:
|
||||
return logx.LevelDebug
|
||||
case g.quiet:
|
||||
return logx.LevelError
|
||||
default:
|
||||
return logx.LevelInfo
|
||||
}
|
||||
}
|
||||
|
||||
// newLogger sends progress to stdout normally, and to stderr in --json mode so
|
||||
// stdout carries nothing but the JSON document.
|
||||
func (g *globals) newLogger() *logx.Logger {
|
||||
var console io.Writer = os.Stdout
|
||||
if g.json {
|
||||
console = os.Stderr
|
||||
}
|
||||
log := logx.New(console, g.level())
|
||||
|
||||
if !g.noLog {
|
||||
path := g.logFile
|
||||
if path == "" {
|
||||
path = filepath.Join(platform.LogDir(), "deployr.log")
|
||||
}
|
||||
if err := log.AttachFile(path); err != nil {
|
||||
log.Debugf("log file unavailable (%v); continuing without one", err)
|
||||
}
|
||||
}
|
||||
return log
|
||||
}
|
||||
|
||||
// Main runs the CLI and returns the process exit code.
|
||||
func Main(args []string) int {
|
||||
if len(args) == 0 {
|
||||
usage(os.Stdout)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer stop()
|
||||
|
||||
cmd := args[0]
|
||||
rest := args[1:]
|
||||
|
||||
switch cmd {
|
||||
case "install", "i":
|
||||
return actionCommand(ctx, "install", rest)
|
||||
case "update", "upgrade":
|
||||
return actionCommand(ctx, "update", rest)
|
||||
case "uninstall", "remove":
|
||||
return actionCommand(ctx, "uninstall", rest)
|
||||
case "list", "ls":
|
||||
return listCommand(ctx, rest)
|
||||
case "status":
|
||||
return statusCommand(ctx, rest)
|
||||
case "info", "show":
|
||||
return infoCommand(rest)
|
||||
case "profiles":
|
||||
return profilesCommand(rest)
|
||||
case "doctor":
|
||||
return doctorCommand(ctx, rest)
|
||||
case "catalog":
|
||||
return catalogCommand(rest)
|
||||
case "version", "--version", "-v":
|
||||
fmt.Println(version.String())
|
||||
return ExitOK
|
||||
case "help", "--help", "-h":
|
||||
usage(os.Stdout)
|
||||
return ExitOK
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "deployr: unknown command %q\n\n", cmd)
|
||||
usage(os.Stderr)
|
||||
return ExitUsage
|
||||
}
|
||||
}
|
||||
|
||||
// parseFlags parses args and returns the non-flag operands. The standard flag
|
||||
// package stops at the first operand, which would make `deployr install chrome
|
||||
// --json` silently treat --json as an app name. Repeatedly parsing what is left
|
||||
// after each operand gives the flags-anywhere behaviour every other CLI has.
|
||||
func parseFlags(fs *flag.FlagSet, args []string) ([]string, error) {
|
||||
var operands []string
|
||||
for {
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rest := fs.Args()
|
||||
if len(rest) == 0 {
|
||||
return operands, nil
|
||||
}
|
||||
operands = append(operands, rest[0])
|
||||
args = rest[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// loadCatalog reads the embedded catalog plus overlays, reporting a usage error
|
||||
// rather than a stack trace when an overlay is malformed.
|
||||
func loadCatalog(paths []string) (*catalog.Catalog, error) {
|
||||
cat, err := catalog.Load(paths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("catalog: %w", err)
|
||||
}
|
||||
return cat, nil
|
||||
}
|
||||
|
||||
// resolveApps maps command-line names onto catalog entries, printing a
|
||||
// "did you mean" hint for anything unrecognised.
|
||||
func resolveApps(cat *catalog.Catalog, names []string) ([]*catalog.App, error) {
|
||||
apps, unknown := cat.Resolve(names)
|
||||
if len(unknown) > 0 {
|
||||
var msg strings.Builder
|
||||
fmt.Fprintf(&msg, "unknown app or profile: %s", strings.Join(unknown, ", "))
|
||||
if suggestions := cat.Suggest(unknown[0]); len(suggestions) > 0 {
|
||||
fmt.Fprintf(&msg, "\n did you mean: %s", strings.Join(suggestions, ", "))
|
||||
}
|
||||
msg.WriteString("\n run `deployr list` to see the catalog")
|
||||
return nil, errors.New(msg.String())
|
||||
}
|
||||
if len(apps) == 0 {
|
||||
return nil, errors.New("no apps given")
|
||||
}
|
||||
return apps, nil
|
||||
}
|
||||
|
||||
func usage(w io.Writer) {
|
||||
fmt.Fprint(w, `deployr - cross-platform application deployment for Windows, macOS and Linux
|
||||
|
||||
USAGE
|
||||
deployr <command> [flags] [apps...]
|
||||
|
||||
COMMANDS
|
||||
install <app|@profile>... install applications
|
||||
update <app|@profile>... update installed applications (--all for everything present)
|
||||
uninstall <app|@profile>... remove applications
|
||||
list show the catalog
|
||||
status [app...] report what is installed
|
||||
info <app> show one app's install recipe
|
||||
profiles list the bundles defined in the catalog
|
||||
doctor report platform, privileges and available providers
|
||||
catalog validate [path] check a catalog overlay for errors
|
||||
version print the build version
|
||||
|
||||
COMMON FLAGS
|
||||
--json machine-readable output on stdout
|
||||
--dry-run plan without changing the machine
|
||||
--verbose log every command executed
|
||||
--catalog PATH extra catalog file or directory (repeatable)
|
||||
--timeout 30m per-installer timeout
|
||||
|
||||
EXAMPLES
|
||||
deployr install chrome 7zip vlc
|
||||
deployr install @standard --json
|
||||
deployr update --all
|
||||
deployr status --json
|
||||
deployr list --category browser
|
||||
|
||||
EXIT CODES
|
||||
0 success 1 one or more apps failed 2 usage error 3 insufficient privileges
|
||||
`)
|
||||
}
|
||||
@@ -0,0 +1,594 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/catalog"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/detect"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/engine"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/logx"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/platform"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/provider"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/run"
|
||||
"git.starwarslab.us/ATOM_IT/Deployr/internal/version"
|
||||
)
|
||||
|
||||
// ------------------------------------------------- install/update/remove ---
|
||||
|
||||
func actionCommand(ctx context.Context, action string, args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr "+action, flag.ContinueOnError)
|
||||
g.registerAction(fs)
|
||||
all := fs.Bool("all", false, "apply to every catalog app that is installed on this machine")
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
if !platform.Supported() {
|
||||
fmt.Fprintf(os.Stderr, "deployr: %s is not a supported platform\n", runtime.GOOS)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
var apps []*catalog.App
|
||||
switch {
|
||||
case *all && len(names) == 0:
|
||||
apps = installedApps(cat)
|
||||
if len(apps) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: nothing from the catalog is installed on this machine")
|
||||
return ExitOK
|
||||
}
|
||||
case *all:
|
||||
fmt.Fprintln(os.Stderr, "deployr: --all takes no app arguments")
|
||||
return ExitUsage
|
||||
default:
|
||||
apps, err = resolveApps(cat, names)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
}
|
||||
|
||||
log := g.newLogger()
|
||||
defer log.Close()
|
||||
|
||||
if code := checkPrivileges(&g, log); code != ExitOK {
|
||||
return code
|
||||
}
|
||||
|
||||
log.Infof("%s: %s on %s (%s/%s)%s", version.String(), action, platform.Hostname(),
|
||||
runtime.GOOS, runtime.GOARCH, dryRunNote(g.dryRun))
|
||||
|
||||
summary := engine.Run(ctx, apps, engine.Config{
|
||||
Action: engine.Action(action),
|
||||
DryRun: g.dryRun,
|
||||
Force: g.force,
|
||||
Strict: g.strict,
|
||||
Timeout: g.timeout,
|
||||
CacheDir: platform.CacheDir(),
|
||||
RequireChecksum: g.requireChecksum,
|
||||
KeepDownloads: g.keepDownloads,
|
||||
OnlyProvider: g.only,
|
||||
Log: log,
|
||||
})
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(summary); err != nil {
|
||||
log.Errorf("%v", err)
|
||||
return ExitFailure
|
||||
}
|
||||
} else {
|
||||
log.Infof("%s", summaryLine(summary))
|
||||
if summary.RebootRequired {
|
||||
log.Infof("a reboot is required to finish")
|
||||
}
|
||||
}
|
||||
return summary.ExitCode()
|
||||
}
|
||||
|
||||
func dryRunNote(dry bool) string {
|
||||
if dry {
|
||||
return " [dry run]"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func summaryLine(s engine.Summary) string {
|
||||
return fmt.Sprintf("%d changed, %d skipped, %d failed, %d unsupported in %s",
|
||||
s.Changed, s.Skipped, s.Failed, s.Unsupported,
|
||||
(time.Duration(s.DurationMS) * time.Millisecond).Round(time.Second))
|
||||
}
|
||||
|
||||
// installedApps returns the catalog apps present on this machine, which is what
|
||||
// `--all` means for an update run.
|
||||
func installedApps(cat *catalog.Catalog) []*catalog.App {
|
||||
var out []*catalog.App
|
||||
for _, app := range cat.ForPlatform(runtime.GOOS) {
|
||||
target := app.TargetFor(runtime.GOOS)
|
||||
if ok, _ := detect.Installed(target.Detect); ok {
|
||||
out = append(out, app)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// checkPrivileges refuses to start a run that cannot possibly succeed. Windows
|
||||
// needs a real elevated token; Unix accepts either root or passwordless sudo.
|
||||
func checkPrivileges(g *globals, log *logx.Logger) int {
|
||||
if platform.IsElevated() {
|
||||
return ExitOK
|
||||
}
|
||||
if runtime.GOOS != "windows" && run.CanEscalate() {
|
||||
log.Debugf("not root; escalating individual commands with sudo -n")
|
||||
return ExitOK
|
||||
}
|
||||
if g.allowUnelevated || g.dryRun {
|
||||
log.Warnf("running without administrator/root rights; most installs will fail")
|
||||
return ExitOK
|
||||
}
|
||||
log.Errorf("administrator/root rights are required: %s", platform.PrivilegeHint())
|
||||
log.Errorf("pass --allow-unelevated to try anyway")
|
||||
return ExitPrivilege
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ list ---
|
||||
|
||||
type listRow struct {
|
||||
App string `json:"app"`
|
||||
Name string `json:"name"`
|
||||
Platforms []string `json:"platforms"`
|
||||
Categories []string `json:"categories,omitempty"`
|
||||
Installed *bool `json:"installed,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
||||
|
||||
func listCommand(_ context.Context, args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr list", flag.ContinueOnError)
|
||||
g.register(fs)
|
||||
category := fs.String("category", "", "only show apps in this category")
|
||||
everywhere := fs.Bool("all-platforms", false, "include apps with no recipe for this platform")
|
||||
onlyInstalled := fs.Bool("installed", false, "only show apps present on this machine")
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
if len(names) > 0 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: list takes no arguments; did you mean `deployr status`?")
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
apps := cat.Apps
|
||||
if !*everywhere {
|
||||
apps = cat.ForPlatform(runtime.GOOS)
|
||||
}
|
||||
|
||||
rows := make([]listRow, 0, len(apps))
|
||||
for _, app := range apps {
|
||||
if *category != "" && !app.HasCategory(*category) {
|
||||
continue
|
||||
}
|
||||
row := listRow{App: app.ID, Name: app.Name, Platforms: app.Platforms(), Categories: app.Categories}
|
||||
if target := app.TargetFor(runtime.GOOS); target != nil {
|
||||
present, ver := detect.Installed(target.Detect)
|
||||
row.Installed = &present
|
||||
row.Version = ver
|
||||
}
|
||||
if *onlyInstalled && (row.Installed == nil || !*row.Installed) {
|
||||
continue
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(rows); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitFailure
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(w, "APP\tNAME\tPLATFORMS\tCATEGORIES\tINSTALLED")
|
||||
for _, r := range rows {
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", r.App, r.Name,
|
||||
strings.Join(shortPlatforms(r.Platforms), ","),
|
||||
strings.Join(r.Categories, ","), installedCell(r))
|
||||
}
|
||||
w.Flush()
|
||||
fmt.Printf("\n%d apps\n", len(rows))
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
func installedCell(r listRow) string {
|
||||
switch {
|
||||
case r.Installed == nil:
|
||||
return "-"
|
||||
case !*r.Installed:
|
||||
return "no"
|
||||
case r.Version != "":
|
||||
return "yes " + r.Version
|
||||
default:
|
||||
return "yes"
|
||||
}
|
||||
}
|
||||
|
||||
func shortPlatforms(p []string) []string {
|
||||
out := make([]string, 0, len(p))
|
||||
for _, os := range p {
|
||||
switch os {
|
||||
case "windows":
|
||||
out = append(out, "win")
|
||||
case "darwin":
|
||||
out = append(out, "mac")
|
||||
default:
|
||||
out = append(out, os)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- status ---
|
||||
|
||||
type statusRow struct {
|
||||
App string `json:"app"`
|
||||
Name string `json:"name"`
|
||||
Installed bool `json:"installed"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Supported bool `json:"supported"`
|
||||
}
|
||||
|
||||
func statusCommand(_ context.Context, args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr status", flag.ContinueOnError)
|
||||
g.register(fs)
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
apps := cat.ForPlatform(runtime.GOOS)
|
||||
if len(names) > 0 {
|
||||
apps, err = resolveApps(cat, names)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
}
|
||||
|
||||
rows := make([]statusRow, 0, len(apps))
|
||||
for _, app := range apps {
|
||||
row := statusRow{App: app.ID, Name: app.Name}
|
||||
if target := app.TargetFor(runtime.GOOS); target != nil {
|
||||
row.Supported = true
|
||||
row.Installed, row.Version = detect.Installed(target.Detect)
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(rows); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitFailure
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(w, "APP\tNAME\tSTATE\tVERSION")
|
||||
installed := 0
|
||||
for _, r := range rows {
|
||||
state := "not installed"
|
||||
switch {
|
||||
case !r.Supported:
|
||||
state = "unsupported"
|
||||
case r.Installed:
|
||||
state = "installed"
|
||||
installed++
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", r.App, r.Name, state, r.Version)
|
||||
}
|
||||
w.Flush()
|
||||
fmt.Printf("\n%d of %d installed\n", installed, len(rows))
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ info ---
|
||||
|
||||
func infoCommand(args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr info", flag.ContinueOnError)
|
||||
g.register(fs)
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
if len(names) != 1 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: info takes exactly one app")
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
app, ok := cat.Get(names[0])
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, "deployr: unknown app %q\n", names[0])
|
||||
if s := cat.Suggest(names[0]); len(s) > 0 {
|
||||
fmt.Fprintf(os.Stderr, " did you mean: %s\n", strings.Join(s, ", "))
|
||||
}
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(app); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitFailure
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
fmt.Printf("%s (%s)\n", app.Name, app.ID)
|
||||
if app.Description != "" {
|
||||
fmt.Printf(" %s\n", app.Description)
|
||||
}
|
||||
if app.Homepage != "" {
|
||||
fmt.Printf(" homepage: %s\n", app.Homepage)
|
||||
}
|
||||
if len(app.Categories) > 0 {
|
||||
fmt.Printf(" categories: %s\n", strings.Join(app.Categories, ", "))
|
||||
}
|
||||
if len(app.Aliases) > 0 {
|
||||
fmt.Printf(" aliases: %s\n", strings.Join(app.Aliases, ", "))
|
||||
}
|
||||
for _, goos := range app.Platforms() {
|
||||
target := app.TargetFor(goos)
|
||||
fmt.Printf("\n %s\n", goos)
|
||||
for i, src := range target.Sources {
|
||||
id := src.ID
|
||||
if src.Provider == "direct" {
|
||||
id = src.URLFor(runtime.GOARCH)
|
||||
if id == "" {
|
||||
id = src.URL
|
||||
}
|
||||
}
|
||||
fmt.Printf(" %d. %-8s %s\n", i+1, src.Provider, id)
|
||||
}
|
||||
if !target.Detect.Empty() {
|
||||
fmt.Printf(" detect: %s\n", describeDetect(target.Detect))
|
||||
}
|
||||
if target.Notes != "" {
|
||||
fmt.Printf(" note: %s\n", target.Notes)
|
||||
}
|
||||
if goos == runtime.GOOS {
|
||||
present, ver := detect.Installed(target.Detect)
|
||||
state := "not installed"
|
||||
if present {
|
||||
state = "installed"
|
||||
if ver != "" {
|
||||
state += " " + ver
|
||||
}
|
||||
}
|
||||
fmt.Printf(" state: %s\n", state)
|
||||
}
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
func describeDetect(d catalog.Detect) string {
|
||||
var parts []string
|
||||
if len(d.Registry) > 0 {
|
||||
parts = append(parts, "registry "+strings.Join(d.Registry, " | "))
|
||||
}
|
||||
if len(d.Paths) > 0 {
|
||||
parts = append(parts, "paths "+strings.Join(d.Paths, " | "))
|
||||
}
|
||||
if len(d.Commands) > 0 {
|
||||
parts = append(parts, "commands "+strings.Join(d.Commands, " | "))
|
||||
}
|
||||
return strings.Join(parts, "; ")
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------- profiles ---
|
||||
|
||||
func profilesCommand(args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr profiles", flag.ContinueOnError)
|
||||
g.register(fs)
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
if len(names) > 0 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: profiles takes no arguments")
|
||||
return ExitUsage
|
||||
}
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(cat.Profiles); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitFailure
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
profileNames := cat.ProfileNames()
|
||||
if len(profileNames) == 0 {
|
||||
fmt.Println("no profiles defined")
|
||||
return ExitOK
|
||||
}
|
||||
for _, name := range profileNames {
|
||||
members := append([]string(nil), cat.Profiles[name]...)
|
||||
sort.Strings(members)
|
||||
fmt.Printf("@%s (%d apps)\n %s\n\n", name, len(members), strings.Join(members, " "))
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- doctor ---
|
||||
|
||||
type doctorReport struct {
|
||||
Version string `json:"version"`
|
||||
Host string `json:"host"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Elevated bool `json:"elevated"`
|
||||
CanSudo bool `json:"can_escalate"`
|
||||
Dirs map[string]string `json:"dirs"`
|
||||
CatalogApp int `json:"catalog_apps"`
|
||||
ForThisOS int `json:"catalog_apps_this_os"`
|
||||
Profiles int `json:"profiles"`
|
||||
Providers map[string]bool `json:"providers"`
|
||||
}
|
||||
|
||||
func doctorCommand(ctx context.Context, args []string) int {
|
||||
var g globals
|
||||
fs := flag.NewFlagSet("deployr doctor", flag.ContinueOnError)
|
||||
g.register(fs)
|
||||
names, err := parseFlags(fs, args)
|
||||
if err != nil {
|
||||
return ExitUsage
|
||||
}
|
||||
if len(names) > 0 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: doctor takes no arguments")
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
cat, err := loadCatalog(g.catalogs)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
|
||||
rep := doctorReport{
|
||||
Version: version.Version,
|
||||
Host: platform.Hostname(),
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
Elevated: platform.IsElevated(),
|
||||
CanSudo: run.CanEscalate(),
|
||||
Dirs: map[string]string{
|
||||
"data": platform.DataDir(),
|
||||
"logs": platform.LogDir(),
|
||||
"cache": platform.CacheDir(),
|
||||
},
|
||||
CatalogApp: len(cat.Apps),
|
||||
ForThisOS: len(cat.ForPlatform(runtime.GOOS)),
|
||||
Profiles: len(cat.Profiles),
|
||||
Providers: map[string]bool{},
|
||||
}
|
||||
|
||||
opts := provider.Opts{Timeout: 30 * time.Second}
|
||||
providers := provider.ForOS(runtime.GOOS)
|
||||
for _, p := range providers {
|
||||
rep.Providers[p.Name()] = p.Available(ctx, opts)
|
||||
}
|
||||
|
||||
if g.json {
|
||||
if err := writeJSON(rep); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitFailure
|
||||
}
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
fmt.Println(version.String())
|
||||
fmt.Printf("\nhost: %s\n", rep.Host)
|
||||
fmt.Printf("platform: %s/%s\n", rep.OS, rep.Arch)
|
||||
fmt.Printf("elevated: %s\n", yesNo(rep.Elevated))
|
||||
if runtime.GOOS != "windows" {
|
||||
fmt.Printf("sudo -n: %s\n", yesNo(rep.CanSudo))
|
||||
}
|
||||
fmt.Printf("\ncatalog: %d apps (%d for %s), %d profiles\n",
|
||||
rep.CatalogApp, rep.ForThisOS, rep.OS, rep.Profiles)
|
||||
fmt.Println("overlays:")
|
||||
for _, dir := range platform.CatalogDirs() {
|
||||
fmt.Printf(" %s\n", dir)
|
||||
}
|
||||
fmt.Println("directories:")
|
||||
for _, key := range []string{"data", "logs", "cache"} {
|
||||
fmt.Printf(" %-6s %s\n", key, rep.Dirs[key])
|
||||
}
|
||||
fmt.Println("providers:")
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
for _, p := range providers {
|
||||
state := "missing"
|
||||
if rep.Providers[p.Name()] {
|
||||
state = "available"
|
||||
}
|
||||
fmt.Fprintf(w, " %s\t%s\t%s\n", p.Name(), state, p.Label())
|
||||
}
|
||||
w.Flush()
|
||||
return ExitOK
|
||||
}
|
||||
|
||||
func yesNo(b bool) string {
|
||||
if b {
|
||||
return "yes"
|
||||
}
|
||||
return "no"
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------- catalog ---
|
||||
|
||||
func catalogCommand(args []string) int {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "deployr: catalog needs a subcommand (validate)")
|
||||
return ExitUsage
|
||||
}
|
||||
switch args[0] {
|
||||
case "validate":
|
||||
cat, err := loadCatalog(args[1:])
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "deployr:", err)
|
||||
return ExitUsage
|
||||
}
|
||||
fmt.Printf("catalog ok: %d apps, %d profiles\n", len(cat.Apps), len(cat.Profiles))
|
||||
for _, goos := range []string{"windows", "darwin", "linux"} {
|
||||
fmt.Printf(" %-8s %d apps\n", goos, len(cat.ForPlatform(goos)))
|
||||
}
|
||||
return ExitOK
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "deployr: unknown catalog subcommand %q\n", args[0])
|
||||
return ExitUsage
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(v any) error {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(v)
|
||||
}
|
||||
Reference in new issue
Block a user