Module: MagisterCLI

Extended by:
MagisterCLI
Included in:
MagisterCLI
Defined in:
lib/magister_cli.rb

Instance Method Summary collapse

Instance Method Details

#mainObject



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
# File 'lib/magister_cli.rb', line 19

def main
  $magcli_runtime_variables["magister"] = Magister.new

  begin
    if ARGV.include?("-r") || ARGV.include?("--run")
        $magcli_runmode = "inline"
        args = ARGV
        while args.include?("-r") do
          i = args.find_index("-r")
          args.delete_at(i)
        end

        while args.include?("--run") do
          i = args.find_index("--run")
          args.delete_at(i)
        end
        # TODO: tokenize command

        # TODO: parse command

        # TODO: run command

        return
    end

    parser = Parser.new("")
    while true
      print "> "
      input = gets.chomp
      parser.string = input
      output = parser.parse

      # loop over each action that was outputted

      output.each do |action|
        props = action["parameters"]
        doneParams = Array.new
        # get any parametres that werent set and ask then set them

        action["lacking_params"].each do |param|
          print "#{param}: "
          if param == "password"
            ans = STDIN.noecho(&:gets).chomp
            print "\n"
          else
            ans = gets.chomp
          end
          props.append({"type" => "PROPERTY", "key" => param, "value" => ans})
          doneParams.append param
        end

        # update lacking_params value, done here as to not mess with the order while looping over them

        doneParams.each do |p|
          action["lacking_params"].delete(p)
        end
        action["parameters"] = props
        props = Array.new

        runtime = Runtime.new(action)
      end
    end
  rescue SignalException
    $magcli_runtime_variables = Array.new
    puts "[CTRL + C]\nExiting..."
    exit
  rescue UncaughtThrowError => e
    if e.message.include? "#<SyntaxError: "
      message = e.message.split("#<SyntaxError: ").last[0..-2]
      puts "Invalid syntax: #{message}"
    else
      puts "Something went wrong: #{e.message}"
    end
  rescue => e
    puts "Something went wrong: #{e.message}"
  end
end