Skip to content

Commit

Permalink
released 0.29.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bsutton committed Sep 23, 2020
1 parent fd97bcf commit 8dafa1c
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 124 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# 0.29.0
breaking change: PubSpec now returns a map for dependencies and dependency overrides rather than a list.
renamed the default.dart template to basic.dart as default is a dart keyword and was causing conflicts in the asset generation.
optimised the unit test by using a shared file system.
Fixed a bug in the unit test runner as we need to run with a bash shell rather than sh as a number of unit tests expect to find bash.
fixed a unit test as dependencies have changed fro ma list to a map.
change from env to loggedInUser as this will be more cross platform.
reverted logic that had the script being run from the script directory as it doesn't allow people to select the directory they want to run the script in.
removed references to syslog as it doesn't exists in the docker test environment.
updated name in pubspect to match directory.
added version no. to the install started message.
changed to using the newist patToProjectRoot
copyTree: Changed the default for the [recursive] argument. It is now true by default (it recurses). The command is copyTree which implies its recursive.
renamed directory test_scripts to test_script in line with dart conventions.
Fix: create was ignorning the passed templatename.
fixed bug as clean now works on projects not scripts.
added test for warmup.
removed the --package switch as we no longer need it as we have removed virtual projects.
re-implemented touch using native dart libs.
install: fixed bug where we were trying to use chmod to change ownership :<

