Browse Source

sketch out some basic commands

Brennen Bearnes 6 months ago
commit
dc94b763e8
13 changed files with 297 additions and 0 deletions
  1. 8
    0
      .gitignore
  2. 24
    0
      Build.PL
  3. 26
    0
      bin/mark
  4. 44
    0
      bin/mark-add
  5. 9
    0
      bin/mark-clear
  6. 24
    0
      bin/mark-cp
  7. 0
    0
      bin/mark-each
  8. 12
    0
      bin/mark-ls
  9. 12
    0
      bin/mark-ls-nullsep
  10. 29
    0
      bin/mark-mv
  11. 0
    0
      bin/mark-rm
  12. 109
    0
      lib/App/MarkFiles.pm
  13. 0
    0
      t/.gitkeep

+ 8
- 0
.gitignore View File

@@ -0,0 +1,8 @@
1
+blib
2
+_build
3
+Build
4
+MANIFEST
5
+MANIFEST.bak
6
+MANIFEST.SKIP.bak
7
+MYMETA.json
8
+MYMETA.yml

+ 24
- 0
Build.PL View File

@@ -0,0 +1,24 @@
1
+use Module::Build;
2
+
3
+my $build = Module::Build->new(
4
+
5
+  module_name => 'App::MarkFiles',
6
+
7
+  license  => 'gpl',
8
+
9
+  requires => {
10
+    'Getopt::Long'  => 0,
11
+    'POSIX'         => 0,
12
+    'Cwd'           => 0,
13
+    'DBI'           => 0,
14
+    'DBD::SQLite'   => 0,
15
+    'Sys::Hostname' => 0,
16
+    'perl'          => '5.10.0',
17
+  },
18
+
19
+  recommends => {
20
+  },
21
+
22
+);
23
+
24
+$build->create_build_script;

+ 26
- 0
bin/mark View File

