Skip to content

Commit 03ba138

Browse files
author
Simon Rivera
committed
creation of the repository and creation of the basic operation of the software
0 parents  commit 03ba138

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vim/
2+
venv/
3+
__pycache__/
4+
data/
5+
scripts/

PSExec.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import json_manager
2+
import click
3+
4+
@click.group()
5+
def PSExec():
6+
pass
7+
8+
@PSExec.command()
9+
@click.option('--name','-n',required=True,help='Name of the script')
10+
@click.option('--desc','-d',required=True,help='Description of the script')
11+
@click.option('--path','-p',required=True,help='Path of the script')
12+
@click.option('--module','-m',required=True,help='Module of the script')
13+
@click.option('--tags','-t',required=True,multiple=True,help='Tags of the script')
14+
@click.pass_context
15+
def new(ctx,name,desc,path,module,tags):
16+
if not name or not desc or not path or not module or not tags:
17+
ctx.fail('The all fields are required.')
18+
else:
19+
data = json_manager.read_json()
20+
newId = len(data) + 1
21+
22+
folder = json_manager.create_folder('./scripts')
23+
24+
if not folder:
25+
print(f"The folder scripts already exists.")
26+
27+
moveScript = json_manager.move_file(path,'./scripts')
28+
29+
if not moveScript:
30+
ctx.fail('Could not be moved the file to the folder scripts.')
31+
32+
newScript = {
33+
'id': newId,
34+
'name': name,
35+
'description': desc,
36+
'path': moveScript,
37+
'module': module,
38+
'tags': tags,
39+
}
40+
data.append(newScript)
41+
json_manager.write_json(data)
42+
print(f"The Script - {name} created successfully with id {newId}")
43+
44+
@PSExec.command()
45+
def scripts():
46+
data = json_manager.read_json()
47+
48+
if len(data) == 0:
49+
print("Don't exist scripts, please add scripts for use.")
50+
else:
51+
print("NAME".ljust(20),"DESC".ljust(len(data[0]['description'])),"MODULE".ljust(20), "TAGS".ljust(20))
52+
for script in data:
53+
tagg = ''
54+
for tag in script['tags']:
55+
tagg += '[' + tag + ']'
56+
57+
print(script['name'].ljust(20),script['description'].ljust(20),script['module'].ljust(20),tagg.ljust(20))
58+
59+
@PSExec.command()
60+
@click.option('--name','-n',required=True,help='Name of the script')
61+
@click.pass_context
62+
def exec(ctx,name):
63+
execFile = json_manager.exec_file(name)
64+
if execFile == 0:
65+
print(f"The {name} script has been executed successfully.")
66+
else:
67+
print(f"The {name} script has not been executed correctly.")
68+
69+
70+
if __name__ == '__main__':
71+
PSExec()

json_manager.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import os
3+
4+
def read_json():
5+
if not os.path.isfile('data/data.json'):
6+
with open('data/data.json', 'w') as f:
7+
json.dump([],f)
8+
with open('data/data.json', 'r') as f:
9+
data = json.load(f)
10+
return data
11+
12+
def write_json(data):
13+
with open('data/data.json','w') as f:
14+
json.dump(data, f)
15+
16+
def create_folder(path):
17+
try:
18+
os.mkdir(path)
19+
return True
20+
except FileExistsError:
21+
return False
22+
23+
def move_file(oldPath,newPath):
24+
try:
25+
file = os.path.basename(oldPath)
26+
newPath = newPath + '/' + file
27+
os.replace(oldPath,newPath)
28+
return newPath
29+
except:
30+
return False
31+
32+
def exec_file(name):
33+
data = read_json()
34+
script = next((x for x in data if x['name'] == name), None)
35+
if script is None:
36+
return 'Script does not exist'
37+
else:
38+
return os.system(f"pwsh {script['path']}")

readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PSExec
2+
3+
## Getting Started
4+
5+
This project is for easy and fast PowerShell script management.
6+
7+
### Content

0 commit comments

Comments
 (0)