0% found this document useful (0 votes)
26 views40 pages

(English (Auto-Generated) ) Day-8 - DevOps Zero To Hero - Shell Scripting Project Used in Real Time - GitHub API Integration (DownSub - Com)

SHELL SCRIPTING

Uploaded by

pkg2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views40 pages

(English (Auto-Generated) ) Day-8 - DevOps Zero To Hero - Shell Scripting Project Used in Real Time - GitHub API Integration (DownSub - Com)

SHELL SCRIPTING

Uploaded by

pkg2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 40

hello everyone my name is abishek and

welcome back to my channel I know some

of you are very surprised you might be

thinking day eight of devop zero2 hero

series but abishek you have already

completed this 6 months back yes I'm

going to do take two of this particular

video in the devop Z to Hero playlist

I'll tell you why in a minute but before

that if there are any new subscribers or

anyone new who is watching this channel

or this particular video so we already

have a devop Zero to Hero playlist which

covers devops right from Basics so even

if you have zero knowledge on devops you

can start with this particular playlist

and you can learn devops from it there

are hundreds of people who followed this

as well as our AWS devops playlist and

were able to get the dream devops jobs

so if you haven't followed just

subscribe to the playlist and you can

start watching right from day one now

going back if this playlist is already

done why I'm doing this particular video

one more time that is day8 devops

project using shell scripting GitHub API

integration so there is a reason of

course this video has 37k views it was

created eight months ago and many people


liked it right if you see the comments

you will notice that a lot of people

were able to follow this and they were

also able to implement it but there are

few people who were not able to follow

this as well if you watch all the

comments I usually read all the comments

there are some concerns about the video

concerns are basically that you know

some of them are not able to follow this

video some of them are not able to

understand it

completely and I don't want to leave

anyone who is watching this playlist

right you know I don't want anyone to be

blogged on any video on the playlist so

that's why what I thought is I will do

take two of this video even better one

with better understanding and complete

code script on the GitHub repository so

that you can watch this video as well as

you can try this video at your end and

you will understand the real time use

case in a much better way there were

only three to four comments which were

saying that they were not able to follow

this video completely but I don't want

to even leave that three to four people

I want everyone to follow our playlist


and understand devops right from Basics

and learn devops and Cloud that's the

reason why I'm doing it so this is the

first take to video on the channel and

probably never know if there is some

other video where you know people are

finding it very difficult to understand

as the time grows even I will become

better person right because as I keep

doing videos sometimes I feel that okay

uh this was the better way of doing a

previous video that I've done probably

if I find something like that I'll do

take to I don't find anything awkward in

it because I want to share knowledge as

much as possible and I want to reach as

many people as possible perfect now keep

this video aside I'll explain you right

from basic what exactly we are going to

learn so let's say that you are a devops

engineer and this is one of the

repository that you are maintaining that

means you are creating users right and

you are monitoring this GitHub

repository monitoring in the sense if if

there is some vulnerabilities on the

repo or you know you are creating cicd

pipelines on this repo so basically you

own this repository and now someone asks

you that abishek uh you know I don't


have access uh to the settings tab and I

just want to see who are the list of

people who have access to this

particular

repository you know uh what you would do

you would cck simply click on the

settings tab and you would go to the

collaborators and you will find who are

the list of people and you will probably

send them a screenshot right right but

as a devops engineer you want to

understand this on a daily basis or you

know let's say someone is resigning your

organization okay and you want to see if

this person has access to this repo if

if that person has access to the repo

you have to revoke the access so on a

day-to-day basis you might want to go to

the GitHub repository and see who has

access to the repository someone wants

to have the read access someone wants to

have the right access so you will just

go to the repository see if that person

already has read access so you can

promote them to write access so to do

all of these things every time instead

of logging to the repository right

instead of every time logging to the

GitHub repository what you can do is you


can write a shell

script okay and what this shell script

requires this shell script requires

GitHub

