Module: Wisco::Commands::Init

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

Constant Summary collapse

HOSTNAMES =
[
  { region: 'US Data Center', hostname: 'www.workato.com'    },
  { region: 'EU Data Center', hostname: 'app.eu.workato.com' },
  { region: 'JP Data Center', hostname: 'app.jp.workato.com' },
  { region: 'SG Data Center', hostname: 'app.sg.workato.com' },
  { region: 'AU Data Center', hostname: 'app.au.workato.com' },
  { region: 'IL Data Center', hostname: 'app.il.workato.com' },
].freeze

Class Method Summary collapse

Class Method Details

.deploy_github_workflow(target_dir, connector_file, config) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wisco/commands/init.rb', line 105

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

  hostname         = config.dig('workato_developer_api', 'hostname')
  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

.prompt_hostname(config) ⇒ Object



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

def prompt_hostname(config)
  api_cfg = config['workato_developer_api'] ||= {}
  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

  puts 'Select your Workato instance:'
  HOSTNAMES.each_with_index do |entry, i|
    puts "  #{i + 1}. #{entry[:hostname]}  (#{entry[:region]})"
  end

  loop do
    print "Enter number (1-#{HOSTNAMES.size}): "
    input = $stdin.gets.strip
    index = input.to_i
    if index >= 1 && index <= HOSTNAMES.size
      api_cfg['hostname'] = HOSTNAMES[index - 1][:hostname]
      puts "Hostname set to: #{api_cfg['hostname']}"
      return
    end
    warn "Invalid selection. Please enter a number between 1 and #{HOSTNAMES.size}."
  end
end

.run(target_dir) ⇒ Object



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

def run(target_dir)
  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)

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

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

.update_gitignore(target_dir) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wisco/commands/init.rb', line 86

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