@@ -0,0 +1,26 @@
1
+#!/bin/sh
2
+
3
+print_help() {
4
+  echo "$0 - mark and operate on files"
5
+  echo
6
+  echo "Usage: mark [command] [args]"
7
+  echo "    mark -h          Print this help message"
8
+  echo
9
+  echo "You must specify a command."
10
+  exit 1
11
+}
12
+
13
+if [ $# -lt 1 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
14
+  print_help
15
+fi
16
+
17
+subprog="mark-$1"
18
+
19
+# Make sure that the command we've been given exists:
20
+command -v "$subprog" >/dev/null 2>&1 || {
21
+  echo "mark: '$1' is not a mark command.  See 'mark -h'."
22
+  exit 1
23
+}
24
+
25
+shift
26
+exec "$subprog" "$@"

+ 44
- 0
bin/mark-add View File

@@ -0,0 +1,44 @@
1
+#!/usr/bin/env perl
2
+
3
+=pod
4
+
5
+=head1 NAME
6
+
7
+mark-add - add files to the current set of marked files
8
+
9
+=head1 SYNOPSIS
10
+
11
+  mark-add:  stashes a filesystem path for use with other utilities
12
+
13
+  USAGE:
14
+    mark add path_to_mark
15
+
16
+  EXAMPLE:
17
+    mark add random.txt
18
+    cd ~/notes
19
+    mark mv
20
+
21
+=head1 AUTHOR
22
+
23
+Brennen Bearnes <bbearnes@gmail.com>
24
+
25
+=cut
26
+
27
+use warnings;
28
+use strict;
29
+use 5.10.0;
30
+
31
+use App::MarkFiles;
32
+use Cwd qw(cwd abs_path);
33
+use Sys::Hostname;
34
+
35
+my $dbh = App::MarkFiles::get_dbh();
36
+
37
+my $sth = $dbh->prepare(q{
38
+  INSERT INTO marks (path, datetime) VALUES (?, datetime('now'))
39
+});
40
+
41
+foreach my $arg (@ARGV) {
42
+  my $path = abs_path($arg);
43
+  $sth->execute($path);
44
+}

+ 9
- 0
bin/mark-clear View File

@@ -0,0 +1,9 @@
1
+#!/usr/bin/env perl
2
+
3
+use warnings;
4
+use strict;
5
+use 5.10.0;
6
+
7
+use App::MarkFiles;
8
+
9
+App::MarkFiles::get_dbh()->prepare(q{DELETE FROM marks})->execute();

+ 24
- 0
bin/mark-cp View File

@@ -0,0 +1,24 @@
1
+#!/usr/bin/env perl
2
+
3
+use warnings;
4
+use strict;
5
+use 5.10.0;
6
+
7
+use App::MarkFiles;
8
+use File::Copy;
9
+
10
+App::MarkFiles::each(sub {
11
+  my ($mark) = @_;
12
+  my ($path) = $mark->{path};
13
+
14
+  unless (-e $path) {
15
+    say "No such file: $path";
16
+    return;
17
+  }
18
+
19
+  if (copy($path, './')) {
20
+    say "Copied: $path";
21
+  } else {
22
+    say "Copy failed: $!"
23
+  }
24
+});

+ 0
- 0
bin/mark-each View File


+ 12
- 0
bin/mark-ls View File

@@ -0,0 +1,12 @@
1
+#!/usr/bin/env perl
2
+
3
+use strict;
4
+use warnings;
5
+use 5.10.0;
6
+
7
+use App::MarkFiles;
8
+
9
+App::MarkFiles::each(sub {
10
+  my ($data) = @_;
11
+  say $data->{path};
12
+});

+ 12
- 0
bin/mark-ls-nullsep View File

@@ -0,0 +1,12 @@
1
+#!/usr/bin/env perl
2
+
3
+use strict;
4
+use warnings;
5
+use 5.10.0;
6
+
7
+use App::MarkFiles;
8
+
9
+App::MarkFiles::each(sub {
10
+  my ($data) = @_;
11
+  print $data->{path} . "\0";
12
+});

+ 29
- 0
bin/mark-mv View File

@@ -0,0 +1,29 @@
1
+#!/usr/bin/env perl
2
+
3
+use warnings;
4
+use strict;
5
+use 5.10.0;
6
+
7
+use App::MarkFiles;
8
+use File::Copy;
9
+
10
+my @unmark;
11
+
12
+App::MarkFiles::each(sub {
13
+  my ($mark) = @_;
14
+  my $path = $mark->{path};
15
+
16
+  unless (-e $path) {
17
+    say "No such file: " . $path;
18
+    return;
19
+  }
20
+
21
+  if (move($path, './')) {
22
+    say "Moved: $path";
23
+    push @unmark, $path;
24
+  } else {
25
+    say "Move failed: $!"
26
+  }
27
+});
28
+
29
+App::MarkFiles::unmark(@unmark);

+ 0
- 0
bin/mark-rm View File


+ 109
- 0
lib/App/MarkFiles.pm View File

@@ -0,0 +1,109 @@
1
+=pod
2
+
3
+=head1 NAME
4
+
5
+App::MarkFiles - Tools for logging command line history
6
+
7
+=head1 SYNOPSIS
8
+
9
+tk tk tk
10
+
11
+=head1 INSTALLING
12
+
13
+    $ perl Build.PL
14
+    $ ./Build
15
+    $ ./Build install
16
+
17
+=cut
18
+
19
+package App::MarkFiles;
20
+
21
+our ($VERSION) = '0.0.1';
22
+
23
+use strict;
24
+use warnings;
25
+
26
+use DBI;
27
+
28
+=over
29
+
30
+=item get_dbh()
31
+
32
+Get database handle for default commandlog database.
33
+
34
+=cut
35
+
36
+sub get_dbh {
37
+  my $dbfile = $ENV{HOME} . "/marks.db";
38
+
39
+  my $init_new = 0;
40
+  $init_new = 1 unless -f $dbfile;
41
+
42
+  my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
43
+
44
+  # Make a new database and table(s), if needed:
45
+  create_mark_db($dbh) if $init_new;
46
+
47
+  return $dbh;
48
+}
49
+
50
+sub create_mark_db {
51
+  my ($dbh) = @_;
52
+  $dbh->do(<<'SQL'
53
+    CREATE TABLE marks (
54
+      id integer primary key,
55
+      path text,
56
+      datetime text
57
+    );
58
+SQL
59
+  );
60
+}
61
+
62
+sub each {
63
+  my ($func) = @_;
64
+
65
+  my ($dbh) = get_dbh();
66
+
67
+  my $sth = $dbh->prepare(q{
68
+    SELECT * FROM marks ORDER BY datetime;
69
+  });
70
+
71
+  $sth->execute();
72
+
73
+  while (my $data = $sth->fetchrow_hashref()) {
74
+    $func->($data);
75
+  }
76
+}
77
+
78
+sub unmark {
79
+  my ($dbh) = get_dbh();
80
+  my (@paths) = @_;
81
+
82
+  my $sth = $dbh->prepare(q{
83
+    DELETE FROM marks WHERE PATH = ?;
84
+  });
85
+
86
+  foreach my $path (@paths) {
87
+    $sth->execute($path);
88
+  }
89
+}
90
+
91
+=back
92
+
93
+=head1 AUTHOR
94
+
95
+Copyright 2018 Brennen Bearnes
96
+
97
+    commandlog is free software; you can redistribute it and/or modify
98
+    it under the terms of the GNU General Public License as published by
99
+    the Free Software Foundation; either version 2 of the License, or
100
+    (at your option) any later version.
101
+
102
+    This program is distributed in the hope that it will be useful,
103
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
104
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
105
+    GNU General Public License for more details.
106
+
107
+=cut
108
+
109
+1;

+ 0
- 0
t/.gitkeep View File