integration now let's see how does this

GitHub integration work right so on a

broad level okay if you want to

integrate or if you want to talk to any

application so there are two ways to

talk to it one is

API and the other is

CLI so you can talk to applications

using the API or the CLI applications it

can be GitHub it can be genkins gitlab

or anything even that can be kubernetes

right for kubernetes we mostly use Cube

CTL right what is Cube CTL Cube CTL is

the CLI option but you can also talk to

kubernetes using the apis right but

because Cube CTL is a very simple

utility we prefer using Cube CTL now

similarly for GitHub also you have a CLI

and you have an API but for GitHub API

is much simpler because API was there

traditionally for a long time and you

can write scripts it can be shell

scripting that it can be bash it can be

python it can be nodejs Java any

programming language so what you can do

is you can directly talk to the API of


GitHub and you can get the required

information now some people might wonder

but abishek what exactly is API I don't

know what is API that's fine API

basically means application

interface okay so again what exactly is

this so for example the one that you're

seeing here right I am talking to

github.com but how am I talking to

github.com I am talking to github.com

through the user interface understand it

very clearly okay so I'm going to my

browser and in the browser I typed

github.com and then this particular page

popped up so this is called as user

interface okay whereas in instead of

doing all of these things you can even

get the information from GitHub from

this particular repository

programmatically so first one is a user

interface where you are seeing it

directly on your browser but the second

one that I'm talking about is getting

this information programmatically what

is programmatically that means you can

basically talk to these applications

through shell scripting python or any

other programming language if that

particular application expose their


application interface okay where you

will run some uh modules for Shell

scripting you have curl for python you

have the request module okay so using

this modules what you can do is you will

use the HTTP protocol or these days

people also use the grpc protocol which

is Advanced to http but let's restrict

our discussion to http so using C or

request or depending upon your

programming language there are some

modules and using this modules you will

directly talk to the application so you

don't want to go to your browser you

don't want to go any other place but you

can use either

Postman or you can use uh call command

requests uh if you're using python or

depending upon your programming language

any module and you can get the

information

right so if you are getting this

information in such a way then basically

you talking to that programmatically

through the application interface so

that's why it's called as

API now the question is should devops

Engineers write these

apis the answer is no as a devops

Engineers you don't write the apis but


you use the apis or you consume the

apis simple and best example is is

boto3 right when you want to talk to

AWS either you can use the AWS CLI which

is the CLI utility that AWS provides you

where you can just say AWS S3 LS you can

get all the list of AWS buckets or what

you can do you can write a Python

program you can use the boto3 module and

you can interact with AWS so this is CLI

way of doing it whereas this is doing

through the API that is application

interface similarly you have github.com

or jira gitlab genkins all of them have

their own apis written by the developers

and you will go to the documentation so

for every API there is a API reference

documentation okay whenever developers

create the apis they write the reference

documentations because without reference

documentation devops Engineers won't

understand how to make request to this

apis right devops Engineers know how to

use Curl devops Engineers know how to

use request module but devops Engineers

does not know what is the API that is

what is the URL for the API you will

understand in a minute what is the URL

for the API and in the URL how to get


particular information for

example so now I am in this particular

repository called Argo CD operator for

example I want to find

the pull request in this repository

because this is a user interface I can

simply see everything and here I see a

button for pull request I'll click on

this button and I'll see all the pull

requests right but for API how will you

understand that that is in your shell

scripting or in your python scripting or

any other programming language how would

you understand that what should be the

URL or what should be the API URL that

you should hit I'll give you example

right for

example you just want to find the pull

request then what you will do is you

will go to the GitHub API

doc I'll show you the API documentation

so you will search for GitHub API

documentation and in the GitHub API

documentation you will search for pull

requests where the GitHub API

documentation will tell you that okay in

your shell scripting try to hit this

particular URL and you'll get the list

of pull requests so let's see so what

I'll search is GitHub API


talk see I got the GitHub API rest

