{"id":1954,"date":"2018-01-14T04:00:24","date_gmt":"2018-01-14T03:00:24","guid":{"rendered":"https:\/\/akim.sissaoui.com\/?p=1954"},"modified":"2018-01-15T15:59:04","modified_gmt":"2018-01-15T14:59:04","slug":"script-creer-un-depot-github-automatiquement-en-local-et-en-distant","status":"publish","type":"post","link":"https:\/\/akim.sissaoui.com\/en\/informatique\/script-creer-un-depot-github-automatiquement-en-local-et-en-distant\/","title":{"rendered":"Script: Create a github repository, a git local directory, and link them in a single script"},"content":{"rendered":"<p>I finally started to use Github. That will make my life easier for sure. I found git and github a bit hard to apprehend at start, until a colleague sent me the link to this great online training made by Code School and sponsored by Github. I way want to try it&#8230; Well&#8230; it&#8217;s called &#8220;Try Github&#8221; by the way: <a href=\"https:\/\/try.github.io\/\">https:\/\/try.github.io\/<\/a><\/p>\n<p>I plan to use Github for all the little scripts and files I will be creating. Much more convenient. than syncing with any kind of cloud repository like Nextcloud. As I want to use both private and public github, I had to subscribe to github&#8230;<\/p>\n<p>So, you might get it, I will create multiple gits and github repos, and as I&#8217;m lazy, I don&#8217;t want to remember and to type all commands everytime. <a href=\"https:\/\/akim.sissaoui.com\/wp-content\/uploads\/2018\/01\/github-logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-1965\" src=\"https:\/\/akim.sissaoui.com\/wp-content\/uploads\/2018\/01\/github-logo.png\" alt=\"\" width=\"363\" height=\"144\" srcset=\"https:\/\/akim.sissaoui.com\/wp-content\/uploads\/2018\/01\/github-logo.png 580w, https:\/\/akim.sissaoui.com\/wp-content\/uploads\/2018\/01\/github-logo-300x119.png 300w, https:\/\/akim.sissaoui.com\/wp-content\/uploads\/2018\/01\/github-logo-242x96.png 242w\" sizes=\"(max-width: 363px) 100vw, 363px\" \/><\/a>Therefore I made that little scripts to help.<!--more--><\/p>\n<p>This script will create a github folder in your home directory if it does not exist, then it will create a new subfolder, and finally, create a github repository on your account, private or public, and link both. You then just have to share the link and start using it.<\/p>\n<p>And obviously, the script, along with other scripts I&#8217;ll do in the future, will be available on my github repository called Superkikim (yep&#8230; that&#8217;s me) Useful-Scripts: <a href=\"https:\/\/github.com\/Superkikim\/Useful-Scripts\">https:\/\/github.com\/Superkikim\/Useful-Scripts<\/a>. That one is public. Well. You can also find the script below. Feel free to share and enjoy.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"atomic\">#!\/bin\/sh\r\n\r\n###############################################################################\r\n#\r\n#&nbsp;Author:&nbsp;&nbsp;Akim&nbsp;Sissaoui\r\n#&nbsp;Website:&nbsp;https:\/\/akim.sissaoui.com\/informatique\r\n#\r\n#&nbsp;Description:\r\n#&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;script&nbsp;ease&nbsp;the&nbsp;github&nbsp;repository&nbsp;creation\r\n#&nbsp;&nbsp;&nbsp;&nbsp;It&nbsp;will&nbsp;create&nbsp;a&nbsp;github&nbsp;folder&nbsp;in&nbsp;your&nbsp;home&nbsp;folder&nbsp;if&nbsp;it&nbsp;does&nbsp;not&nbsp;exist\r\n#&nbsp;&nbsp;&nbsp;&nbsp;Then&nbsp;it&nbsp;will&nbsp;create&nbsp;a&nbsp;new&nbsp;folder&nbsp;based&nbsp;on&nbsp;$1&nbsp;variable,&nbsp;make&nbsp;it&nbsp;a&nbsp;git&nbsp;\r\n#&nbsp;&nbsp;&nbsp;&nbsp;repo&nbsp;and&nbsp;create&nbsp;a&nbsp;github&nbsp;repository&nbsp;on&nbsp;your&nbsp;github&nbsp;account.&nbsp;$2&nbsp;allow&nbsp;you\r\n#&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;specify&nbsp;public&nbsp;or&nbsp;private.\r\n#\r\n#&nbsp;Requirement:\r\n#&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;scripts&nbsp;use&nbsp;SSH&nbsp;to&nbsp;retrieve&nbsp;your&nbsp;github&nbsp;username,&nbsp;therefore,\r\n#&nbsp;&nbsp;&nbsp;&nbsp;authentication&nbsp;via&nbsp;SSH&nbsp;to&nbsp;github&nbsp;according&nbsp;to&nbsp;following&nbsp;URL&nbsp;is&nbsp;required:\r\n#\r\n#&nbsp;&nbsp;https:\/\/help.github.com\/articles\/adding-a-new-ssh-key-to-your-github-account\/\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;An&nbsp;API&nbsp;token&nbsp;has&nbsp;been&nbsp;generated&nbsp;from&nbsp;https:\/\/github.com\/settings\/tokens&nbsp;and\r\n#&nbsp;&nbsp;&nbsp;&nbsp;added&nbsp;into&nbsp;textfile.&nbsp;Token&nbsp;needs&nbsp;admin&nbsp;tree&nbsp;permissions\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;~\/.github\r\n#\r\n#&nbsp;Usage:\r\n#&nbsp;&nbsp;&nbsp;&nbsp;.\/git-create.sh&nbsp;[repo&nbsp;name]&nbsp;[type]\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;[repo&nbsp;name]&nbsp;&nbsp;required:&nbsp;Repositery&nbsp;name.&nbsp;Must&nbsp;follow&nbsp;Github&nbsp;repository\r\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;requirements\r\n#&nbsp;&nbsp;&nbsp;&nbsp;[type]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optional:&nbsp;private&nbsp;or&nbsp;public.&nbsp;Default&nbsp;is&nbsp;private\r\n#\r\n#&nbsp;Example:\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;Below&nbsp;example&nbsp;will&nbsp;create&nbsp;MyApp&nbsp;Github&nbsp;repository&nbsp;as&nbsp;private\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;.\/git-create.sh&nbsp;MyApp&nbsp;private\r\n#\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;Below&nbsp;example&nbsp;will&nbsp;create&nbsp;MyCreation&nbsp;Github&nbsp;repository&nbsp;as&nbsp;public\r\n#\r\n#&nbsp;&nbsp;&nbsp;&nbsp;.\/git-create.sh&nbsp;MyCreation&nbsp;public\r\n#\r\n###############################################################################\r\n\r\n#&nbsp;Parse&nbsp;parameters\r\nreponame=$1\r\nprivate=$2\r\n\r\n#&nbsp;Check&nbsp;reponame\r\nif&nbsp;[&nbsp;-z&nbsp;$reponame&nbsp;]\r\nthen\r\n echo&nbsp;\"Repository&nbsp;name&nbsp;is&nbsp;required.&nbsp;Exiting\"\r\nexit&nbsp;1\r\nfi\r\n\r\n#&nbsp;Define&nbsp;if&nbsp;private&nbsp;or&nbsp;public\r\nif&nbsp;[&nbsp;-z&nbsp;$private&nbsp;]\r\nthen\r\n&nbsp;&nbsp;&nbsp;private=\"true\"\r\nelse\r\n&nbsp;&nbsp;&nbsp;case&nbsp;$private&nbsp;in\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private)\r\n private=\"true\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private=\"false\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;\"[type]&nbsp;parameter&nbsp;not&nbsp;recognized.&nbsp;Exiting.\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit&nbsp;1\r\n&nbsp;&nbsp;&nbsp;esac\r\nfi\r\n\r\n#&nbsp;Create&nbsp;github&nbsp;folder&nbsp;in&nbsp;the&nbsp;current&nbsp;user&nbsp;home&nbsp;folder&nbsp;if&nbsp;it&nbsp;does&nbsp;not&nbsp;exist\r\nif&nbsp;[&nbsp;!&nbsp;-d&nbsp;~\/github&nbsp;];&nbsp;then\r\n&nbsp;&nbsp;mkdir&nbsp;~\/github\r\nfi\r\n\r\n#&nbsp;Test&nbsp;the&nbsp;ssh&nbsp;authentication&nbsp;and&nbsp;save&nbsp;the&nbsp;result&nbsp;in&nbsp;the&nbsp;$git_user&nbsp;variable\r\ngit_user=$(ssh&nbsp;-T&nbsp;git@github.com&nbsp;-o&nbsp;\"StrictHostKeyChecking&nbsp;no\"&nbsp;2>&1&nbsp;>&nbsp;\/dev\/null)\r\n\r\n#&nbsp;Verify&nbsp;if&nbsp;ssh&nbsp;connection&nbsp;was&nbsp;successfull\r\nsuccess=`grep&nbsp;-c&nbsp;successfully&nbsp;<<<&nbsp;\"$git_user\"`\r\n\r\n#&nbsp;If&nbsp;ssh&nbsp;connection&nbsp;was&nbsp;successful,&nbsp;parse&nbsp;username&nbsp;otherwise&nbsp;exit&nbsp;with&nbsp;exit&nbsp;code&nbsp;1\r\nif&nbsp;[&nbsp;$success&nbsp;-eq&nbsp;1&nbsp;]\r\nthen\r\n git_user=`echo&nbsp;$git_user&nbsp;|&nbsp;sed&nbsp;-e&nbsp;'s\/.*Hi&nbsp;\/\/'&nbsp;-e&nbsp;'s\/You.*\/\/'`\r\n git_user=${git_user%??}\r\nelse\r\n echo&nbsp;\"ssh&nbsp;authentication&nbsp;failed.&nbsp;Please&nbsp;verify&nbsp;ssh&nbsp;authentication.\"\r\n exit&nbsp;1\r\nfi\r\n\r\n#&nbsp;Check&nbsp;if&nbsp;repository&nbsp;exists\r\nfor&nbsp;repo&nbsp;in&nbsp;$(curl&nbsp;-s&nbsp;-H&nbsp;\"Authorization:&nbsp;token&nbsp;`cat&nbsp;~\/.github`\"&nbsp;https:\/\/api.github.com\/user\/repos&nbsp;|&nbsp;grep&nbsp;-o&nbsp;'git@[^\"]*'&nbsp;|&nbsp;grep&nbsp;.git)\r\ndo\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;[&nbsp;$reponame&nbsp;=&nbsp;$(echo&nbsp;$repo&nbsp;|&nbsp;sed&nbsp;-e&nbsp;'s\/.*\/\/\/'&nbsp;-e&nbsp;'s\/.git.*\/\/')&nbsp;]\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;\"Repository&nbsp;with&nbsp;name&nbsp;$reponame&nbsp;already&nbsp;exists.&nbsp;Exiting.\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit&nbsp;1\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi\r\ndone\r\n\r\n#&nbsp;Check&nbsp;if&nbsp;local&nbsp;folder&nbsp;already&nbsp;exit\r\nif&nbsp;[&nbsp;-d&nbsp;~\/github\/$reponame&nbsp;]\r\nthen\r\n&nbsp;&nbsp;&nbsp;echo&nbsp;\"Folder&nbsp;~\/github\/$reponame&nbsp;already&nbsp;exist.&nbsp;Exiting.\"\r\n&nbsp;&nbsp;&nbsp;exit&nbsp;1\r\nfi\r\n\r\n#&nbsp;Create&nbsp;local&nbsp;folder&nbsp;repository\r\nmkdir&nbsp;~\/github\/$reponame\r\ncd&nbsp;~\/github\/$reponame\r\ngit&nbsp;init\r\n\r\n#&nbsp;Create&nbsp;remote&nbsp;repository\r\ncurl&nbsp;-H&nbsp;\"Authorization:&nbsp;token&nbsp;`cat&nbsp;~\/.github`\"&nbsp;https:\/\/api.github.com\/user\/repos&nbsp;-d&nbsp;\"{\"name\":\"$reponame\",\"private\":$private}\"\r\ngit&nbsp;remote&nbsp;add&nbsp;origin&nbsp;https:\/\/github.com\/$git_user\/$reponame.git\r\n\r\necho&nbsp;\"Repository&nbsp;$reponame&nbsp;has&nbsp;been&nbsp;created&nbsp;in&nbsp;~\/github\/$reponame&nbsp;and&nbsp;as&nbsp;remote&nbsp;repository&nbsp;at&nbsp;https:\/\/github.com\/$git_user\/$reponame\"\r\nif&nbsp;[&nbsp;$private&nbsp;=&nbsp;\"true\"&nbsp;]\r\nthen\r\n&nbsp;&nbsp;&nbsp;echo&nbsp;\"This&nbsp;is&nbsp;a&nbsp;private&nbsp;repository.\"\r\nfi&nbsp;\r\necho\r\necho&nbsp;\"You&nbsp;can&nbsp;now&nbsp;add&nbsp;files,&nbsp;commit&nbsp;and&nbsp;push&nbsp;to&nbsp;populate&nbsp;the&nbsp;remote&nbsp;repository\"\r\necho\r\nexit&nbsp;0\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally started to use Github. That will make my life easier for sure. I found git and github a bit hard to apprehend at start, until a colleague sent me the link to this great online training made by Code School and sponsored by Github. I way want to try it&#8230; Well&#8230; it&#8217;s called [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[220],"tags":[82,251,264],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/1954"}],"collection":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/comments?post=1954"}],"version-history":[{"count":15,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/1954\/revisions"}],"predecessor-version":[{"id":1989,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/posts\/1954\/revisions\/1989"}],"wp:attachment":[{"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/media?parent=1954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/categories?post=1954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/akim.sissaoui.com\/en\/wp-json\/wp\/v2\/tags?post=1954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}