Class: Deploio::AppResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/deploio/app_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nctl_client:) ⇒ AppResolver

Returns a new instance of AppResolver.



7
8
9
10
11
# File 'lib/deploio/app_resolver.rb', line 7

def initialize(nctl_client:)
  @nctl = nctl_client
  @git_remote_url = Utils.detect_git_remote
  @current_org = @nctl.current_org
end

Instance Attribute Details

#current_orgObject (readonly)

Returns the value of attribute current_org.



5
6
7
# File 'lib/deploio/app_resolver.rb', line 5

def current_org
  @current_org
end

#git_remote_urlObject (readonly)

Returns the value of attribute git_remote_url.



5
6
7
# File 'lib/deploio/app_resolver.rb', line 5

def git_remote_url
  @git_remote_url
end

#nctlObject (readonly)

Returns the value of attribute nctl.



5
6
7
# File 'lib/deploio/app_resolver.rb', line 5

def nctl
  @nctl
end

Instance Method Details

#available_apps_hashObject

Returns hash mapping app names -> app_name: Supports both full names (org-project-app) and short names (project-app)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/deploio/app_resolver.rb', line 45

def available_apps_hash
  @available_apps_hash ||= begin
    hash = {}
    current_org = @nctl.current_org
    @nctl.get_all_apps.each do |app|
       = app["metadata"] || {}
      project_name = ["namespace"] || ""
      app_name = ["name"]
      full_name = "#{project_name}-#{app_name}"
      hash[full_name] = {project_name: project_name, app_name: app_name}

      # Also index by short name (without org prefix) for convenience
      if current_org && project_name.start_with?("#{current_org}-")
        project = project_name.delete_prefix("#{current_org}-")
        short_name = "#{project}-#{app_name}"
        hash[short_name] ||= {project_name: project_name, app_name: app_name}
      end
    end
    hash
  end
rescue
  {}
end

#resolve(app_name: nil) ⇒ Object

Raises:



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
# File 'lib/deploio/app_resolver.rb', line 13

def resolve(app_name: nil)
  # 1. Explicit --app flag
  if app_name
    return AppRef.new(app_name, available_apps: available_apps_hash)
  end

  # 2. Match git remote against nctl apps
  if git_remote_url
    matches = find_apps_by_git_remote(git_remote_url)

    case matches.size
    when 0
      raise Deploio::AppNotFoundError,
        "No deploio apps found matching git remote: #{git_remote_url}\n" \
        "Use --app to specify the app explicitly.\n" \
        "Run 'deploio apps' to see available apps."
    when 1
      return build_app_ref_from_match(matches.first)
    else
      app_names = matches.map { |m| format_match(m) }
      raise Deploio::Error,
        "Multiple apps found for this repo. Use --app to specify which one:\n" \
        "#{app_names.map { |name| "  #{name}" }.join("\n")}"
    end
  end

  raise Deploio::Error,
    "No app specified. Use --app <project-app> or run from a git repo with a matching remote."
end

#short_name_for(namespace, app_name) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/deploio/app_resolver.rb', line 69

def short_name_for(namespace, app_name)
  org = current_org
  if org && namespace.start_with?("#{org}-")
    project = namespace.delete_prefix("#{org}-")
    "#{project}-#{app_name}"
  else
    "#{namespace}-#{app_name}"
  end
end