documentation that is using HTTP

protocol how should you access the

GitHub apis now here you have bunch of

things right so best example I wanted to

get the pull request so I'll search this

entire thing and I scroll down I found

some option called pulls and here I'll

click on this page where GitHub tells

you that okay if you want to get let me

increase the font so if you want to get

the list of pull request on a particular

repository this is the API that you want

to access and they also shared a sample

script right see this is a beauty of API

documentation so there is also a sample

script where they are saying if you are

writing this code in Shell scripting

then use this particular Cur command why

are you using Cur command because in

Shell scripting curl is a module to

interact with the apis if you're using

python request is a module if you're

using JavaScript see they have provided

module for JavaScript as well if you're

using GitHub CLI then this is how you do

it right now we are focused on the shell

scripting part so this is the curl

command and this is the URL they are


saying right so this is the URL that you

have to hit what is it

https api. github.com watch it carefully

this URL is github.com whereas this URL

is api. github.com / repos SL owner/

repo SL pulls so here you have to

replace owner and repo with your repo

details if your repository is kubernetes

then here you have to provide kubernetes

/ kubernetes if your repository is

something of your own then here you have

to provide your organization name if it

is my repository then here I'll provide

I am vamala and here I'll provide the

name of the repository let's say devop 0

to hero or AWS 0 To Hero I'll show that

in the Shell scripting format as well

don't worry but for now you understood

how to read the API documentation right

for example I want to understand about

uh the number of let's say I want to get

the number of um issues okay so that is

another easy thing right so this is a

repository and I want to get the list of

issues on the user interface I'll simply

click on the issues tab but for the API

documentation what you can do is again

go back and find search for issues tab

so this is the issues Tab and I'll click

on the issues page again you will have


the API documentation where it says okay

if you want to get list of issues then

this is the API command okay once you

enter this API command you will get the

list of issues so here again they have

clearly provided in the issues tab

these are the different apis that GitHub

exposes list issues assigned to a user

or get an issue you can also create

issues right so let's say you want to

create issue through a shell scripting

so this is the shell scripting

command it is not mandatory in the API

documentation you should get all of

these things GitHub has a very good API

documentation so that's why they are

sharing some sample script as well but

let's say for some other documentation

they don't share the sample script but

they will definitely share you this

particular thing okay how should you hit

the API so here the API is https api.

github.com repos owner repo issues right

here again both of these the both of

these things will

change how did I tell you because here

these are in the capitals and rest all

in the small so here Capital means that

you have to replace these ones with the


actual ones so these are just example

okay so now let's try to understand what

are we learning today so what I'll teach

you today is we will take an example so

this is a very common task that devops

Engineers do I'm not creating it this is

a very very common task that is as a

devops

engineer you maintain a lot of GitHub

repositories so again it can be GitHub

bit bucket git bash sorry uh gitlab

anything let's say you maintain a lot of

GitHub repositories what does maintain

mean here so as a devops engineer you

support multiple

teams right you support multiple teams

and each team let's say each team works

on a micros service for each team you

will create a GitHub

repo and you will not just create the

GitHub repo for them but what you will

also do is for each

repository you will make sure proper

access is granted that means only people

who require access to the repo are

granted with the access right and then

you will make sure that you will write

the cicd pipelines and whatever is

required now in today's video what I am

going to teach you is as a devops


engineer how can you

list how can you list

people who have access to the

repo now why is this required let's say

someone is resigning someone has

resigned to the organization and today

is their last day and as a devops

engineer you want to find if this

particular person has access to the repo

if he has access you want to revoke the

access now every time you don't want to

go to the GitHub UI search for

repository search for the settings page

go to the collaborators and if this

person has access you have to revoke the

access instead of doing this what you

can do first you can list group of

people who have access to the repository

and if the person is there then either

through shell scripting or manually you

you can revoke the access because this

is a quite common task what you can do

is instead of manually doing it you can

automate this with shell script and

