Skip to content

Commit 68a9b5b

Browse files
committed
all: support alias import from file
1 parent 5b73208 commit 68a9b5b

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

import.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
changkun: https://changkun.de/

redir.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var (
18-
daemon = flag.Bool("s", false, "run redir service")
19-
operate = flag.String("op", "create", "operators, create/update/delete/fetch")
20-
alias = flag.String("a", "", "alias for a new link")
21-
link = flag.String("l", "", "actual link for the alias, optional for delete/fetch")
18+
daemon = flag.Bool("s", false, "run redir service")
19+
fromfile = flag.String("f", "", "import aliases from a YAML file")
20+
operate = flag.String("op", "create", "operators, create/update/delete/fetch")
21+
alias = flag.String("a", "", "alias for a new link")
22+
link = flag.String("l", "", "actual link for the alias, optional for delete/fetch")
2223
)
2324

2425
func main() {
@@ -27,6 +28,11 @@ func main() {
2728
flag.Usage = usage
2829
flag.Parse()
2930

31+
if len(os.Args) < 2 {
32+
flag.Usage()
33+
return
34+
}
35+
3036
if daemon == nil {
3137
flag.Usage()
3238
return
@@ -48,19 +54,31 @@ func processServer() {
4854
}
4955

5056
func usage() {
51-
fmt.Fprintf(os.Stderr, `usage: redir [-s] [-op <operator> -a <alias> -l <link>]
57+
fmt.Fprintf(os.Stderr, `usage: redir [-s] [-f <file>] [-op <operator> -a <alias> -l <link>]
5258
options:
5359
`)
5460
flag.PrintDefaults()
5561
fmt.Fprintf(os.Stderr, `example:
5662
redir -s run the redir service
63+
redir -f ./import.yml import aliases from a file
5764
redir -a alias -l link allocate new short link if possible
5865
redir -op fetch -a alias fetch alias information
5966
`)
6067
os.Exit(2)
6168
}
6269

6370
func processCmd() {
71+
if fromfile != nil {
72+
fname := *fromfile
73+
if fname == "" {
74+
flag.Usage()
75+
return
76+
}
77+
78+
shortFile(fname)
79+
return
80+
}
81+
6482
if operate == nil || !op(*operate).valid() {
6583
flag.Usage()
6684
return

short.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13+
"io/ioutil"
1314
"log"
1415
"net"
1516
"net/http"
1617
"strings"
18+
"time"
1719

1820
"github.com/go-redis/redis/v8"
21+
"gopkg.in/yaml.v3"
1922
)
2023

2124
// op is a short link operator
@@ -41,6 +44,31 @@ func (o op) valid() bool {
4144
}
4245
}
4346

47+
func shortFile(fname string) {
48+
b, err := ioutil.ReadFile(fname)
49+
if err != nil {
50+
log.Fatalf("cannot read import file, err: %v\n", err)
51+
}
52+
53+
d := map[string]string{}
54+
err = yaml.Unmarshal(b, &d)
55+
if err != nil {
56+
log.Fatalf("cannot unmarshal the imported file, err: %v\n", err)
57+
}
58+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
59+
defer cancel()
60+
for alias, link := range d {
61+
err = shortCmd(ctx, opUpdate, alias, link)
62+
if err != nil {
63+
err = shortCmd(ctx, opCreate, alias, link)
64+
if err != nil {
65+
log.Fatalf("cannot import alias %v, err: %v\n", alias, err)
66+
}
67+
}
68+
}
69+
return
70+
}
71+
4472
// shortCmd processes the given alias and link with a specified op.
4573
func shortCmd(ctx context.Context, operate op, alias, link string) (retErr error) {
4674
s, err := newStore(conf.Store)

0 commit comments

Comments
 (0)