Skip to content

Commit b2f2835

Browse files
committed
Fix some edge case bugs related to no dir
1 parent 8b4d946 commit b2f2835

File tree

7 files changed

+150
-44
lines changed

7 files changed

+150
-44
lines changed

.cspell.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.1",
3+
"language": "en",
4+
"ignorePaths": [
5+
"**/node_modules/**",
6+
"**/vscode-extension/**",
7+
"**/.git/**",
8+
".vscode",
9+
"package-lock.json",
10+
"report"
11+
],
12+
"words": []
13+
}

.github/workflows/mega-linter.yml

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
# Mega-Linter GitHub Action configuration file
3+
# More info at https://nvuillam.github.io/mega-linter
4+
name: Mega-Linter
5+
6+
on:
7+
# Trigger mega-linter at every push. Action will also be visible from Pull Requests to master
8+
push: # Comment this line to trigger action only on pull-requests (not recommended if you don't pay for GH Actions)
9+
pull_request:
10+
branches: [master, main]
11+
12+
env: # Comment env block if you do not want to apply fixes
13+
# Apply linter fixes configuration
14+
APPLY_FIXES: all # When active, APPLY_FIXES must also be defined as environment variable (in github/workflows/mega-linter.yml or other CI tool)
15+
APPLY_FIXES_EVENT: pull_request # Decide which event triggers application of fixes in a commit or a PR (pull_request, push, all)
16+
APPLY_FIXES_MODE: commit # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request)
17+
18+
jobs:
19+
# Cancel duplicate jobs: https://github.com/fkirc/skip-duplicate-actions#option-3-cancellation-only
20+
cancel_duplicates:
21+
name: Cancel duplicate jobs
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: fkirc/skip-duplicate-actions@master
25+
with:
26+
github_token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
27+
28+
build:
29+
name: Mega-Linter
30+
runs-on: ubuntu-latest
31+
steps:
32+
# Git Checkout
33+
- name: Checkout Code
34+
uses: actions/checkout@v2
35+
with:
36+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
37+
fetch-depth: 0
38+
39+
# Mega-Linter
40+
- name: Mega-Linter
41+
id: ml
42+
# You can override Mega-Linter flavor used to have faster performances
43+
# More info at https://nvuillam.github.io/mega-linter/flavors/
44+
uses: nvuillam/mega-linter/flavors/go@v4
45+
env:
46+
# All available variables are described in documentation
47+
# https://nvuillam.github.io/mega-linter/configuration/
48+
VALIDATE_ALL_CODEBASE: true # Set ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} to validate only diff with master branch
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
# ADD YOUR CUSTOM ENV VARIABLES HERE TO OVERRIDE VALUES OF .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
51+
52+
# Upload Mega-Linter artifacts
53+
- name: Archive production artifacts
54+
if: ${{ success() }} || ${{ failure() }}
55+
uses: actions/upload-artifact@v2
56+
with:
57+
name: Mega-Linter reports
58+
path: |
59+
report
60+
mega-linter.log
61+
62+
# Create pull request if applicable (for now works only on PR from same repository, not from forks)
63+
- name: Create Pull Request with applied fixes
64+
id: cpr
65+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
66+
uses: peter-evans/create-pull-request@v3
67+
with:
68+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
69+
commit-message: "[Mega-Linter] Apply linters automatic fixes"
70+
title: "[Mega-Linter] Apply linters automatic fixes"
71+
labels: bot
72+
- name: Create PR output
73+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
74+
run: |
75+
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
76+
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
77+
78+
# Push new commit if applicable (for now works only on PR from same repository, not from forks)
79+
- name: Prepare commit
80+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/master' && github.event.pull_request.head.repo.full_name == github.repository
81+
run: sudo chown -Rc $UID .git/
82+
- name: Commit and push applied linter fixes
83+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/master' && github.event.pull_request.head.repo.full_name == github.repository
84+
uses: stefanzweifel/git-auto-commit-action@v4
85+
with:
86+
branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
87+
commit_message: "[Mega-Linter] Apply linters fixes"

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*.DS_Store
22
__debug_bin
3-
/tii
3+
/rem
44
.idea

.jscpd.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"threshold": 0,
3+
"reporters": [
4+
"html",
5+
"markdown"
6+
],
7+
"ignore": [
8+
"**/node_modules/**",
9+
"**/.git/**",
10+
"**/.rbenv/**",
11+
"**/.venv'/**",
12+
"**/*cache*/**",
13+
"**/.github/**",
14+
"**/.idea/**",
15+
"**/report/**"
16+
]
17+
}

.mega-linter.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Configuration file for Mega-Linter
2+
# See all available variables at https://nvuillam.github.io/mega-linter/configuration/ and in linters documentation
3+
4+
APPLY_FIXES: all # all, none, or list of linter keys
5+
DEFAULT_BRANCH: main # Usually master or main
6+
# ENABLE: # If you use ENABLE variable, all other languages/formats/tooling-formats will be disabled by default
7+
# ENABLE_LINTERS: # If you use ENABLE_LINTERS variable, all other linters will be disabled by default
8+
DISABLE:
9+
- COPYPASTE # Comment to disable checks of abusive copy-pastes
10+
- SPELL # Comment to disable checks of spelling mistakes # - SPELL # Uncomment to disable checks of spelling mistakes
11+
SHOW_ELAPSED_TIME: true
12+
FILEIO_REPORTER: false

lol

Whitespace-only changes.

rem.go