that's what I'll show you today okay so

this particular script is available I'll

share the script in the description so

everything is written in a very detailed

way but but I will walk you through the


script and I'll execute the script and

I'm also going to show you how to

improve the script so please watch it

till the end because we will not just

run how to run how to execute the script

but I am also going to show you some

improvements and I'll walk you through

the entire script so that you will

understand how devops Engineers write

shell scripts for realtime use

cases okay let's say that there is this

GitHub repository GitHub kubernetes /

kubernetes now what is this

repository kubernetes kubernetes is the

official repository for kubernetes

documentation sorry uh kubernetes source

code right here you don't know who have

access to this repository because you

are not the owner of this repository but

in your organization as a devops

engineer you have access to the

repository and you know who exactly have

access to it now what open source

projects do is they create a particular

file called owners okay and here you

have list of people who have access to

the

repository but in your organization you

will directly have access to a settings

tab and within the set settings tab you


can see who has access for example I

have a particular organization that I

have created which is called devops by

examples okay so this is a organization

that I created just for my practice

purpose you can also create

organizations just go here and create

organization but I'll not deep dive into

it because it will take a lot more time

so you can practice on your organization

if you're working in an organization you

can practice it or else you can create a

organization and create a repository and

then you can play around with it for

example like I told you this is the

organization that I already have and

here I have some repositories so let's

say I want to go to this particular uh

repository called Jenkin 02

hero okay this is a fork uh instead of

that let's go to a actual repository

only so devolves by examples um I think

this is a repository that I okay not

even this

one so I just want to take a

repository let's click on view all

repositories so you can pick up

anything so this is one repository let's

say complete devop score


okay and here you will see a settings

settings tab I mean I'm seeing a

settings tab because I'm owner of this

organization and a

reposit here there will be an option

called collaborators and team okay I

just have to enter my password one

time okay so

here it will list you all the

collaborators and users now the reason

why there are no users no collaborators

is because nobody has access to this

Repository within my organization but

there is another repository that I know

for example this one where I am

collaborating with some others on this

repository so if I go to a settings tab

in the collaborators and team I'll see

one person right so this is one person

who has access to the repository now

every time if I want to go and check who

has access to repositories in

organization it's a difficult task

because I have to log into github.com I

have to go to that repository go to that

organization and find who has access but

if you run this particular script that

devops Engineers use on a day-to-day

basis okay so what we will do is we will

clone this reposit uh clone this


particular shell script onto an E2

instance we will execute against this

particular repository and see if it is

working fine or not later I'll explain

you the script and I'll also tell how to

improve it so firstly we will use an ec2

instance okay so let's go to a WS ec2

instances let's launch a instance right

from the sketch so that everybody can

follow this particular project so here

let's say shell script

example we will take UB

2 use your uh public key whatever it is

click on the launch

instance okay now after this what we

will do once the instance is created we

will use a terminal and we will log into

it if you're using puty you need to use

a PPK file not the PM file let's say you

are using mobile exm or something

depending upon your if you're on Windows

or Linux you know you need to take pem

file or PPK file you have to import that

to your mobile xterm or puty and you

have to log into it if you are not aware

of this information you can watch day

three of AWS devop 0 To Hero series

there I have EXP explain very clearly

how to do it
okay now let me

refresh I hope the instance is created

perfect shell scripting example copy

this one take a

terminal okay I have a terminal

SSH controlr

ssh and here I'll replace this ID with

the

ID this is the IP address right

accept the

connection perfect now let's clone this

particular shell

script so I already copied uh the shell

script path what I'll do is get clone

this is the shell scripting project

right where did I get this from you can

just clone this repository just click on

this one you can find this link in the

description and you will have two

folders this is a previous project that

we have done and this is the

today's project okay so what I'll do LS

CD shell script projects and inside that

I'll go to the GitHub

API right so this is a shell script that

we got list users. SH now don't worry

I'm executing it directly I'll show you

