|
root / src / commands / owner.reef
owner.reef Reef 98 lines 2.9 KB
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/******************************************************************************
               __               ____                __
              / /   ___  ____ _/ __/_____________ _/ /__
             / /   / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
            / /___/  __/ /_/ / __(__  ) /__/ /_/ / /  __/
           /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/

   (C)opyright 2025, Leafscale, LLC -  https://www.leafscale.com

   Project: Zygaena
  Filename: owner.reef
   Authors: Chris Tusa <chris.tusa@leafscale.com>
   License: <see LICENSE file included with this source code>
Description: Owner command - find which package owns a file

******************************************************************************/

module commands.owner

import sys.args
import core.str
import core.config
import core.database
import core.pkgdb
import types
import util.color
import exitcodes as ec

export
    fn execute(opts: types.GlobalOptions): int
end export

fn execute(opts: types.GlobalOptions): int
    let argc = args.count()

    // File path should be at cmd_index + 1
    if argc <= opts.cmd_index + 1
        print_usage()
        return ec.EXIT_USAGE()
    end if

    // Load configuration and apply command-line overrides
    let base_cfg = config.load()
    let cfg = config.apply_overrides(base_cfg, opts.root, opts.prefix, opts.no_chroot)
    let root_path = config.get_root(cfg)

    // Show root if set (verbose mode)
    if opts.verbose and str.length(root_path) > 0
        color.print_info("Using root: " + root_path)
    end if

    let file_path = args.get(opts.cmd_index + 1)

    // Find owner (use rooted version if root is set)
    let owner = find_owner(file_path, root_path)

    if str.length(owner) == 0
        color.print_error("No package owns: " + file_path)
        return ec.EXIT_PKG_NOT_FOUND()
    end if

    // Get package info (use rooted version if root is set)
    let pkg = get_installed_pkg(owner, root_path)

    color.print_action("File: " + file_path)
    println("")
    println("Owned by: " + owner + " " + pkg.info.version)
    return ec.EXIT_SUCCESS()
end execute

// Helper functions for rooted operations
// Uses pkgdb for fast indexed lookup with fallback to database scan
fn find_owner(file_path: string, root_path: string): string
    return pkgdb.find_file_owner_rooted(file_path, root_path)
end find_owner

fn get_installed_pkg(name: string, root_path: string): types.InstalledPackage
    if str.length(root_path) > 0
        return database.get_installed_rooted(name, root_path)
    end if
    return database.get_installed(name)
end get_installed_pkg

proc print_usage()
    println("Usage: coral owner <file>")
    println("")
    println("Find which package owns a given file.")
    println("")
    println("Arguments:")
    println("  <file>    Path to the file to look up")
    println("")
    println("Examples:")
    println("  coral owner /usr/bin/vim")
    println("  coral owner /usr/bin/hello")
end print_usage

end module