shell 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

shell #

Pub

Wrapper over dart:io [Process] API's that supports features like environment management, user switches, and more.

Useful for writing shell utility scripts in Dart, or within applications that perform system administration tasks on behalf of other users.

import 'dart:io';
import 'package:file/local.dart';
import 'package:shell/shell.dart';

main() async {
  var fs = const LocalFileSystem();
  var shell = new Shell();
  var password = Platform.environment['PASSWORD'];
  print('Password: $password');

  // Pipe results to files, easily.
  var echo = await shell.start('echo', ['hello world']);
  await echo.stdout.writeToFile(fs.file('hello.txt'));
  await echo.stderr.drain();

  // You can run a program, and expect a certain exit code.
  //
  // If a valid exit code is returned, stderr is drained, and
  // you don't have to manually.
  //
  // Otherwise, a StateError is thrown.
  var find = await shell.start('find', ['.']);
  await find.expectExitCode([0]); // Can also call find.expectZeroExit()

  // Dump outputs.
  print(await find.stdout.readAsString());

  // You can also run a process and immediately receive a string.
  var pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // Navigation allows you to `cd`.
  shell.navigate('./lib/src');
  pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // We can make a separate shell, with the same settings.
  var forked = new Shell.copy(shell)
    ..sudo = true
    ..password = password;

  // Say hi, as an admin!
  var superEcho = await forked.start('echo', ['hello, admin!']);
  await superEcho.expectExitCode([0, 1]);
  await superEcho.stdout.writeToFile(fs.file('hello_sudo.txt'));
}

1.0.1 #

  • Add runInShell attribute for Shell class

1.0.0 #

  • First version. 🎯

example/main.dart

import 'dart:io';
import 'package:file/local.dart';
import 'package:shell/shell.dart';

main() async {
  var fs = const LocalFileSystem();
  var shell = new Shell();
  var password = Platform.environment['PASSWORD'];
  print('Password: $password');

  // Pipe results to files, easily.
  var echo = await shell.start('echo', ['hello world']);
  await echo.stdout.writeToFile(fs.file('hello.txt'));
  await echo.stderr.drain();

  // You can run a program, and expect a certain exit code.
  //
  // If a valid exit code is returned, stderr is drained, and
  // you don't have to manually.
  //
  // Otherwise, a StateError is thrown.
  var find = await shell.start('find', ['.']);
  await find.expectExitCode([0]); // Can also call find.expectZeroExit()

  // Dump outputs.
  print(await find.stdout.readAsString());

  // You can also run a process and immediately receive a string.
  var pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // Navigation allows you to `cd`.
  shell.navigate('./lib/src');
  pwd = await shell.startAndReadAsString('pwd', []);
  print('cwd: $pwd');

  // We can make a separate shell, with the same settings.
  var forked = new Shell.copy(shell)
    ..sudo = true
    ..password = password;

  // Say hi, as an admin!
  var superEcho = await forked.start('echo', ['hello, admin!']);
  await superEcho.expectExitCode([0, 1]);
  await superEcho.stdout.writeToFile(fs.file('hello_sudo.txt'));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  shell: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:shell/shell.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
56
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:shell/shell.dart with components: io.

Health suggestions

Fix lib/src/shell.dart. (-4.41 points)

Analysis of lib/src/shell.dart reported 9 hints, including:

line 18 col 27: Use = to separate a named parameter from its default value.

line 19 col 36: Use = to separate a named parameter from its default value.

line 21 col 16: Use = to separate a named parameter from its default value.

line 22 col 22: Use = to separate a named parameter from its default value.

line 25 col 38: Use = to separate a named parameter from its default value.

Fix lib/src/wrapped_process.dart. (-1.49 points)

Analysis of lib/src/wrapped_process.dart reported 3 hints:

line 28 col 7: DO use curly braces for all flow control structures.

line 31 col 7: DO use curly braces for all flow control structures.

line 61 col 49: Use = to separate a named parameter from its default value.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
file ^5.0.0 5.1.0
path ^1.0.0 1.6.4
platform ^2.0.0 2.2.1
process ^3.0.0 3.0.12
Transitive dependencies
intl 0.16.0
meta 1.1.7