how to use it after that and there is

one prerequisite for this shell script

that is you need to export your username


so the command for that is export

username is equals to whatever is your

username so my GitHub username is I am

vamala and then you also have to export

your

token what is this token let me tell you

so whenever you are logging in to

github.com what you will do is you will

provide your username and password right

so for the apis you will not have

username and password but you will use

some something called as API token so

how to create that API token for

github.com what you can do is you can

scroll down you will find settings uh

Tab and then you will have a tab called

developer settings right in the

developer settings you can click on

personal access tokens click on the

classic tokens and here what you can do

is you can generate a new token just

click on this button say generate new

token

and for now just call it as test and

Grant the entire access or you can grant

access to whatever you would like to

okay for now just for the purpose of uh

this demo project what I'll do is I'll

just remove the delete uh ones and the


admin ones and I'll just grant all the

other

accesses but you just have to be very

careful about it because if someone gets

access to this uh API token then they

can technically access the entire

github.com of yours okay so there is a

good chance of getting hacked so

whenever you are doing such things what

you can do is you can stop I'm stop

sharing the screen because I don't want

to share this with anyone similarly you

can also not share this with anyone

click on generate token which I'm

doing okay generate token perfect I have

generated the token and I have copied it

right so now what I'll do is I'll go

back to my terminal I know you are still

not seeing my screen but what I'm doing

is I'm exporting the token as well so

now I have exported the token I've

exported the username now I'll start

sharing the screen

again done so now you are seeing my

screen I have exported the token I have

exported the username now what I will do

is I will execute this particular script

and for this script you know you have to

provide two command line

arguments why you need to provide that


when I walk you through the script you

will understand

but the command line arguments are very

simple one is you know for whichever

repository that you want to get the list

of users who has access to it what you

need to do is you have to provide the

organization name followed by the

repository name in this case

organization name is devops by examples

and the repository name is python okay

so that is what I'll provide

devops by examples

and python is the repository name I will

execute let's see if it provides

required information or not it said

permission denied because I have to

Grant access to the script for now let

me give

777 we have learned in the previous

classes what this 777 means and how to

read write execute I mean how to provide

those accesses to yourself your group

and others so if you don't know what

this 777 means please watch the previous

videos perfect now let's try to execute

one more

time what does it say JQ command not F

that's still okay that's the reason is


this script requires you to install JQ

so let me install JQ

first Pudo apt install

JQ minus y now don't worry I haven't

explained you the script yet because

first I want to show the use case and

then I will show you and I walk you

through the script that way you will

understand this in a much better way

right

perfect oh sorry I'm doing it again now

let me execute the

script see it says Abhishek viala has

access to it bavya Bali and

moit these are the people who have

access you might be wondering abishek

why exactly it is only showing moit

because I have only selected the read

okay if you select all the uh here

right Ro type oh sorry uh the reason why

is it sh showing only moit is because

other two people are the owners of this

repository okay so the reason why it

doesn't show bavya Bali and I am vamala

because I am vamala is me and bavya has

access to this repository but we are the

admins of it right here it clearly

indicates see all two members can access

this Repository one has access to this

repository which is a outside


collaborator so moit is an outside

collaborator that's why it directly

shows moit here whereas two members

right is a am vamala and bavya Bali who

are the admins of this repository so

that's why it does not show up here if

you execute this project in your

organization it will show list of people

who has access to it doesn't matter

outside collaborator or uh you know this

particular thing in in this case because

you know both of us are the admins of

this repository that's why it's not

showing up otherwise this is the one

that is required and moit is here so

what you can

do as a devops engineer no you will just

execute this and you will understand

okay so these people have access to the

repository now another question is

abishek what if I want to get access to

other repositories it's very simple just

change

the organization name and just change

the repo name for example right uh let's

say I want to understand who has access

to kubernetes

kubernetes okay so github.com

uh my bad github.com kubernetes


