Class: FPM::Cookery::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/fpm/cookery/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



13
14
15
16
# File 'lib/fpm/cookery/cli.rb', line 13

def initialize
  @colors = true
  @debug = false
end

Instance Method Details

#args(argv) ⇒ Object



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
# File 'lib/fpm/cookery/cli.rb', line 18

def args(argv)
  program = File.basename($0)
  options = OptionParser.new
  options.banner = \
    "Usage: #{program} [options] [path/to/recipe.rb] action [...]"
  options.separator "Actions:"
  options.separator "  package - builds the package"
  options.separator "  clean - cleans up"
  options.separator "Options:"

  options.on("-c", "--color",
             "Toggle color. (default #{@colors.inspect})") do |o|
    @colors = !@colors
  end

  options.on("-D", "--debug", "Enable debug output.") do |o|
    @debug = true
  end

  options.on("-t TARGET", "--target TARGET",
            "Set the desired fpm output target (deb, rpm, etc)") do |o|
    @target = o
  end

  options.on("-p PLATFORM", "--platform PLATFORM",
            "Set the target platform. (centos, ubuntu, debian)") do |o|
    @platform = o
  end

  # Parse flags and such, remainder is all non-option args.
  remainder = options.parse(argv)

  # Initialize logging.
  FPM::Cookery::Log.enable_debug(@debug)
  if @colors
    FPM::Cookery::Log.output(FPM::Cookery::Log::Output::ConsoleColor.new)
  else
    FPM::Cookery::Log.output(FPM::Cookery::Log::Output::Console.new)
  end

  # Default recipe to find is in current directory named 'recipe.rb'
  @filename = File.expand_path('recipe.rb')

  # See if something that looks like a recipe path is in arguments
  remainder.each do |arg|
    # If 'foo.rb' was given, and it exists, use it.
    if arg =~ /\.rb$/ and File.exists?(arg)
      remainder.delete(arg)
      @filename = arg
      break
    end

    # Allow giving the directory containing a recipe.rb
    if File.directory?(arg) and File.exists?(File.join(arg, "recipe.rb"))
      remainder.delete(arg)
      @filename = File.join(arg, "recipe.rb")
      break
    end
  end

  # Everything that's not the recipe filename is an action.
  @actions = remainder
  return self
end

#runObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fpm/cookery/cli.rb', line 110

def run
  validate

  FPM::Cookery::Recipe.send(:include, FPM::Cookery::BookHook)

  FPM::Cookery::Book.load_recipe(@filename) do |recipe|
    packager = FPM::Cookery::Packager.new(recipe)
    packager.target = FPM::Cookery::Facts.target.to_s

    @actions.each do |action|
      case action
      when "clean" ; packager.cleanup
      when "package" ; packager.dispense
      else
        # TODO(sissel): fail if this happens
        Log.error "Unknown action: #{action}"
      end
    end
  end
end

#validateObject



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
# File 'lib/fpm/cookery/cli.rb', line 83

def validate
  unless File.exists?(@filename)
    Log.error 'No recipe.rb found in the current directory, abort.'
    exit 1
  end

  # Default action is "package"
  if @actions.empty?
    @actions = ["package"]
  end

  # Override the detected platform.
  if @platform
    FPM::Cookery::Facts.platform = @platform
  end

  if @target
    FPM::Cookery::Facts.target = @target
  end

  if FPM::Cookery::Facts.target.nil?
    Log.error "No target given and we're unable to detect your platform"
    exit 1
  end

end