Top Level Namespace

Defined Under Namespace

Modules: Qcs Classes: QCS, RepoInfo

Instance Method Summary collapse

Instance Method Details

#main(args) ⇒ Object



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

def main(args)
  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{"qcs COMMAND".green} [#{"OPTIONS".green}]"
    opt.separator "    "
    opt.separator "Commands"
    opt.separator "    "
    opt.separator "     #{"+ pull".green} Fetch from and integrate with another repository or a local branch"
    opt.separator "     #{"+ checkout".green} Switch branches or restore working tree files"
    opt.separator "    "
    opt.separator "Options"
    opt.separator "    "
    opt.on_tail("-h", "--help", "help") do
      puts opt_parser
    end
    opt.on_tail("-v", "--version", "Show the version of the tool") do
      puts Qcs::VERSION
    end
  end

  if args.empty?
    puts opt_parser
    exit
  end

  validOpts = opt_parser.candidate(args[0]) # 校验合法的options
  unless validOpts.empty?   # 处理有options的情况 直接处理
    opt_parser.parse!(args)
    exit
  end

  config = "repos.json"
  if File.exist?(config) # 文件存在 先校验action
    qcs = QCS.new(config)
    action = args[0]
    case action
    when "install"
      qcs.install
    when "pull"
      qcs.pull
    when "checkout"
      if args.count < 2
        hint = "Invalid branch or commitId"
        STDERR.puts(hint.red)
        exit
      end
      branch = args[1]
      qcs.checkout(branch)
    else # 不合法的action
      puts opt_parser
    end
  else
    # 文件不存在 options还不合法 可能是输入了错误的命令或者options
    puts "make sure current directory has a file named #{config}"
  end
end