kubernetes what did I do wrong uh

spelling

mistake Okay so here let's say I want to

get access list of people who has access

to this repository so what I'll do in

the script I'll just change it to

kubernetes

kubernetes see what does it say it says

cannot index string with string

permissions I mean the script has failed

because I don't have access to it right

through the user interface if I cannot

do something through the API also you

cannot do that thing that is because I

don't have access to see the settings

tab itself whereas for this particular

repository I can see the settings tab

that's why I was able to list people who

have access to this repository for

kubernetes / kubernetes I cannot do it

because I don't have access to it now

let's say you want to practice this if

you are working in an organization you

can directly try this project there is

no problem at all because this is just

listing people who has access to repo so

there will not be any problem you can do

it but otherwise okay let's say you want

to try it by yourself not on your

organization what you need to do is go


to your github.com Okay click on your

organizations and then you have option

to create new

organization okay and in the new

organization you have to select create a

free

organization and then you have to

provide your organization account name

few more details and then what will

happen is you will get list of

organizations like you see here this is

just nothing but so for example these

are the list of organizations that I

working for all the open source projects

similarly you can create your own

organization and within your

organization you can add new people you

can create repositories it's just like

you are creating a new open source

organization okay just like you are

creating a new company right for your

company which you are working in they

will already have an organization

because they have a GitHub account right

I hope that is clear if you want to try

this project by yourself you can create

organization you can create repositories

and start giving access to some external

collaborators right or you can create


members in it and you can add

members but let's not go into the

details of it because you can try it and

you can simply do it now let's see and

let's talk about the script first

because we executed the script and we

got list of people who have access to it

but how is that possible what exactly is

this script doing right uh if I go back

where exactly is this script

that's fine I can open this one more

time so this is a script and what I am

doing in the script is basically I'm

just following the GitHub API

documentation I've shown you the GitHub

API documentation and using the GitHub

API documentation taking help of it I've

written the script so what did I do

first is for a shell scripting you

always have to start with this shebang

followed by how we are writing the shell

script right here I'm writing the shell

script in The Bash if you're writing in

sh just replace this with s sh if you're

writing in bash then keep this

particular syntax as the first line and

then I'm saying this is my API URL api.

github.com

after that if you remember I have

exported the username and the token


because whoever is reading this script

they should not know your username and

they should not know your token because

both of them are sensitive information

so that's why what I've done is when I'm

writing the shell script I am taking

these things from the terminal or from

the

environment right so whoever executes

the script instead of hardcoding those

values in the script you can export

those values on your Linux terminal and

then the script can read them right

after that this is the information that

we are passing what exactly we are

passing for example here I said devops

by examples Python and this values are

the command line arguments right this is

the script and these are the command

line arguments that I'm providing to the

script and command line arguments can be

read in this way dollar one indicates

this command line argument dollar two

indicates this command line

argument right so repo owner and repo

name in this case repo owner or the

organization name is devops by examples

and this is the repo name so now we got

the GitHub API URL then we got username


password for authenticating with API do

GitHub API and I have the details of

where to fetch these things from that

means for which repository it is

required now all that I'm doing in this

two functions okay I have split this

into two functions but you can write

this in one single function as well in

this function I am just free phasing the

URL that is you know I'm just forming

this particular URL api. github.com

followed by the API okay this does not

read a function but to write efficiently

what you need to do is create a function

for it and in the function you can

phrase the entire Cur Command right so

how I'm forming the Cur command so here

I'm getting what is the end point okay

let's start from here so that it will be

very simple for you so my end goal is to

form this particular entire command that

you are seeing okay when I'm writing the

script what I have decided is I will

form this entire curl command Okay this

entire curl command I will form in the

different function and you know what I

will do is rest all things I will

execute in a different function so

that's what I'm doing here so I'm

forming the C command what is the output


of this function or what is the final

thing of this function it is forming the

Cur command whereas this function here

it is trying to print the output right

