Golang 1.16 | cobra , promptui CLI | interactive and non-interactive mode
- Data Structure
- Algorithm
- Complexity
- Running Pre-built Image
- Building image
- Program Components
- Command reference
- Program Specification
- Assumptions
- Libraries used
User and roles
--------------
map[roleId]-> [userId]->Pointer to user details
Role Hierarchy
--------------
map [parent roleId] -> [subordinate roleId] -> Pointer to role details
Calculating Indexes
-------------------
for each users in the users file
Add the user to the map of user roles
Add the user to the map of userId
end
for each of the roles in the roles file
Add the roles in the hash of the parent role key
end
Finding Subordinates
--------------------
For the given user, for his role
- Find all the subordinate roleid, from the role hierarchy map
- Add all the roleid to the queue
Repeat, till the queue is empty,
For each role present in the queue, find the subordinates roleid and add them to the queue and process list
While adding check if that role is already processed earlier
Processed roles are kept in Set processed or not)
end
For all the subordinateid roles from the processlist sets
Find the user for the role from the userlist and print them
end
Runtime complexity
-------------------
Indexing: O(n), n-> number of users and roles
Subordinate calculaton: O(1), Amortized value, constant time, as the data is parsed from the index (ideally total users that are matching those subordinates)
To Run the pre-built image, the image can be downloaded from the following releases link
- Download the release corresponding to your Operating System architecture and run the command
The files need to be copied to the working directory
https://github.com/techievee/deputy/releases/tag/v1.0.0
There are multiple ways to build an image
- custom build
git clone https://github.com/techievee/deputy.git
cd deputy
go build -trimpath -ldflags "-X main.Build=v1.0.0" -o ./deputy ./cmd
- using makefile (linux and mac)
git clone https://github.com/techievee/deputy.git
cd deputy
make all
make bin-darwin
make bin-windows
The program consists of the following components
console
: Helps in invoking the program and to run in either interactive or non-interactive CLI modecmd
: Command processor that helps in executing commandsindexer
: Index processor, which stores and process index for various file such as roles and users. Helps in finding the subordinates through index in constant timedata
: Data store that reads and process the json files from disk to memory structures.indexedData
: Data structure that stores the index as a inverted index.sets
: Data structure that stores and processes sets.utilities
: Helper functions.
./deputy --help
deputy is a CLI library that indexes and search json file for finding all the subordinates for the user.
This application is a tool that reads the csv files, indexes it and
calculates the total licences required for the application.
Usage:
deputy [flags]
deputy [command]
Available Commands:
help Help about any command
subordinates Deputy CLI Application to Index and Search Subordinates
Flags:
--config string config file (default is $HOME/deputy.yaml)
-h, --help help for deputy
-R, --role string Path of the JSON file to load Roles, Defaults to currentPath/roles.json
-U, --user string Path of the JSON file to load Users, Defaults to currentPath/users.json
./deputy subordinates --help
Deputy subordinates is a CLI library that indexes and search JSON file.
This application is a tool that reads the json file, indexes it and
searches for subordinates without any user interactions.
Usage:
deputy subordinates [flags]
Flags:
-h, --help help for subordinates
-u, --user-id string ID of the user, for whom the subordinates need to be calculated
Global Flags:
--config string config file (default is $HOME/deputy.yaml)
-R, --roles string Path of the JSON file to load Roles, Defaults to currentPath/roles.json
-U, --users string Path of the JSON file to load Users, Defaults to currentPath/users.json
-
Running the
./deputy
command without any verb will invoke the Interactive CLI, the roles and the users input files need to be present in the directory where the command is present or need to override using- -R or --role flags or DEPUTY_ROLE environment variable for roles
- -U or --user flags or DEPUTY_USER environment variable for users
Invoking interactive command without any CLI flags
Invoking interactive command using the short flags
Invoking interactive command using the full flags
Invoking interactive command after setting environment variable
-
Enter the user id for whom the subordinates need to be calculated
While entering the user id string, it gets validated to passthrough the subordinates command, the following rules apply
-
If enter is selected without any value it is considered as empty search, and it won't be validated
-
If alphanumeric or text is entered for field, it won't allow to passthroughs, until the input is corrected
- Running the
./deputy subordinates -u 1
command (deputy with additional subordinates verb) will invoke the Non-interactive CLI for one time run or calculation, the users and roles input files need to be present in the directory where the command is present or need to override using- -R or --role flags or DEPUTY_ROLE environment variable for roles
- -U or --user flags or DEPUTY_USER environment variable for users
- -u or --user-id flags or DEPUTY_USER_ID environment variable for usersId
Invoking non-interactive command with full flag name
Invoking non-interactive command using the short flags
-
Each file is considered as a valid json file containing the array of json data, if data is noisy the parsing is halted
-
There can be a user without corresponding roles and roles without user, that doesn't impact loading and indexing
-
If the file contains noisy data such as string for integer field are considered as invalid
-
The uniqueness for roles and user were not considered, when multiple user or roles with same id when present in the file, the data that was present first were considered
-
When the parent is not present for the roles, they are considered as root or parent=0, and the data is valid
{ "Id": 1, "Name": "System Administrator" }
-
When the other tags such as Id and Name for roles and all fields in the users when missing from the file, the file is considered as invalid
cobra
- [CLI Engine] ([https://github.com/spf13/cobra])(https://github.com/spf13/cobra)promptui
: [CLI Prompts] ([https://github.com/manifoldco/promptui.git])(https://github.com/manifoldco/promptui)viper
: [configuration manager] ([https://github.com/spf13/viper])(https://github.com/spf13/viper)