Skip to content

Commit d5664c8

Browse files
committed
オプション、引数の基本的な処理の場所を移動
1 parent 6c9f2bb commit d5664c8

File tree

8 files changed

+81
-58
lines changed

8 files changed

+81
-58
lines changed

kadai1/tanaka0325/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
```zsh
2626
# build
27+
$ cd imgconv
2728
$ go build -o imgconv ./cmd/imgconv
2829

2930
# display help

kadai1/tanaka0325/imgconv/args.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package imgconv
2+
3+
type Args []string
4+
5+
func (args Args) uniq() []string {
6+
m := map[string]bool{}
7+
u := []string{}
8+
9+
for _, v := range args {
10+
if !m[v] {
11+
m[v] = true
12+
u = append(u, v)
13+
}
14+
}
15+
16+
return u
17+
}

kadai1/tanaka0325/imgconv/cmd/imgconv/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67

78
"github.com/gopherdojo/dojo8/kadai1/tanaka0325/imgconv"
89
)
910

11+
var options imgconv.Options
12+
var args imgconv.Args
13+
14+
func init() {
15+
options.From = flag.String("f", "jpg", "file extention before convert")
16+
options.To = flag.String("t", "png", "file extention after convert")
17+
options.DryRun = flag.Bool("n", false, "dry run")
18+
flag.Parse()
19+
20+
args = flag.Args()
21+
}
22+
1023
func main() {
11-
if err := imgconv.Run(); err != nil {
24+
if err := imgconv.Run(options, args); err != nil {
1225
fmt.Fprintln(os.Stderr, err)
1326
os.Exit(1)
1427
}

kadai1/tanaka0325/imgconv/exts.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

kadai1/tanaka0325/imgconv/go.mod.back

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/gopherdojo/dojo8/kadai1/tanaka0325/imgconv
2+
3+
go 1.14
4+
5+
require golang.org/x/image v0.0.0-20200618115811-c13761719519

kadai1/tanaka0325/imgconv/go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
github.com/gopherdojo/dojo8 v0.0.0-20200703052727-6a79d18126bf h1:lpYevjFQMxI5VNBc3WXV6Z5pDDrdppdDKwmeBoyt5BE=
21
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34=
32
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
43
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

kadai1/tanaka0325/imgconv/imgconv.go

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
package imgconv
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76
"path/filepath"
87
"strings"
98
)
109

11-
var (
12-
// flags
13-
f = flag.String("f", "jpg", "file extention before convert")
14-
t = flag.String("t", "png", "file extention after convert")
15-
dryRun = flag.Bool("n", false, "dry run")
16-
17-
// allow extensions
18-
allowedExts = exts{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"}
19-
)
20-
21-
func init() {
22-
flag.Parse()
23-
}
10+
var allowedExts = []string{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"}
2411

2512
// Run is to convert image file format
26-
func Run() error {
27-
// check options ext
28-
to := strings.ToLower(*t)
29-
from := strings.ToLower(*f)
30-
targetExts := []string{to, from}
31-
for _, e := range targetExts {
32-
if err := allowedExts.include(e); err != nil {
33-
return fmt.Errorf("%w. ext is only allowd in %s", err, allowedExts)
34-
}
13+
func Run(options Options, args Args) error {
14+
// validator
15+
if err := options.validate(allowedExts); err != nil {
16+
return err
3517
}
3618

3719
// get target image paths from args
38-
dns := flag.Args()
39-
udns := uniq(dns)
40-
paths, err := getPaths(udns, from)
20+
udns := args.uniq()
21+
paths, err := getPaths(udns, *options.From)
4122
if err != nil {
4223
return err
4324
}
4425

4526
// convert
46-
imgs, err := createConvImages(paths, from, to)
27+
imgs, err := createConvImages(paths, *options.From, *options.To)
4728
if err != nil {
4829
return err
4930
}
@@ -52,7 +33,7 @@ func Run() error {
5233
return err
5334
}
5435

55-
if *dryRun {
36+
if *options.DryRun {
5637
fmt.Println(img.filename+"."+img.fromExt, "=>", img.filename+"."+img.toExt)
5738
} else {
5839
if err := img.encode(); err != nil {
@@ -64,20 +45,6 @@ func Run() error {
6445
return nil
6546
}
6647

67-
func uniq(s []string) []string {
68-
m := map[string]bool{}
69-
u := []string{}
70-
71-
for _, v := range s {
72-
if !m[v] {
73-
m[v] = true
74-
u = append(u, v)
75-
}
76-
}
77-
78-
return u
79-
}
80-
8148
func getPaths(dns []string, from string) ([]string, error) {
8249
paths := []string{}
8350

kadai1/tanaka0325/imgconv/options.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package imgconv
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
type Options struct {
10+
From *string
11+
To *string
12+
DryRun *bool
13+
}
14+
15+
func (opt Options) validate(allow_list []string) error {
16+
to := strings.ToLower(*opt.To)
17+
from := strings.ToLower(*opt.From)
18+
targetExts := []string{to, from}
19+
for _, e := range targetExts {
20+
if err := include(allow_list, e); err != nil {
21+
return fmt.Errorf("%w. ext is only allowd in %s", err, allow_list)
22+
}
23+
}
24+
25+
return nil
26+
}
27+
28+
func include(list []string, w string) error {
29+
for _, e := range list {
30+
if e == w {
31+
return nil
32+
}
33+
}
34+
return errors.New(w + " is not allowed")
35+
}

0 commit comments

Comments
 (0)