if the uh if we execute this particular

function it is printing some output

right if you execute this particular uh

script with the devops do devops by

examples and python this is the output

that you're seeing so what I'm doing for

taking inputs and printing output I have

created a function and then I have

created a different function

just for forming the API URL and all the

parameters that are required for the API

that is the curl command and all the

things in a different function inputs

and output in a different function right

so here if you see this function this

has nothing what is it doing it is

basically calling this particular

function right see here what is it doing

it is just invoking the function with

the required information and then what

it is doing it is using using JQ to get

the output I'll make it very simple

don't worry if you if you did not

understand okay what exactly this

function is doing firstly it is taking


the input of what exactly is the end

point okay here what is the end point

means this particular

thing repos owner repo issues so this is

the API and this is the API endpoint so

I've taken this API endpoint as an input

here which is hardcoded because this

particular shell script only does list

users and it does not anything else so

that's why we can hard code this

information so firstly I have hardcoded

this right after that what I'm doing is

I am invoking this particular function

which is creating the curl Command right

how I'm invoking I'm just passing this

particular end point so that this entire

curl command is

formed right this entire curl command is

formed now once this command is formed

what I am doing is I am executing this C

command and once the executed

information is done what github.com will

do when you execute this particular API

it will give you entire Json information

github.com or most of the apis they will

only give Json information that means

let's say for this particular uh

function if I remove this GQ thing okay

this will be very interesting please

watch it carefully so what I'm going to


do is I going to remove this JQ

thing then you will notice that GitHub

is actually giving you the Json

information okay let me just close the

bracket and most of the apas will give

you Json information depending upon the

programming Lang language you have to

use different modules and you have to

get the required information for

example now if I execute this see you

got the Json output whereas using JQ

what I've have done is from this entire

information I just need the usern name

that is what is the login name of the

particular user who has access that is

moit

436 see here when I am taking the Json

information you see that there are three

users one is I am vamala then another is

bavya Bali and another is moit now why

did I print only moit because the reason

for that is both of these people are

admins and I already know they have

access to it because let's say they are

from the devops team and you already

know because they are from the devops

team they have admin access to it and

they have all the permissions but what

my focus is to print only the list of


developers or list of people who can

read this repository and write to this

repository and not the admins or the

owners of this repository how do how do

I get that information so in the Json

there is one particular field called

permission inside the permission there

is something called admin true or false

so here for moit admin is false whereas

if you

check this one I am vamala permissions

admin is true right whereas for moit

permissions admin is false so that's why

what I'm doing in the shell script if

you go back to the project using JQ I am

just selecting permissions pull is

equals to True right here permissions

pull is equals to true I'm using this

parameter if you use permissions admin

that is also fine but here what I'm

using is one particular field when I

when I've written this shell script but

you can choose any of these fields to

determine if you want to get only moit

or if you want to get bavya plus abishek

as well so you can play on these fields

basically okay but going back so what

I've done is using JQ I have selected

what type of users do I need and then I

am printing their login name right so in


JQ it is very very simple whichever

fields that you want to get you just

have to use Dot and followed by the

field for example here this is one Json

object moit has one Json object abishek

has one Json object and then bavia also

has a Json object so what you need to do

inside this Json object let's say you

want to get the value of permissions

admin what you will do is you'll just

simply write JQ space. permissions.

admin right see that is what I did do

permissions do pull okay I choose this

particular parameter but using this

particular statement what I am saying is

if permissions. pull is equals to is

equals to

True okay permissions. pull is equals to

is equals to true then print the do

login okay that is print the username uh

where exactly it is yeah print the usern

name that is login and you'll get this

particular output got it so why are we

using JQ exactly is instead of printing

this entire Json information if you want

to get only particular information from

the Json payload in Shell scripting you

will use JQ whereas if you're using

python you will use the request module


depending upon programming language you

will change this particular thing but

going back to the thing in this

particular function I have taken the

input what exactly is the Endo then you

