From a822327745f4026643a749d4c1e9e1d1d3adb01b Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Mon, 5 Apr 2021 12:08:15 +0200 Subject: [PATCH] Add createrepo script --- script/createrepo | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 script/createrepo diff --git a/script/createrepo b/script/createrepo new file mode 100755 index 0000000..d13020e --- /dev/null +++ b/script/createrepo @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# +# frozen_string_literal: true + +# Create a Git repo on git.netflux.io + +require 'json' +require 'net/http' +require 'optparse' +require 'uri' + +options = {} +banner = 'Usage: createrepo [options] ' + +OptionParser.new do |opts| + opts.banner = banner + + opts.on('--private', 'Make repo private') do |v| + options[:private] = v + end + + opts.on('-d', '--description=DESC', 'Description') do |v| + options[:description] = v + end +end.parse! + +options[:name] = ARGV.pop.to_s.strip +if options[:name].empty? + puts banner + exit 1 +end + +auth_token = ENV['GIT_NETFLUX_IO_TOKEN'] +if !auth_token || auth_token.empty? + puts 'Env var GIT_NETFLUX_IO_TOKEN must be set' + exit 1 +end + +uri = URI('https://git.netflux.io/api/v1/user/repos') +auth_string = "token #{auth_token}" + +req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Authorization' => auth_string) +req.body = options.to_json + +Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + resp = http.request(req) + if resp.code != '201' + begin + errmsg = JSON.parse(resp.body) + puts errmsg['message'] + rescue JSON::ParserError + puts resp.body + end + exit 1 + end + + repo = JSON.parse(resp.body) + puts repo['ssh_url'] +end