tagm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +30 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +43 -0
- data/CODE_OF_CONDUCT.md +134 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +12 -0
- data/exe/tagm +8 -0
- data/lib/tagm/cli.rb +77 -0
- data/lib/tagm/config.rb +19 -0
- data/lib/tagm/db.rb +201 -0
- data/lib/tagm/version.rb +5 -0
- data/lib/tagm.rb +10 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e77dba199d42762e97c564b9c38ef12da589bcb736bef951facad1649ea212e
|
4
|
+
data.tar.gz: d8bc453571324083c037ec58ade5d676d2ecd9e046c93b27a9e4d3e4a7e985b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3284fba542988028f64475b2bbc9b12dee0c679ab8a0069d2a4c1e3ae737a5c21ddef28b0a2165d8897c9b0bf75fc75695a3a418dce14d3db18f24be989eb344
|
7
|
+
data.tar.gz: f75b6664a6495620cb38cfa490e7035bc267c29129e8a12799735cd267392c2fd7fa20f37d4d81e2311104f6e4b58bf8af2a28ac769cc49d8038cecd79db88c3
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
plugins:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rake
|
4
|
+
- rubocop-rspec
|
5
|
+
|
6
|
+
AllCops:
|
7
|
+
TargetRubyVersion: 3.1
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Layout/HashAlignment:
|
11
|
+
EnforcedColonStyle: table
|
12
|
+
EnforcedHashRocketStyle: table
|
13
|
+
|
14
|
+
Layout/LineLength:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/Documentation:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/HashSyntax:
|
21
|
+
EnforcedShorthandSyntax: never
|
22
|
+
|
23
|
+
Style/SymbolArray:
|
24
|
+
EnforcedStyle: brackets
|
25
|
+
|
26
|
+
Style/TrailingCommaInArrayLiteral:
|
27
|
+
EnforcedStyleForMultiline: comma
|
28
|
+
|
29
|
+
Style/TrailingCommaInHashLiteral:
|
30
|
+
EnforcedStyleForMultiline: comma
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
<!--
|
9
|
+
Quick remainder of the possible sections:
|
10
|
+
-----------------------------------------
|
11
|
+
### Added
|
12
|
+
for new features.
|
13
|
+
### Changed
|
14
|
+
for changes in existing functionality.
|
15
|
+
### Deprecated
|
16
|
+
for soon-to-be removed features.
|
17
|
+
### Removed
|
18
|
+
for now removed features.
|
19
|
+
### Fixed
|
20
|
+
for any bug fixes.
|
21
|
+
### Security
|
22
|
+
in case of vulnerabilities.
|
23
|
+
### Maintenance
|
24
|
+
in case of rework, dependencies change
|
25
|
+
|
26
|
+
Please, keep them in this order when updating.
|
27
|
+
|
28
|
+
Breaking changes should be prefixed by `[**BREAKING**]` (without the quotes), to stand out.
|
29
|
+
-->
|
30
|
+
|
31
|
+
## [Unreleased]
|
32
|
+
|
33
|
+
## [0.1.0] 2022/03/07
|
34
|
+
|
35
|
+
### Added
|
36
|
+
|
37
|
+
Base features; be able to:
|
38
|
+
|
39
|
+
- add tags on nodes
|
40
|
+
- remove tags
|
41
|
+
- show tags for a node
|
42
|
+
- show nodes for one or more tags
|
43
|
+
- merge tags
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
|
64
|
+
contact-project+experimentslabs-tagm-67777926-issue-@incoming.gitlab.com
|
65
|
+
|
66
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
67
|
+
|
68
|
+
All community leaders are obligated to respect the privacy and security of the
|
69
|
+
reporter of any incident.
|
70
|
+
|
71
|
+
## Enforcement Guidelines
|
72
|
+
|
73
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
74
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
75
|
+
|
76
|
+
### 1. Correction
|
77
|
+
|
78
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
79
|
+
unprofessional or unwelcome in the community.
|
80
|
+
|
81
|
+
**Consequence**: A private, written warning from community leaders, providing
|
82
|
+
clarity around the nature of the violation and an explanation of why the
|
83
|
+
behavior was inappropriate. A public apology may be requested.
|
84
|
+
|
85
|
+
### 2. Warning
|
86
|
+
|
87
|
+
**Community Impact**: A violation through a single incident or series of
|
88
|
+
actions.
|
89
|
+
|
90
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
91
|
+
interaction with the people involved, including unsolicited interaction with
|
92
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
93
|
+
includes avoiding interactions in community spaces as well as external channels
|
94
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
95
|
+
ban.
|
96
|
+
|
97
|
+
### 3. Temporary Ban
|
98
|
+
|
99
|
+
**Community Impact**: A serious violation of community standards, including
|
100
|
+
sustained inappropriate behavior.
|
101
|
+
|
102
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
103
|
+
communication with the community for a specified period of time. No public or
|
104
|
+
private interaction with the people involved, including unsolicited interaction
|
105
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
106
|
+
Violating these terms may lead to a permanent ban.
|
107
|
+
|
108
|
+
### 4. Permanent Ban
|
109
|
+
|
110
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
111
|
+
standards, including sustained inappropriate behavior, harassment of an
|
112
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
113
|
+
|
114
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
115
|
+
community.
|
116
|
+
|
117
|
+
## Attribution
|
118
|
+
|
119
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
120
|
+
version 2.1, available at
|
121
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
122
|
+
|
123
|
+
Community Impact Guidelines were inspired by
|
124
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
125
|
+
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
127
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
128
|
+
[https://www.contributor-covenant.org/translations][translations].
|
129
|
+
|
130
|
+
[homepage]: https://www.contributor-covenant.org
|
131
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
132
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
133
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
134
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 manu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Tagm
|
2
|
+
|
3
|
+
Small tool to associate tags to files.
|
4
|
+
|
5
|
+
This gem was not tested on Windows; use at your own risks.
|
6
|
+
|
7
|
+
## Community
|
8
|
+
|
9
|
+
- [Matrix chatroom](https://matrix.to/#/#experimentslabs-general:matrix.org)
|
10
|
+
- [Issues](https://gitlab.com/experimentslabs/tagm/-/issues)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
```sh
|
15
|
+
gem install tagm
|
16
|
+
```
|
17
|
+
|
18
|
+
Tagm relies on the `TAGM_DB_PATH` environment variable, so you should export it from your .bashrc or similar file:
|
19
|
+
|
20
|
+
```sh
|
21
|
+
export TAGM_DB_PATH=$HOME/.config/tagm_database.sqlite3
|
22
|
+
```
|
23
|
+
|
24
|
+
Create the database:
|
25
|
+
|
26
|
+
```sh
|
27
|
+
tagm create_database
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
See all options and help on commands with:
|
33
|
+
|
34
|
+
```sh
|
35
|
+
# Commands list
|
36
|
+
tagm
|
37
|
+
|
38
|
+
# Help on a specific command
|
39
|
+
tagm help tag
|
40
|
+
```
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
45
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
48
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
49
|
+
push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on Gitlab at https://gitlab.com/experimentslabs/tagm. This project is intended
|
54
|
+
to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
|
55
|
+
the [code of conduct](https://gitlab.com/experimentslabs/tagm/-/blob/main/CODE_OF_CONDUCT.md).
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
60
|
+
|
61
|
+
## Code of Conduct
|
62
|
+
|
63
|
+
Everyone interacting in the Tagm project's codebases, issue trackers, chat rooms and mailing lists is expected to follow
|
64
|
+
the [code of conduct](https://gitlab.com/experimentslabs/tagm/-/main/CODE_OF_CONDUCT.md).
|
65
|
+
|
66
|
+
## Gem story
|
67
|
+
|
68
|
+
I was searching for a tool to manage files with tags instead of directories; and I then found [TMSU](https://tmsu.org/),
|
69
|
+
which provides taggings _and_ the ability to mount tags as directories with Fuse. Sadly, the project did not move since
|
70
|
+
11/2022.
|
71
|
+
|
72
|
+
So why not?
|
73
|
+
|
74
|
+
If you dig a bit in the code, you'll see we don't use any ORM for accessing SQLite database. This was primarily intended
|
75
|
+
as an exercice, as I have not written any SQL statement in ages. I'm OK to change this :)
|
data/Rakefile
ADDED
data/exe/tagm
ADDED
data/lib/tagm/cli.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'skittlize'
|
4
|
+
require 'thor'
|
5
|
+
|
6
|
+
require_relative 'config'
|
7
|
+
require_relative 'db'
|
8
|
+
|
9
|
+
module Tagm
|
10
|
+
class Cli < Thor
|
11
|
+
desc 'tag NODE', 'Adds tag to specified node, or to current directory when nothing specified'
|
12
|
+
option 'tags', aliases: ['t'], type: :array, desc: 'List of space-separated tags', required: true, default: []
|
13
|
+
def tag(node)
|
14
|
+
path = resolve_path node
|
15
|
+
Db.tag node: path, tags: options[:tags]
|
16
|
+
|
17
|
+
put_path_tags path, node
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'untag NODE', 'Removes one or many tags to specified node, or to current directory when nothing specified'
|
21
|
+
option 'tags', aliases: ['t'], type: :array, desc: 'List of space-separated tags', required: true, default: []
|
22
|
+
def untag(node)
|
23
|
+
path = resolve_path node
|
24
|
+
Db.untag node: path, tags: options[:tags]
|
25
|
+
|
26
|
+
put_path_tags path, node
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'on [NODE]', 'Displays tags associated to specified node. Displays all nodes if no node is provided'
|
30
|
+
def on(node = nil)
|
31
|
+
path = resolve_path(node)
|
32
|
+
|
33
|
+
put_path_tags path, node
|
34
|
+
end
|
35
|
+
|
36
|
+
desc 'tagged [TAGS]', 'Displays all registered paths matching the tags. Displays all nodes if no tag is provided'
|
37
|
+
def tagged(tags = [])
|
38
|
+
paths = Db.list(*tags)
|
39
|
+
paths.each_pair do |path, list|
|
40
|
+
puts "#{path}: #{colorize(list).join(', ')}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
desc 'merge', 'Merges one or many tags into another'
|
45
|
+
option 'from', aliases: ['-f'], type: :array, desc: 'List of space-separated tags', required: true, default: []
|
46
|
+
option 'into', aliases: ['-i'], type: :string, desc: 'Target tag', required: true
|
47
|
+
def merge
|
48
|
+
Db.merge(from: options[:from], into: options[:into])
|
49
|
+
puts "Merged \"#{options[:from].sort.join('", "')}\" into \"#{options[:into]}\""
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'create_db', "Creates database at \"$#{Config::DB_PATH_VAR}\""
|
53
|
+
def create_db
|
54
|
+
puts "Creating database at #{Config::DB_PATH_VAR}"
|
55
|
+
Tagm::Db.create_database!
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def resolve_path(path)
|
61
|
+
path = File.absolute_path(path || Dir.pwd)
|
62
|
+
|
63
|
+
raise 'File does not exist' unless File.exist? path
|
64
|
+
|
65
|
+
path
|
66
|
+
end
|
67
|
+
|
68
|
+
def put_path_tags(full_path, option_node)
|
69
|
+
tags = colorize(Db.show(full_path)).join(', ')
|
70
|
+
puts "#{option_node}: #{tags}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def colorize(strings)
|
74
|
+
strings.map(&:skittlize)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/tagm/config.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tagm
|
4
|
+
class Config
|
5
|
+
class MissingDbPathError < StandardError; end
|
6
|
+
|
7
|
+
DB_PATH_VAR = 'TAGM_DB_PATH'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def db_path
|
11
|
+
@db_path ||= ENV.fetch(DB_PATH_VAR, nil)
|
12
|
+
|
13
|
+
raise MissingDbPathError, "Missing #{DB_PATH_VAR} environment variable" unless @db_path
|
14
|
+
|
15
|
+
@db_path
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/tagm/db.rb
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
|
5
|
+
require_relative 'config'
|
6
|
+
|
7
|
+
require 'debug'
|
8
|
+
|
9
|
+
module Tagm
|
10
|
+
class Db # rubocop:disable Metrics/ClassLength
|
11
|
+
class << self
|
12
|
+
def db
|
13
|
+
@db ||= SQLite3::Database.new Config.db_path
|
14
|
+
end
|
15
|
+
|
16
|
+
# Tags a node
|
17
|
+
#
|
18
|
+
# @param node [String] Node to tag
|
19
|
+
# @param tags [Array<String>] Tags to apply
|
20
|
+
#
|
21
|
+
# @return [nil]
|
22
|
+
def tag(node:, tags:)
|
23
|
+
tags = sanitize_tags(tags).uniq
|
24
|
+
|
25
|
+
path = find_path node
|
26
|
+
existing = show(node) || []
|
27
|
+
|
28
|
+
tags_to_create = tags - existing
|
29
|
+
return if tags_to_create.empty?
|
30
|
+
|
31
|
+
sql_inserts = tags_to_create.map do |t|
|
32
|
+
tag = find_tag t
|
33
|
+
"(#{path[:id]}, #{tag[:id]})"
|
34
|
+
end.join(', ')
|
35
|
+
|
36
|
+
db.execute "INSERT INTO paths_tags (path_id, tag_id) VALUES #{sql_inserts};"
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remove tags from a node
|
40
|
+
#
|
41
|
+
# @param node [String] Node to tag
|
42
|
+
# @param tags [Array<String>] Tags to remove
|
43
|
+
#
|
44
|
+
# @return [nil]
|
45
|
+
def untag(node:, tags:)
|
46
|
+
tags = sanitize_tags(tags)
|
47
|
+
node = find_path node
|
48
|
+
|
49
|
+
tags = find_tags(tags).map { |t| t[:id] }
|
50
|
+
|
51
|
+
placeholders = Array.new(tags.length) { '?' }.join(', ')
|
52
|
+
db.execute "DELETE from paths_tags WHERE path_id=? AND tag_id IN (#{placeholders})", [node[:id]] + tags
|
53
|
+
end
|
54
|
+
|
55
|
+
# Merge tags
|
56
|
+
#
|
57
|
+
# @param from [Array<String>] List of tags to be merged
|
58
|
+
# @param into [String] Target tag
|
59
|
+
#
|
60
|
+
# @return [nil]
|
61
|
+
def merge(from:, into:) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
62
|
+
into = sanitize_tag(into)
|
63
|
+
# Don't sanitize "from" to allow work on previously malformed tags
|
64
|
+
from = [from] unless from.is_a? Array
|
65
|
+
|
66
|
+
raise 'Cannot merge into itself' if from.include? into
|
67
|
+
|
68
|
+
into = find_tag(into) # Will create target if missing
|
69
|
+
|
70
|
+
# Merge tags
|
71
|
+
from_ids = db.execute("SELECT id FROM tags WHERE name IN (#{query_placeholders(from)})", from).flatten
|
72
|
+
db.execute "UPDATE paths_tags SET tag_id= ? WHERE tag_id IN (#{query_placeholders(from_ids)})",
|
73
|
+
[into[:id]] + from_ids
|
74
|
+
|
75
|
+
# Remove duplicate entries
|
76
|
+
dupes = db.execute <<~SQL
|
77
|
+
SELECT path_id, tag_id, count(*)
|
78
|
+
FROM paths_tags
|
79
|
+
GROUP BY path_id, tag_id
|
80
|
+
HAVING count(*) > 1
|
81
|
+
SQL
|
82
|
+
|
83
|
+
sql = dupes.map do |(path_id, tag_id)|
|
84
|
+
<<~SQL
|
85
|
+
DELETE FROM paths_tags WHERE path_id=#{path_id} AND tag_id=#{tag_id};
|
86
|
+
INSERT INTO paths_tags (path_id, tag_id) VALUES (#{path_id}, #{tag_id});
|
87
|
+
SQL
|
88
|
+
end
|
89
|
+
|
90
|
+
db.execute_batch sql.join
|
91
|
+
end
|
92
|
+
|
93
|
+
# Show tags on a node
|
94
|
+
#
|
95
|
+
# @param node [String] Node to check
|
96
|
+
# @return [Array<String>] Tags associated to the node
|
97
|
+
def show(node)
|
98
|
+
path = find_path node, create: false
|
99
|
+
|
100
|
+
return nil unless path
|
101
|
+
|
102
|
+
pt = db.execute <<~SQL, path[:id]
|
103
|
+
SELECT tags.name FROM tags INNER JOIN paths_tags pt on tags.id = pt.tag_id AND pt.path_id = ? ORDER BY tags.name
|
104
|
+
SQL
|
105
|
+
|
106
|
+
pt.flatten
|
107
|
+
end
|
108
|
+
|
109
|
+
# List nodes with tags
|
110
|
+
#
|
111
|
+
# @param tags [Array<String>] List of tags for the filter
|
112
|
+
# @return [Hash] Hash with path as keys and tags list as value
|
113
|
+
def list(*tags) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
114
|
+
tags = sanitize_tags(tags)
|
115
|
+
|
116
|
+
sql = <<~SQL
|
117
|
+
SELECT paths.path, tags.name
|
118
|
+
FROM paths, tags
|
119
|
+
INNER JOIN paths_tags pt on paths.id = pt.path_id AND tags.id = pt.tag_id
|
120
|
+
SQL
|
121
|
+
sql += "WHERE tags.name IN (#{query_placeholders(tags)})" unless tags.empty?
|
122
|
+
sql += 'ORDER BY tags.name'
|
123
|
+
|
124
|
+
results = if tags.empty?
|
125
|
+
db.execute sql
|
126
|
+
else
|
127
|
+
db.execute sql, [tags]
|
128
|
+
end
|
129
|
+
|
130
|
+
# { <path>: [<tags>] }
|
131
|
+
paths = {}
|
132
|
+
results.each do |l|
|
133
|
+
paths[l[0]] ||= []
|
134
|
+
paths[l[0]] << l[1]
|
135
|
+
end
|
136
|
+
|
137
|
+
paths
|
138
|
+
end
|
139
|
+
|
140
|
+
def create_database! # rubocop:disable Metrics/MethodLength
|
141
|
+
db.execute_batch <<~SQL
|
142
|
+
CREATE TABLE IF NOT EXISTS tags (
|
143
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
144
|
+
name VARCHAR(30)
|
145
|
+
);
|
146
|
+
CREATE TABLE IF NOT EXISTS paths (
|
147
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
148
|
+
path TEXT
|
149
|
+
);
|
150
|
+
CREATE TABLE IF NOT EXISTS paths_tags (
|
151
|
+
path_id INTEGER,
|
152
|
+
tag_id INTEGER,
|
153
|
+
FOREIGN KEY(path_id) REFERENCES paths(id),
|
154
|
+
FOREIGN KEY(tag_id) REFERENCES tags(id)
|
155
|
+
);
|
156
|
+
SQL
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def find_tag(tag, create: true)
|
162
|
+
record = db.get_first_row 'SELECT id, name FROM tags WHERE name = ? LIMIT 1', tag
|
163
|
+
return [:id, :name].zip(record).to_h unless record.nil?
|
164
|
+
return unless create
|
165
|
+
|
166
|
+
db.execute 'INSERT INTO tags (name) VALUES (?)', tag
|
167
|
+
{ id: db.last_insert_row_id, name: tag }
|
168
|
+
end
|
169
|
+
|
170
|
+
def find_tags(tags)
|
171
|
+
results = db.execute "SELECT id, name FROM tags WHERE name IN (#{query_placeholders(tags)})", tags
|
172
|
+
|
173
|
+
fields = [:id, :name]
|
174
|
+
results.map do |r|
|
175
|
+
fields.zip(r).to_h
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def find_path(node, create: true)
|
180
|
+
record = db.get_first_row 'SELECT id, path FROM paths WHERE path = ? LIMIT 1', node
|
181
|
+
return [:id, :path].zip(record).to_h unless record.nil?
|
182
|
+
return unless create
|
183
|
+
|
184
|
+
db.execute 'INSERT INTO paths (path) VALUES (?)', node
|
185
|
+
{ id: db.last_insert_row_id, path: node }
|
186
|
+
end
|
187
|
+
|
188
|
+
def sanitize_tags(tags)
|
189
|
+
tags.map { |t| sanitize_tag t }
|
190
|
+
end
|
191
|
+
|
192
|
+
def sanitize_tag(tag)
|
193
|
+
tag.downcase.strip.sub(/,$/, '')
|
194
|
+
end
|
195
|
+
|
196
|
+
def query_placeholders(array)
|
197
|
+
Array.new(array.length) { '?' }.join(', ')
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
data/lib/tagm/version.rb
ADDED
data/lib/tagm.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tagm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- manu
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-03-07 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: skittlize
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.2'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1.2'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: sqlite3
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.6'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.6'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: thor
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.3'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: tty-config
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0.6'
|
61
|
+
type: :runtime
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0.6'
|
68
|
+
description: Allows to add tags on files, then find the files by tags. A SQLite database
|
69
|
+
stores the references.
|
70
|
+
email:
|
71
|
+
- manu@experimentslabs.com
|
72
|
+
executables:
|
73
|
+
- tagm
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".ruby-version"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- CODE_OF_CONDUCT.md
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- exe/tagm
|
86
|
+
- lib/tagm.rb
|
87
|
+
- lib/tagm/cli.rb
|
88
|
+
- lib/tagm/config.rb
|
89
|
+
- lib/tagm/db.rb
|
90
|
+
- lib/tagm/version.rb
|
91
|
+
homepage: https://experimentslabs.com
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
homepage_uri: https://experimentslabs.com
|
96
|
+
source_code_uri: https://gitlab.com/experimentslabs/tagm
|
97
|
+
changelog_uri: https://gitlab.com/experimentslabs/tagm/-/blob/main/CHANGELOG.md
|
98
|
+
rubygems_mfa_required: 'true'
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 3.1.0
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubygems_version: 3.6.5
|
114
|
+
specification_version: 4
|
115
|
+
summary: Tool to manage tags on files
|
116
|
+
test_files: []
|