aboutsummaryrefslogtreecommitdiff
path: root/src/guff/config.cr
blob: 205c65da6a2fde563cc9b3f36c589a4f7ec3303d (plain)
1
2
3
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
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require "option_parser"

class Guff::Config
  property :mode, :env, :host, :port, :data_dir, :system_dir, 
           :theme_action, :theme_src_dir, :theme_dst_path

  DEFAULTS = {
    mode:         "help",
    env:          "production",
    host:         "127.0.0.1",
    port:         "8989",
    data_dir:     "./data",
    system_dir:   "/usr/local/share/guff",
    theme_action: "list"
  }

  @theme_action   : String
  @theme_src_dir  : String?
  @theme_dst_path : String?

  def initialize
    @mode         = DEFAULTS[:mode] as String
    @env          = (ENV["GUFF_ENVIRONMENT"]? || DEFAULTS[:env]) as String
    @host         = (ENV["GUFF_HOST"]? || DEFAULTS[:host]) as String
    @port         = (ENV["GUFF_PORT"]? || DEFAULTS[:port]) as String
    @data_dir     = (ENV["GUFF_DATA_DIR"]? || DEFAULTS[:data_dir]) as String
    @system_dir   = (ENV["GUFF_SYSTEM_DIR"]? || DEFAULTS[:system_dir]) as String
    @theme_action = DEFAULTS[:theme_action]
  end

  VALID_MODES = %w{init run help theme}

  VALID_THEME_ACTIONS = %w{list pack}

  def mode=(mode : String)
    raise "unknown mode: \"#{mode}\"" unless VALID_MODES.includes?(mode)
    @mode = mode
  end

  VALID_ENVS = %w{development production}

  def env=(env : String)
    raise "unknown environment: \"#{env}\"" unless VALID_ENVS.includes?(env)
    @env = env
  end

  def port=(port : String)
    val = port.to_i
    raise "invalid port: #{port}" unless val > 0 && val < 65535
    @port = port
  end

  def system_dir=(dir : String)
    raise "missing system dir: \"#{dir}\"" unless Dir.exists?(dir)
    @system_dir = dir
  end

  def db_path
    File.join(@data_dir, "guff.db")
  end

  def self.parse(
    app   : String,
    args  : Array(String)
  ) : Config
    r = Config.new

    raise "missing mode" unless args.size > 0

    # get mode
    r.mode = case mode = args.shift
    when "-h", "--help"
      "help"
    else
      mode
    end

    # parse arguments
    p = OptionParser.parse(args) do |p|
      p.banner = "Usage: #{app} [mode] <args>"

      p.separator
      p.separator("Run Options:")

      p.on(
        "-H HOST", "--host HOST",
        "TCP host (defaults to \"#{DEFAULTS[:host]}\")"
      ) do |arg|
        r.host = arg
      end

      p.on(
        "-p PORT", "--port PORT",
        "TCP port (defaults to \"#{DEFAULTS[:port]}\")"
      ) do |arg|
        r.port = arg
      end

      p.separator
      p.separator("Directory Options:")

      p.on(
        "-D DIR", "--data-dir DIR",
        "Data directory (defaults to \"#{DEFAULTS[:data_dir]}\")"
      ) do |arg|
        r.data_dir = arg
      end

      p.on(
        "-S DIR", "--system-dir DIR",
        "Guff system directory (defaults to \"#{DEFAULTS[:system_dir]}\")"
      ) do |arg|
        r.system_dir = arg
      end

      p.separator
      p.separator("Development Options:")

      p.on(
        "-E ENV", "--environment ENV",
        "Environment (defaults to \"#{DEFAULTS[:env]})\""
      ) do |arg|
        r.env = arg
      end

      p.separator
      p.separator("Other Options:")

      p.on("-h", "--help", "Print usage.") do
        r.mode = "help"
      end

      p.separator
      p.separator("
Examples:
    # init new data directory
    guff init some-app

    # run using directory some-app
    guff run -D some-app

    # pack theme in directory /path/to/some-theme as ./some-theme.zip
    guff theme pack /path/to/some-theme

    # pack theme in directory /path/to/some-theme as /other/path/output.zip
    guff theme pack /path/to/some-theme /other/path/output.zip
    ")
    end

    case r.mode
    when "init"
      # shortcut for -D parameter
      r.data_dir = args.shift if args.size > 0
    when "theme"
      # get theme action
      r.theme_action = args.shift if args.size > 0
      unless VALID_THEME_ACTIONS.includes?(r.theme_action)
        raise "unknown theme action: \"#{r.theme_action}\""
      end

      case r.theme_action
      when "pack"
        raise "missing theme directory" unless args.size > 0
        r.theme_src_dir = args.shift

        if args.size > 0
          r.theme_dst_path = args.shift
        else
          r.theme_dst_path = File.basename(r.theme_src_dir.not_nil!) + ".zip"
        end
      end
    when "help"
      # print help
      puts p
    end

    # return config
    r
  end
end