Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
3d02c5b7
Commit
3d02c5b7
authored
1 year ago
by
Valera Shaitorov
Browse files
Options
Downloads
Patches
Plain Diff
Перенесены util из perxis, перенесен код dowloader'a
parent
f825f043
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
pkg/cli/gracefull.go
+40
-0
40 additions, 0 deletions
pkg/cli/gracefull.go
pkg/data/translit.go
+54
-0
54 additions, 0 deletions
pkg/data/translit.go
pkg/data/translit_test.go
+45
-0
45 additions, 0 deletions
pkg/data/translit_test.go
pkg/files/downloader.go
+24
-0
24 additions, 0 deletions
pkg/files/downloader.go
with
163 additions
and
0 deletions
pkg/cli/gracefull.go
0 → 100644
+
40
−
0
View file @
3d02c5b7
package
cli
import
(
"os"
"os/signal"
"syscall"
"go.uber.org/zap"
)
func
WaitForQuit
(
logger
*
zap
.
Logger
,
finailizer
func
())
{
donec
:=
make
(
chan
struct
{})
sigc
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
sigc
,
syscall
.
SIGTERM
,
os
.
Interrupt
)
var
signalsReceived
uint
go
func
()
{
for
{
select
{
case
s
:=
<-
sigc
:
logger
.
Info
(
"Signal received. Exiting"
,
zap
.
String
(
"Signal"
,
s
.
String
()))
signalsReceived
++
if
signalsReceived
<
2
{
// After first Ctrl+C start quitting the worker gracefully
go
func
()
{
finailizer
()
close
(
donec
)
}()
}
else
{
// Abort the program when user hits Ctrl+C second time in a row
logger
.
Info
(
"Force exit"
)
close
(
donec
)
}
}
}
}()
<-
donec
logger
.
Info
(
"Quit"
)
}
This diff is collapsed.
Click to expand it.
pkg/data/translit.go
0 → 100644
+
54
−
0
View file @
3d02c5b7
package
data
import
(
"bytes"
"strings"
"unicode"
)
var
mapRuEn
=
map
[
rune
]
string
{
'а'
:
"a"
,
'б'
:
"b"
,
'в'
:
"v"
,
'г'
:
"g"
,
'д'
:
"d"
,
'е'
:
"e"
,
'ё'
:
"yo"
,
'ж'
:
"zh"
,
'з'
:
"z"
,
'и'
:
"i"
,
'й'
:
"j"
,
'к'
:
"k"
,
'л'
:
"l"
,
'м'
:
"m"
,
'н'
:
"n"
,
'о'
:
"o"
,
'п'
:
"p"
,
'р'
:
"r"
,
'с'
:
"s"
,
'т'
:
"t"
,
'у'
:
"u"
,
'ф'
:
"f"
,
'х'
:
"h"
,
'ц'
:
"c"
,
'ч'
:
"ch"
,
'ш'
:
"sh"
,
'щ'
:
"shh"
,
'ъ'
:
""
,
'ы'
:
"y"
,
'ь'
:
""
,
'э'
:
"e"
,
'я'
:
"ya"
,
}
func
mapSpecial
(
r
rune
)
rune
{
if
r
<=
unicode
.
MaxASCII
&&
(
unicode
.
IsLetter
(
r
)
||
unicode
.
IsNumber
(
r
)
||
r
==
':'
||
r
==
'/'
)
{
return
r
}
return
'-'
}
func
StringTransform
(
s
string
,
lowercase
bool
)
string
{
s
=
TableEncode
(
s
,
mapRuEn
)
s
=
strings
.
Map
(
mapSpecial
,
s
)
if
lowercase
{
s
=
strings
.
ToLower
(
s
)
}
return
s
}
func
TableEncode
(
s
string
,
tlm
map
[
rune
]
string
)
string
{
in
:=
bytes
.
NewBufferString
(
s
)
out
:=
bytes
.
NewBuffer
(
nil
)
for
{
r
,
_
,
err
:=
in
.
ReadRune
()
if
err
!=
nil
{
break
// EOF
}
tr
,
ok
:=
tlm
[
unicode
.
ToLower
(
r
)]
if
!
ok
{
out
.
WriteRune
(
r
)
continue
}
if
unicode
.
IsUpper
(
r
)
{
tr
=
strings
.
Title
(
tr
)
}
out
.
WriteString
(
tr
)
}
return
out
.
String
()
}
This diff is collapsed.
Click to expand it.
pkg/data/translit_test.go
0 → 100644
+
45
−
0
View file @
3d02c5b7
package
data
import
(
"testing"
)
func
TestTranslit
(
t
*
testing
.
T
)
{
type
args
struct
{
s
string
lowercase
bool
}
tests
:=
[]
struct
{
name
string
args
args
want
string
}{
{
name
:
"test translit cyrillic"
,
args
:
args
{
"Ёжик 2019"
,
true
},
want
:
"yozhik-2019"
,
},
{
name
:
"test translit cyrillic"
,
args
:
args
{
"Ёжик"
,
false
},
want
:
"Yozhik"
,
},
{
name
:
"test translit english"
,
args
:
args
{
"Hello world,sample"
,
true
},
want
:
"hello-world-sample"
,
},
{
name
:
"test translit english"
,
args
:
args
{
"Специальные...символы"
,
true
},
want
:
"specialnye---simvoly"
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
if
got
:=
StringTransform
(
tt
.
args
.
s
,
tt
.
args
.
lowercase
);
got
!=
tt
.
want
{
t
.
Errorf
(
"Translit() = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
This diff is collapsed.
Click to expand it.
pkg/files/downloader.go
+
24
−
0
View file @
3d02c5b7
package
files
import
(
"encoding/base64"
"errors"
"io"
"net/http"
...
...
@@ -30,3 +31,26 @@ func (d *downloader) Download(dst io.Writer, file *File) error {
_
,
err
=
io
.
Copy
(
dst
,
r
.
Body
)
return
err
}
type
noopDownloader
struct
{}
func
NewNoopDownloader
()
Downloader
{
return
&
noopDownloader
{}
}
func
(
d
*
noopDownloader
)
Download
(
dst
io
.
Writer
,
file
*
File
)
error
{
return
nil
}
type
dummyDownloader
struct
{}
func
NewDummyDownloader
()
Downloader
{
return
&
dummyDownloader
{}
}
func
(
d
*
dummyDownloader
)
Download
(
dst
io
.
Writer
,
file
*
File
)
error
{
// png pixel 10x10
pixel
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
"iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mNkYPhfz0AEYBxVSF+FAP5FDvcfRYWgAAAAAElFTkSuQmCC"
)
_
,
err
=
dst
.
Write
(
pixel
)
return
err
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment