bin/jekyll-auth in jekyll-auth-0.6.1 vs bin/jekyll-auth in jekyll-auth-1.0.0

- old
+ new

@@ -1,145 +1,133 @@ #!/usr/bin/env ruby # Command-line interface for jekyll-auth -require 'rubygems' -require 'commander/import' -require 'rake' +require 'mercenary' require 'jekyll-auth' -require 'git' -require 'mkmf' +require 'open3' -def changed? - git = Git.init - git.diff('HEAD', 'config.ru').entries.length != 0 || git.diff('HEAD', 'Rakefile').entries.length != 0 -end +Mercenary.program("jekyll-auth") do |p| + p.version JekyllAuth::VERSION + p.description "A simple way to use Github OAuth to serve a protected jekyll site to your GitHub organization" + p.syntax 'jekyll-auth <subcommand> options' -program :version, JekyllAuth::VERSION -program :description, 'A simple way to use Github Oauth to serve a protected jekyll site to your GitHub organization' + p.command(:new) do |c| + c.syntax 'new' + c.description "Initialize an existing Jekyll site as a Jekyll Auth site" + c.action do |args, options| -command :new do |c| - c.syntax = 'jekyll-auth new' - c.description = "Initialize an existing Jekyll site as a Jekyll Auth site" - c.action do |args, options| - source = File.expand_path( "../", File.dirname(__FILE__) ) - destination = Dir.pwd - say "Initiating new Jekyll Auth site in #{destination}" + JekyllAuth::Commands.copy_templates - ["Rakefile", "config.ru", ".gitignore"].each do |file| - if File.exist? "#{destination}/#{file}" - say "* #{destination}/#{file} already exists... skipping." - else - say "* creating #{destination}/#{file}" - FileUtils.cp "#{source}/#{file}", "#{destination}/#{file}" + if JekyllAuth::Commands.changed? + puts "Looks like we've made some changes, you may want to do a git commit and git push sometime soon".yellow end - end - command(:setup).run if agree "Would you like to set up Heroku now? (Y/n)" - - if changed? - system "git status" - say "Looks like we've made some changes, you may want to do a git commit and git push sometime soon" + puts "Setup complete. Run `jekyll-auth` to view the authenticated site." end + end - say "Setup complete. Run jekyll-auth to view the authenticated site." + # Run the standard jekyll build command + # Called by Rake task, to allow the gem + # to add functionality here in the future + p.command(:build) do |c| + c.syntax 'build' + c.description "Build the Jekyll site" + c.action do |args, options| + require 'jekyll' + Jekyll::Commands::Build.process(options) + end end -end -command :setup do |c| - c.syntax = "jekyll-auth setup" - c.description = "Configure Heroku for use with your Jekyll Auth site" - c.action do |args, options| + p.command(:team_id) do |c| + c.syntax 'team_id --org <ORG> --team <TEAM>' + c.description "Retrieve a team's ID" + c.option 'org', '--org <ORG>', 'The GitHub Organization, e.g., "jekyll"' + c.option 'team', '--team <TEAM>', 'The team name, e.g., "maintainers"' - if find_executable("heroku").nil? - say "Looks like we're missing the Heroku client. Let's see if we can't install it..." - `wget -qO- https://toolbelt.heroku.com/install.sh | sh` - end - - git = Git.init - git.add "config.ru" - git.add "Rakefile" + c.action do |args, options| - if changed? - git.commit "[Jekyll Auth] Initial setup" - end + if !JekyllAuth::Commands.env_var_set? "GITHUB_TOKEN" + puts "You'll need to go to https://github.com/settings/tokens/new and create a personal access token".red + puts "Once you've got the token, prefix the jekyll-auth command with GITHUB_TOKEN=[YOUR TOKEN]".red + puts "You can also add it to a `.env` file in this directory".red + exit 1 + end - if git.remotes.any? { |remote| remote.name == "heroku" } - say "Looks like you've already got heroku set up... skipping." - else + org = options["org"] || ENV["GITHUB_ORG_ID"] + team = options["team"] - say "If you already created an app, enter it's name" - say "otherwise, hit enter, and we'll get you set up with one." - app = ask "Heroku App name?" + if org.nil? || team.nil? + puts "An org name and team ID are required.".red + puts "Usage: jekyll-auth team_id --org <ORG> --team <TEAM>" + exit 1 + end - if app == "" - say "Not a problem, let's create that heroku app for you." - sh "heroku create" + team_id = JekyllAuth::Comands.team_id(org, team) + + if found + puts "The team ID for `@#{org}/#{team}` is `#{team_id}`".green else - say "Great. Let's tell Heroku to use our existing app." - sh "heroku git:remote -a #{app}" + puts "Couldn't find the `@#{org}/#{team}` team.".red end end + end - say "Awesome. Let's teach Heroku about our GitHub app." + p.command(:serve) do |c| + c.syntax "serve" + c.description "Run Jekyll Auth site locally" + c.action do |args, options| - client_id = ask "What's your GitHub Client ID? " - sh "heroku config:set GITHUB_CLIENT_ID=#{client_id}" + # Ensure environmental variables are set + unless ["GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET"].all? { |v| JekyllAuth::Commands.env_var_set?(v) } + puts "Whoops. Looks like you forgot to tell Jekyll Auth about your app".red + puts "Be sure to run export GITHUB_CLIENT_ID=[client id], export GITHUB_CLIENT_SECRET=[client secret], and export GITHUB_ORG_ID=[org id] (or GITHUB_TEAM_ID)".red + puts "See the readme for more information on where to find these".red + exit 1 + end - client_secret = ask "What's your GitHub Client Secret? " - sh "heroku config:set GITHUB_CLIENT_SECRET=#{client_secret}" + # build site + p.go ["build"] - team_id = ask "What's your GitHub Team ID? (you can skip this in favor of an org if you prefer) " - if team_id.length > 0 - sh "heroku config:set GITHUB_TEAM_ID=#{team_id}" - else - org_id = ask "What's your GitHub Org ID? " - sh "heroku config:set GITHUB_ORG_ID=#{org_id}" + puts "Spinning up the server with authentication. Use CTRL-C to stop." + puts "To preview the site without authentication, use the `jekyll serve` command" + execute_command "bundle", "exec", "rackup", "-p", "4000" + end + end - say "We're all set. Time to deploy our code to Heroku" - system "git push heroku master --force" + p.command(:setup) do |c| + c.syntax "setup" + c.description "Configure Heroku for use with your Jekyll Auth site" + c.option "client_id", "--client_id", "Your oauth app client id" + c.option "client_secret", "--client_secret", "Your oauth app client secret" + c.option "team_id", "--team_id", "The team to authenticate against" + c.option "org_id", "--org_id", "An organization to authenticate against" + c.action do |args, options| - say "Let's check if it worked..." - sh "heroku open" + if find_executable("heroku").nil? + say "Looks like we're missing the Heroku client. Let's see if we can't install it..." + JekyllAuth::Commands.execute_command "wget", "-qO-", "https://toolbelt.heroku.com/install.sh", "|", "sh" + end - say "fin." - end -end + JekyllAuth::Commands.init_repo + JekyllAuth::Commands.initial_commit if JekyllAuth::Commands.changed? -command :serve do |c| - c.syntax = "jekyll-auth serve" - c.description = "Run Jekyll Auth site locally" - c.action do |args, options| + if JekyllAuth::Commands.heroku_remote_set? + puts "Looks like you've already got heroku set up... skipping.".green + else + puts "Creating a new Heroku app." + JekyllAuth::Commands.execute_command "heroku", "create" + end - # Ensure environmental variables are set - ["GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET"].each do |var| - next unless ENV[var].nil? - say "Whoops. Looks like you forgot to tell Jekyll Auth about your app" - say "Be sure to run export GITHUB_CLIENT_ID=[client id], export GITHUB_CLIENT_SECRET=[client secret], and export GITHUB_ORG_ID=[org id] (or GITHUB_TEAM_ID)" - say "See the readme for more information on where to find these" - exit(1) - end + puts "Configuring the Heroku app" + JekyllAuth::Commands.configure_heroku(options) - # build site - command(:build).run + puts "Pushing to Heroku" + JekyllAuth::Commands.execute_command "git", "push", "heroku", "master", "--force" - say "Spinning up the server with authentication. Use CTRL-C to stop." - say "To preview the site without authentication, use the `jekyll serve` command" - sh "bundle exec rackup -p 4000" - + puts "Lets check if it worked" + JekyllAuth::Commands.execute_command "heroku", "open" + end end -end -# Run the standard jekyll build command -# Called by Rake task, to allow the gem -# to add functionality here in the future -command :build do |c| - c.syntax = 'jekyll-auth build' - c.description = "Build Jekyll site" - c.action do |args, options| - say "building the site..." - sh "bundle exec jekyll build" - say "site built." - end + p.default_command(:serve) end - -default_command :serve