Class: Pantograph::Actions::PromptAction

Inherits:
Pantograph::Action show all
Defined in:
pantograph/lib/pantograph/actions/prompt.rb

Constant Summary

Constants inherited from Pantograph::Action

Pantograph::Action::AVAILABLE_CATEGORIES, Pantograph::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Pantograph::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



95
96
97
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 95

def self.authors
  ['KrauseFx', 'johnknapprs']
end

.available_optionsObject



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
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 58

def self.available_options
  [
    PantographCore::ConfigItem.new(
      key: :text,
      description: "The text that will be displayed to the user",
      default_value: "Please enter some text: "
    ),
    PantographCore::ConfigItem.new(
      key: :ci_input,
      description: "The default text that will be used when being executed on a CI service",
      default_value: ''
    ),
    PantographCore::ConfigItem.new(
      key: :boolean,
      description: "Is that a boolean question (yes/no)? This will add (y/n) at the end",
      default_value: false,
      is_string: false
    ),
    PantographCore::ConfigItem.new(
      key: :secure_text,
      description: "Is that a secure text (yes/no)?",
      default_value: false,
      is_string: false
    ),
    PantographCore::ConfigItem.new(
      key: :multi_line_end_keyword,
      description: "Enable multi-line inputs by providing an end text (e.g. 'END') which will stop the user input",
      optional: true,
      type: String
    )
  ]
end

.categoryObject



123
124
125
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 123

def self.category
  :misc
end

.descriptionObject



46
47
48
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 46

def self.description
  "Ask the user for a value or for confirmation"
end

.detailsObject



50
51
52
53
54
55
56
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 50

def self.details
  [
    "You can use `prompt` to ask the user for a value or to just let the user confirm the next step.",
    "When this is executed on a CI service, the passed `ci_input` value will be returned.",
    "This action also supports multi-line inputs using the `multi_line_end_keyword` option."
  ].join("\n")
end

.example_codeObject



103
104
105
106
107
108
109
110
111
112
113
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 103

def self.example_code
  [
    'changelog = prompt(text: "Changelog: ")',
    'changelog = prompt(
      text: "Changelog: ",
      multi_line_end_keyword: "END"
    )

    puts changelog'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



99
100
101
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 99

def self.is_supported?(platform)
  true
end

.outputObject



91
92
93
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 91

def self.output
  []
end

.return_typeObject



119
120
121
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 119

def self.return_type
  :string
end

.run(params) ⇒ Object



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
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 4

def self.run(params)
  if params[:boolean]
    return Helper.ci? ? params[:ci_input] : UI.confirm(params[:text])
  end

  UI.message(params[:text])

  return params[:ci_input] if Helper.ci?

  if params[:multi_line_end_keyword]
    # Multi line
    end_tag = params[:multi_line_end_keyword]
    UI.important("Submit inputs using \"#{params[:multi_line_end_keyword]}\"")
    user_input = ""
    loop do
      line = STDIN.gets # returns `nil` if called at end of file
      break unless line
      end_tag_index = line.index(end_tag)
      if end_tag_index.nil?
        user_input << line
      else
        user_input << line.slice(0, end_tag_index)
        user_input = user_input.strip
        break
      end
    end
  else
    # Standard one line input
    if params[:secure_text]
      user_input = STDIN.noecho(&:gets).chomp while (user_input || "").length == 0
    else
      user_input = STDIN.gets.chomp.strip while (user_input || "").length == 0
    end
  end

  return user_input
end

.sample_return_valueObject



115
116
117
# File 'pantograph/lib/pantograph/actions/prompt.rb', line 115

def self.sample_return_value
  "User Content\nWithNewline"
end