know I am just calling this function

which is creating the curl command and

then I'm executing the curl command and

I'm getting the required information

using

JQ finally what I'm doing is I am

printing the list of users okay so here

what this if block indicates if the list

of collaborators that is output of this

particular thing in our case output is

moit right if list of collaborators is

empty then print no user has read access

whereas if the list of collaborators is

not empty then you print the list of

collaborators names in our case the list

of collaborator names is printed that is

moit right so this exactly is what the

function is doing or the entire shell

script is doing now how can you improve

this shell script script basically what

you can do is

firstly write the comment section for it

that is write the about script

section this you can take an assignment

because this is very very simple and it


does not impact the function overall or

the script overall so what you can do

here is write the about of the script

what exactly the script will do right

what are the input parameters that are

required to the script that is you have

to export your username then you have to

export ort your password and then you

have to provide two input

things and then you can say who is the

owner of this script if there are any

issues whom you should contact right

these are the things that you'll add to

the script and then what you can also do

is you can add a helper function so to

add a helper function what you can do

see watch The Script carefully most of

the uh shell scripts have helper

functions that is let's say you are

executing the script wrong let's say

someone is not reading your script and

just executing list users.

sh currently it will just throw you an

error right saying that the the output

of the script is wrong but what I want

to print here is whenever someone

executes this list user Dosh without

giving the command line arguments then I

have to tell them that your way of


executing the script is wrong for that

what you can do is you can just write

something like this helper function

right and inside the helper function

what you can say

is if you know uh you can start

with this particular thing which says if

the list of command line arguments okay

if the list of command line arguments is

not equal to okay if the list of command

line arguments is not equals

to uh how many command line arguments

are we passing uh let's say we can

create a variable as well here I'll say

expected CMD undor ARS is equals to two

for example then here we can say if

number of command line arguments is not

equals to this particular thing then

what I can do is then I can simply say

Echo please execute the

script with require Ed CMD ARs and you

can also print few things about the

required CMD ARS that's what you will

get right let's say you are executing

Cube Cil command in a wrong way then you

will get some uh error or you will get

some hints so how you can write those

errors or helps hints is using the

helper functions and what you can do is

basically so here I wrote a helper


function but I need to invoke the helper

function as well right so for that what

you can do is you can call the helper

function at the starting of your script

so here you can call the helper function

and what it will do is when you're

calling it at the beginning of your

script it will first invoke this

particular function and it will see if

the function is created properly or not

I mean if the fun if the shell script is

executed properly or not only if the

shell script is executed properly by

executed properly I mean the user has

given all the required parameters sorry

I shouldn't have said executed properly

if the shell script is invoked with all

the required parameters then move with

the next lines of the

script if user has not provided all the

required information then you can fail

the script here itself saying that hey

please execute the script with all the

required parameters so this is your

assignment it's very simple I've already

written the code I thought I'll just

give it as an assignment but later I

wrote the script as well while I'm

explaining so this is the helper


function and this is the uh comment

section that you have to right so that

the script is full-fledged got it but

you can also execute it if you don't

have an organization create an

organization and create a repo it should

not take more than five minutes for you

to create or and create a repo and after

that let's say you are already working

in your organization you can try the

script and you can execute it so this

way if you have 100 repositories

thousand repositories you can simply

write a fur Loop for in list of all your

repositories and then you can EX execute

the script one time 10 times 100 times

and you can get list of users who have

access to it right so now I hope this

video is clear right the reason why I'm

doing it again is because some people

did not find day eight uh complete or

some people felt that it needs more

attention so that's why I did this video

again I'll add this video also to the

devop 02 hero playlist right after day8

right I'll provide the name as day8 take

to so people can watch this one as well

as the previous video thank you so much

for watching today's video I hope this

information is
useful in future I'll make more and more

interesting videos so please subscribe

to the channel if you haven't take care

everyone

bye-bye take care

bye-bye

You might also like