+20-43
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ var (
1515
version = "dev"
1616
helpMsg = `Rem - Get some rem sleep knowing your files are safe
1717
Rem is a CLI Trash
18-
Usage: rem [-h/--help | -v/--version | -d/--directory | -l/--list | --t/--set-trash | `
18+
Usage: rem [<option>]
19+
rem fileToTrash
20+
rem -u/--undo fileToRestore
21+
Options:
22+
-h/--help | -v/--version | -d/--directory | -l/--list | --t/--set-trash`
1923
home, _ = os.UserHomeDir()
2024
trashDir = home + "/.remTrash"
2125
logFileName = ".trash.log"
2226
logSeparator = " ==> "
2327
)
2428

2529
func main() {
26-
//for key, value := range parseLogFile() {
27-
// fmt.Println("Key:", key, "Value:", value)
28-
//}
2930
if len(os.Args) == 1 {
3031
handleErrStr("too few arguments")
3132
fmt.Println(helpMsg)
@@ -59,7 +60,7 @@ func main() {
5960
}
6061
if hasOption, _ := argsHaveOptionLong("empty-trash"); hasOption {
6162
color.Red("Warning, permanently deleting these files in trash: ")
62-
fmt.Println(listFilesInTrash())
63+
printFormattedList(listFilesInTrash())
6364
if promptBool("Confirm delete?") {
6465
emptyTrash()
6566
}
@@ -98,24 +99,6 @@ func main() {
9899
}
99100

100101
func listFilesInTrash() []string {
101-
//file, err := os.Open(trashDir)
102-
//if err != nil {
103-
// handleErrStr("Could not open " + trashDir)
104-
// handleErr(err)
105-
// return nil
106-
//}
107-
//defer func() {
108-
// err = file.Close()
109-
// if err != nil {
110-
// handleErr(err)
111-
// }
112-
//}()
113-
//list, err := file.Readdirnames(0) // >=0 to read all files and folders
114-
//if err != nil {
115-
// handleErrStr("An error occurred while trying to list files in " + trashDir)
116-
// handleErr(err)
117-
//}
118-
//return list
119102
m := parseLogFile()
120103
s := make([]string, 0, 10)
121104
for key, _ := range m {
@@ -129,13 +112,13 @@ func emptyTrash() {
129112
}
130113

131114
func parseLogFile() map[string]string {
115+
ensureTrashDir()
132116
file, err := os.OpenFile(trashDir+"/"+logFileName, os.O_CREATE|os.O_RDONLY, 0644)
133117
if err != nil {
134118
handleErr(err)
135119
return nil
136120
}
137121
defer file.Close()
138-
//var lines map[string]string
139122
lines := make(map[string]string)
140123
scanner := bufio.NewScanner(file)
141124
for scanner.Scan() {
@@ -149,12 +132,9 @@ func parseLogFile() map[string]string {
149132
}
150133

151134
func setLogFile(m map[string]string) {
152-
//err := os.Truncate(trashDir+"/"+logFileName, 0)
153-
//if err != nil {
154-
// handleErr(err)
155-
// return
156-
//}
157-
f, err := os.OpenFile(trashDir+"/"+logFileName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) // truncate to empty, create if not exist, write only
135+
//f, err := os.OpenFile(trashDir+"/"+logFileName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) // truncate to empty, create if not exist, write only
136+
ensureTrashDir()
137+
f, err := os.Create(trashDir + "/" + logFileName)
158138
if err != nil {
159139
handleErr(err)
160140
return
@@ -205,12 +185,10 @@ func trashFile(path string) {
205185
return
206186
}
207187
if _, err = os.Stat(path); os.IsNotExist(err) {
208-
// file specified does not exist
209188
handleErrStr(color.YellowString(path) + " does not exist")
210189
return
211190
}
212191
if i, err := os.Stat(toMoveTo); !(os.IsNotExist(err)) {
213-
// file of same name in trash
214192
handleErrStr("file with name " + color.YellowString(i.Name()) + " already in ~/.remTrash at " + color.YellowString(toMoveTo)) // as helpful as possible
215193
return
216194
}
@@ -219,20 +197,10 @@ func trashFile(path string) {
219197
handleErr(err)
220198
return
221199
}
222-
//f, err := os.OpenFile(trashDir+"/"+logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
223-
//if err != nil {
224-
// handleErr(err)
225-
// return
226-
//}
227-
//defer f.Close()
228-
//if _, err = f.WriteString(path + logSeparator + toMoveTo + "\n"); err != nil {
229-
// handleErr(err)
230-
// return
231-
//}
232200
m := parseLogFile()
233201
m[path] = toMoveTo // logfile format is path where it came from ==> path in trash
234202
setLogFile(m)
235-
fmt.Println("Trashed " + color.YellowString(path) + " to " + color.YellowString(toMoveTo) + "\nUndo using " + color.YellowString("rem --undo "+path))
203+
fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+path))
236204
}
237205

238206
func ensureTrashDir() {
@@ -244,6 +212,15 @@ func ensureTrashDir() {
244212
}
245213
}
246214

215+
func ensureLogFile() {
216+
if _, err := os.Stat(trashDir + "/" + logFileName); os.IsNotExist(err) {
217+
_, err = os.Create(trashDir + "/" + logFileName)
218+
if err != nil {
219+
handleErr(err)
220+
}
221+
}
222+
}
223+
247224
func permanentlyDeleteFile(fileName string) {
248225
err := os.RemoveAll(fileName)
249226
if err != nil {

0 commit comments

Comments
 (0)