Module: Wisco::Commands::Init

Defined in:
lib/wisco/commands/init.rb

Class Method Summary collapse

Class Method Details

.deploy_github_workflow(target_dir, connector_file, config) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/wisco/commands/init.rb', line 158

def deploy_github_workflow(target_dir, connector_file, config)
  output_path = File.join(target_dir, '.github', 'workflows', 'deploy.yml')

  if File.exist?(output_path)
    print '.github/workflows/deploy.yml already exists. Overwrite? (y/n): '
    unless $stdin.gets.strip.downcase == 'y'
      puts 'Skipped: .github/workflows/deploy.yml'
      return
    end
  end

  api_cfg  = config['workato_developer_api'] || {}
  hostname = if api_cfg['profile'].to_s.strip != ''
               Wisco::Profiles.get(api_cfg['profile'])&.dig('hostname')
             else
               api_cfg['hostname']
             end
  workato_base_url = "https://#{hostname}"
  connector_name   = connector_file

  template_path = File.join(__dir__, '..', 'assets', '.github', 'workflows', 'deploy.yml.erb')
  rendered      = ERB.new(File.read(template_path)).result(binding)

  FileUtils.mkdir_p(File.dirname(output_path))
  File.write(output_path, rendered)
  puts 'Created .github/workflows/deploy.yml'
end

.ensure_gemfile(target_dir) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/wisco/commands/init.rb', line 144

def ensure_gemfile(target_dir)
  gemfile_path = File.join(target_dir, 'Gemfile')

  if File.exist?(gemfile_path)
    puts 'Gemfile already exists — no changes made.'
    return
  end

  asset_path = File.join(__dir__, '..', 'assets', 'Gemfile')
  FileUtils.cp(asset_path, gemfile_path)
  Wisco::TerminalOutput.emit_info('[INFO] Created Gemfile from template')
  Wisco::TerminalOutput.emit_info("[INFO] Run 'bundle install' to install dependencies.")
end

.prompt_hostname(config, profile: nil) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wisco/commands/init.rb', line 51

def prompt_hostname(config, profile: nil)
  api_cfg = config['workato_developer_api'] ||= {}

  # --profile flag: skip all prompts and attach the named profile
  if profile
    unless Wisco::Profiles.exists?(profile)
      Wisco::TerminalOutput.emit_error("Error: Profile \"#{profile}\" not found in #{Wisco::Profiles.profiles_path}.")
      Wisco::TerminalOutput.emit_error("       Run 'wisco profile list' to see available profiles.")
      exit 1
    end
    api_cfg.delete('hostname')
    api_cfg.delete('api_token')
    api_cfg['profile'] = profile
    puts "Using profile: #{profile}"
    return
  end

  # If profiles exist, offer them alongside "enter manually"
  existing_profiles = Wisco::Profiles.all
  unless existing_profiles.empty?
    current_profile = api_cfg['profile'].to_s.strip
    current_host    = api_cfg['hostname'].to_s.strip

    if !current_profile.empty?
      puts "Current Workato profile: #{current_profile}"
      print 'Keep this? (y/n): '
      return if $stdin.gets.strip.downcase == 'y'
    elsif !current_host.empty?
      puts "Current Workato hostname: #{current_host} (inline credentials)"
      print 'Keep this? (y/n): '
      return if $stdin.gets.strip.downcase == 'y'
    end

    puts 'Workato API credentials'
    name_width = existing_profiles.keys.map(&:length).max
    existing_profiles.each_with_index do |(prof_name, p), i|
      puts "  #{i + 1}. #{prof_name.ljust(name_width)}  (#{p['hostname']})"
    end
    manual_index = existing_profiles.size + 1
    puts "  #{manual_index}. Enter credentials manually"
    loop do
      print "Select an option (1-#{manual_index}): "
      input = $stdin.gets.strip
      index = input.to_i
      if index >= 1 && index < manual_index
        chosen = existing_profiles.keys[index - 1]
        api_cfg.delete('hostname')
        api_cfg.delete('api_token')
        api_cfg['profile'] = chosen
        puts "Using profile: #{chosen}"
        return
      elsif index == manual_index
        break # fall through to manual hostname prompt below
      else
        warn "Invalid selection. Please enter a number between 1 and #{manual_index}."
      end
    end
  else
    # No profiles — check for existing inline hostname
    current = api_cfg['hostname']
    if current && !current.strip.empty?
      puts "Current Workato hostname: #{current}"
      print 'Keep this? (y/n): '
      return if $stdin.gets.strip.downcase == 'y'
    end
  end

  # Manual hostname selection
  hostname = Wisco::Profiles.prompt_hostname_selection
  api_cfg.delete('profile')
  api_cfg['hostname'] = hostname
  puts "Hostname set to: #{hostname}"
end

.run(target_dir, profile: nil) ⇒ Object



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
# File 'lib/wisco/commands/init.rb', line 12

def run(target_dir, profile: nil)
  target_dir = File.expand_path(target_dir)

  unless Dir.exist?(target_dir)
    warn "Error: Directory not found: #{target_dir}"
    exit 1
  end

  puts "Searching for connector in #{target_dir}..."

  connector_file = Wisco::Connector.detect_connector(target_dir)

  if connector_file.nil?
    warn "Error: No valid Workato connector file found in #{target_dir}"
    exit 1
  end

  puts "Found connector: #{connector_file}"

  wisco_dir   = File.join(target_dir, Wisco::WISCO_DIR)
  FileUtils.mkdir_p(wisco_dir)

  config_path = Wisco.config_path(target_dir)
  config      = Wisco::Config.load_config(config_path)

  config['connector'] ||= {}
  config['connector']['path'] = target_dir
  config['connector']['file'] = connector_file

  prompt_hostname(config, profile: profile)

  Wisco::Config.save_config(config_path, config)
  puts "Config written to #{config_path}"

  update_gitignore(target_dir)
  ensure_gemfile(target_dir)
  deploy_github_workflow(target_dir, connector_file, config)
end

.update_gitignore(target_dir) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/wisco/commands/init.rb', line 125

def update_gitignore(target_dir)
  gitignore_path = File.join(target_dir, '.gitignore')
  entry          = "#{Wisco::WISCO_DIR}/"

  if File.exist?(gitignore_path)
    content = File.read(gitignore_path)
    if content.include?(entry)
      puts ".gitignore already contains '#{entry}' — no changes made."
    else
      File.open(gitignore_path, 'a') { |f| f.puts entry }
      puts "Added '#{entry}' to .gitignore"
    end
  else
    asset_path = File.join(__dir__, '..', 'assets', '.gitignore')
    FileUtils.cp(asset_path, gitignore_path)
    puts "Created .gitignore from template"
  end
end