# 0.28.0
This release includes significant changes to the dcli command line.
The command 'clean' has been renamed to warmup.
Expand Down
96 changes: 78 additions & 18 deletions lib/src/templates/expander.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,65 @@ Eventually you will be able to run
dcli create --template cli_args.dart snake.dart''');
}

void cmd_args() {
var expandTo = join(targetPath, 'cmd_args.dart');
expandTo.write(r'''#! /usr/bin/env %dcliName%
import 'dart:io';
import 'package:dcli/dcli.dart';
/// dcli script generated by:
/// dcli create %scriptname%
///
/// See
/// https://pub.dev/packages/dcli#-installing-tab-
///
/// For details on installing dcli.
///
void main(List<String> args) {
var parser = ArgParser();
parser.addFlag(
'verbose',
abbr: 'v',
negatable: false,
defaultsTo: false,
help: 'Logs additional details to the cli',
);
parser.addOption('prompt', abbr: 'p', help: 'The prompt to show the user.');
var parsed = parser.parse(args);
if (parsed.wasParsed('verbose')) {
Settings().setVerbose(enabled: true);
}
if (!parsed.wasParsed('prompt')) {
printerr(red('You must pass a prompt'));
showUsage(parser);
}
var prompt = parsed['prompt'] as String;
var valid = false;
String response;
do {
response = ask('$prompt:', validator: Ask.all([Ask.alpha, Ask.required]));
valid = confirm('Is this your response? ${green(response)}');
} while (!valid);
print(orange('Your response was: $response'));
}
void showUsage(ArgParser parser) {
print('Usage: %scriptname% -v -prompt <a questions>');
print(parser.usage);
exit(1);
}''');
}

void analysis_options() {
var expandTo = join(targetPath, 'analysis_options.yaml');
expandTo.write(r'''
Expand Down Expand Up @@ -60,23 +119,6 @@ void main() {
}''');
}

void pubspec() {
var expandTo = join(targetPath, 'pubspec.yaml.template');
expandTo.write(r'''name: %scriptname%
version: 0.0.1
description: A script generated by dcli.
environment:
sdk: '>=2.6.0 <3.0.0'
dependencies:
args: ^1.0.0
dcli: ^0.25.0
path: ^1.0.0
dev_dependencies:
pedantic: ^1.0.0
''');
}

void basic() {
var expandTo = join(targetPath, 'basic.dart');
expandTo.write(r'''#! /usr/bin/env %dcliName%
Expand Down Expand Up @@ -136,11 +178,29 @@ void showUsage(ArgParser parser) {
}''');
}

void pubspec() {
var expandTo = join(targetPath, 'pubspec.yaml.template');
expandTo.write(r'''name: %scriptname%
version: 0.0.1
description: A script generated by dcli.
environment:
sdk: '>=2.6.0 <3.0.0'
dependencies:
args: ^1.0.0
dcli: ^0.29.0
path: ^1.0.0
dev_dependencies:
pedantic: ^1.0.0
''');
}

void expand() {
README();
cmd_args();
analysis_options();
hello_world();
pubspec();
basic();
pubspec();
}
}
2 changes: 1 addition & 1 deletion lib/src/version/version.g.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// GENERATED BY pub_release do not modify.
/// dcli version
String packageVersion = '0.28.0';
String packageVersion = '0.29.0';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dcli
version: 0.28.0
version: 0.29.0
homepage: https://github.com/bsutton/dcli
description: An API and dev environment for writing console (cli) apps/scripts using dart. e.g. allows you to build bash style scripts with dart.
environment:
Expand Down
1 change: 0 additions & 1 deletion test/priviliged_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

void main() {
/// reduce the scripts privileges
privileged(false);
Expand Down
206 changes: 103 additions & 103 deletions test/src/util/permissions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef getRealUID = int Function();

void main() {
var priv = Priviliges();
priv.descalate();
// priv.descalate();
// priv.escalate();
// priv.descalate();

Expand Down Expand Up @@ -71,7 +71,7 @@ void main() {
}

class Priviliges {
static Priviliges _self = Priviliges._internal();
static final Priviliges _self = Priviliges._internal();

DynamicLibrary dylib;

Expand All @@ -94,25 +94,25 @@ class Priviliges {
factory Priviliges() => _self;

Priviliges._internal() {
var path = 'libc.so.6';
if (Platform.isMacOS) path = '/usr/lib/libSystem.dylib';
if (Platform.isWindows) path = r'primitives_library\Debug\primitives.dll';
dylib = DynamicLibrary.open(path);

realUID = _realUID;

var sudo_uid = env['SUDO_UID'];
if (sudo_uid != null) {
sudo = true;
userUID = int.tryParse(sudo_uid);
} else {
/// we aren't running sudo
sudo = false;
userUID = realUID;
}

originalEffectiveUID = _effectiveUID;
currentEffectiveUID = originalEffectiveUID;
// var path = 'libc.so.6';
// if (Platform.isMacOS) path = '/usr/lib/libSystem.dylib';
// if (Platform.isWindows) path = r'primitives_library\Debug\primitives.dll';
// dylib = DynamicLibrary.open(path);

// realUID = _realUID;

// var sudo_uid = env['SUDO_UID'];
// if (sudo_uid != null) {
// sudo = true;
// userUID = int.tryParse(sudo_uid);
// } else {
// /// we aren't running sudo
// sudo = false;
// userUID = realUID;
// }

// originalEffectiveUID = _effectiveUID;
// currentEffectiveUID = originalEffectiveUID;
}

/// If the script was started as sudo then any [task]
Expand All @@ -121,88 +121,88 @@ class Priviliges {
///
/// If the script wasn't run as sudo then there will
/// be no changes to their privildege level.
void withPrivileges(void Function() task) {
escalate();
task();
descalate();
}

int get effectiveUID => currentEffectiveUID;

void escalate() {
if (sudo && currentEffectiveUID == userUID) {
print(green('escalate'));
_effectiveUID = originalEffectiveUID;
currentEffectiveUID = originalEffectiveUID;
// _realUID = originalEffectiveUID;
}
}

void descalate() {
/// if the real and original is the same then they
/// mustn't be running under sudo so we can't help them.
if (sudo && currentEffectiveUID != userUID) {
print(red('descalate'));
_effectiveUID = userUID;
// _realUID = userUID;
currentEffectiveUID = userUID;
}
}

int get _realUID {
final getuidPointer =
dylib.lookup<NativeFunction<getRealUID_func>>('getuid');
final getuid = getuidPointer.asFunction<getRealUID>();

int uid = getuid();
print('get real guid=$uid');
return uid;
}

set _realUID(int realUID) {
final setuidPointer = dylib.lookup<NativeFunction<setUID_func>>('setreuid');
final setuid = setuidPointer.asFunction<setUID>();

print(blue('settting realUID =$realUID'));
var result = setuid(realUID, -1);
if (result != 0) {
throw PriviligesException('Unable to set the Effective UID: $result');
}
}

int get _effectiveUID {
final geteuidPointer =
dylib.lookup<NativeFunction<getEffectiveUID_func>>('geteuid');
final geteuid = geteuidPointer.asFunction<getEffectiveUID>();
var uid = geteuid();

print('get effiective guid=$uid');
return uid;
}

set _effectiveUID(int effectiveUID) {
print('setting effective to $effectiveUID');
var result = -1;
try {
final seteuidPointer =
dylib.lookup<NativeFunction<setEffectiveUID_func>>('seteuid');
final seteuid = seteuidPointer.asFunction<setEffectiveUID>();

print(blue('settting effectiveUID =$effectiveUID'));
result = seteuid(effectiveUID);
} on ArgumentError catch (_) {
// seteuid isn't available so lets try setreuid

final setreuidPointer =
dylib.lookup<NativeFunction<setUID_func>>('setreuid');
final setreuid = setreuidPointer.asFunction<setUID>();
result = setreuid(-1, effectiveUID);
}

if (result != 0) {
throw PriviligesException('Unable to set the Effective UID: $result');
}
}
// void withPrivileges(void Function() task) {
// escalate();
// task();
// descalate();
// }

// int get effectiveUID => currentEffectiveUID;

// void escalate() {
// if (sudo && currentEffectiveUID == userUID) {
// print(green('escalate'));
// _effectiveUID = originalEffectiveUID;
// currentEffectiveUID = originalEffectiveUID;
// // _realUID = originalEffectiveUID;
// }
// }

// void descalate() {
// /// if the real and original is the same then they
// /// mustn't be running under sudo so we can't help them.
// if (sudo && currentEffectiveUID != userUID) {
// print(red('descalate'));
// _effectiveUID = userUID;
// // _realUID = userUID;
// currentEffectiveUID = userUID;
// }
// }

// int get _realUID {
// final getuidPointer =
// dylib.lookup<NativeFunction<getRealUID_func>>('getuid');
// final getuid = getuidPointer.asFunction<getRealUID>();

// int uid = getuid();
// print('get real guid=$uid');
// return uid;
// }

// set _realUID(int realUID) {
// final setuidPointer = dylib.lookup<NativeFunction<setUID_func>>('setreuid');
// final setuid = setuidPointer.asFunction<setUID>();

// print(blue('settting realUID =$realUID'));
// var result = setuid(realUID, -1);
// if (result != 0) {
// throw PriviligesException('Unable to set the Effective UID: $result');
// }
// }

// int get _effectiveUID {
// final geteuidPointer =
// dylib.lookup<NativeFunction<getEffectiveUID_func>>('geteuid');
// final geteuid = geteuidPointer.asFunction<getEffectiveUID>();
// var uid = geteuid();

// print('get effiective guid=$uid');
// return uid;
// }

// set _effectiveUID(int effectiveUID) {
// print('setting effective to $effectiveUID');
// var result = -1;
// try {
// final seteuidPointer =
// dylib.lookup<NativeFunction<setEffectiveUID_func>>('seteuid');
// final seteuid = seteuidPointer.asFunction<setEffectiveUID>();

// print(blue('settting effectiveUID =$effectiveUID'));
// result = seteuid(effectiveUID);
// } on ArgumentError catch (_) {
// // seteuid isn't available so lets try setreuid

// final setreuidPointer =
// dylib.lookup<NativeFunction<setUID_func>>('setreuid');
// final setreuid = setreuidPointer.asFunction<setUID>();
// result = setreuid(-1, effectiveUID);
// }

// if (result != 0) {
// throw PriviligesException('Unable to set the Effective UID: $result');
// }
// }
}

class PriviligesException extends DCliException {
Expand Down

0 comments on commit 8dafa1c

Please sign in to comment.