generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tanaka0325 / 課題1 #6
Open
tanaka0325
wants to merge
9
commits into
master
Choose a base branch
from
kadai1-tanaka0325
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6d67ac
Implements imgconv
tanaka0325 5868629
Create testdata dir
tanaka0325 51e8c55
change structure
tanaka0325 6c9f2bb
error handling
tanaka0325 d5664c8
オプション、引数の基本的な処理の場所を移動
tanaka0325 bcdcd95
refactoring 1
tanaka0325 a265027
Add comments
tanaka0325 a235d16
refactoring
tanaka0325 4d6cc8d
Fix typo
tanaka0325 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Image Converter | ||
|
||
## Spec | ||
|
||
``` | ||
## 次の仕様を満たすコマンドを作って下さい | ||
|
||
- ディレクトリを指定する | ||
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
- ディレクトリ以下は再帰的に処理する | ||
- 変換前と変換後の画像形式を指定できる(オプション) | ||
|
||
## 以下を満たすように開発してください | ||
|
||
- mainパッケージと分離する | ||
- 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
- 準標準パッケージ:golang.org/x以下のパッケージ | ||
- ユーザ定義型を作ってみる | ||
- GoDocを生成してみる | ||
- Go Modulesを使ってみる | ||
``` | ||
|
||
## Usage | ||
|
||
```zsh | ||
# build | ||
$ go build -o ./imgconv | ||
|
||
# display help | ||
$ ./imgconv -h | ||
Usage of ./imgconv: | ||
-f string | ||
file extention before convert (default "jpg") | ||
-n dry run | ||
-t string | ||
file extention after convert (default "png") | ||
|
||
# single directory | ||
$ ./imgconv testdata/images | ||
|
||
# multi directories | ||
$ ./imgconv testdata/images testdata/images2 | ||
|
||
# customize ext | ||
$ ./imgconv -f png -t gif testdata/images | ||
|
||
# dry run | ||
$ ./imgconv -n testdata/images testdata/images2 | ||
testdata/images/sample1.jpg => testdata/images/sample1.png | ||
testdata/images2/img/sample3.jpg => testdata/images2/img/sample3.png | ||
testdata/images2/sample2.jpg => testdata/images2/sample2.png | ||
|
||
``` | ||
|
||
## 感想 | ||
|
||
- long option(?) はどうやってやれば良いのだろうか | ||
- そもそも作りとしてこれで良いのだろうか・・・めっちゃ悩みました | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package converter | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"io" | ||
"os" | ||
|
||
"golang.org/x/image/bmp" | ||
"golang.org/x/image/tiff" | ||
) | ||
|
||
type convImage struct { | ||
filename string | ||
fromExt string | ||
toExt string | ||
image image.Image | ||
} | ||
|
||
func (i *convImage) decode() error { | ||
r, err := os.Open(i.filename + "." + i.fromExt) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
|
||
img, err := decodeHelper(r, i.fromExt) | ||
if err != nil { | ||
return fmt.Errorf("decode error: %w", err) | ||
} | ||
|
||
i.image = img | ||
return nil | ||
} | ||
|
||
func decodeHelper(r io.Reader, ext string) (image.Image, error) { | ||
switch ext { | ||
case "png": | ||
return png.Decode(r) | ||
case "jpg", "jpeg": | ||
return jpeg.Decode(r) | ||
case "gif": | ||
return gif.Decode(r) | ||
case "bmp": | ||
return bmp.Decode(r) | ||
case "tiff", "tif": | ||
return tiff.Decode(r) | ||
} | ||
return nil, fmt.Errorf("%s is not allowed", ext) | ||
} | ||
|
||
func (i *convImage) encode() error { | ||
w, err := os.Create(i.filename + "." + i.toExt) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() error { | ||
if err := w.Close(); err != nil { | ||
return err | ||
} | ||
return nil | ||
}() | ||
|
||
switch i.toExt { | ||
case "png": | ||
return png.Encode(w, i.image) | ||
case "jpg", "jpeg": | ||
return jpeg.Encode(w, i.image, nil) | ||
case "gif": | ||
return gif.Encode(w, i.image, nil) | ||
case "bmp": | ||
return gif.Encode(w, i.image, nil) | ||
case "tiff", "tif": | ||
return gif.Encode(w, i.image, nil) | ||
} | ||
|
||
return fmt.Errorf("cannot encode image") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package converter | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// flags | ||
var ( | ||
allowedExts = exts{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"} | ||
f = flag.String("f", "jpg", "file extention before convert") | ||
t = flag.String("t", "png", "file extention after convert") | ||
dryRun = flag.Bool("n", false, "dry run") | ||
) | ||
|
||
// Convert is to convert image file format | ||
func Convert() { | ||
// check options ext | ||
flag.Parse() | ||
to := strings.ToLower(*t) | ||
from := strings.ToLower(*f) | ||
targetExts := []string{to, from} | ||
for _, e := range targetExts { | ||
if err := allowedExts.include(e); err != nil { | ||
log.Fatal(fmt.Errorf("%w. ext is only allowd in %s", err, allowedExts)) | ||
} | ||
} | ||
|
||
// get target image paths from args | ||
dns := flag.Args() | ||
udns := uniq(dns) | ||
paths, err := getPaths(udns, from) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// convert | ||
imgs, err := createConvImages(paths, from, to) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, img := range imgs { | ||
if err := img.decode(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if *dryRun { | ||
fmt.Println(img.filename+"."+img.fromExt, "=>", img.filename+"."+img.toExt) | ||
} else { | ||
if err := img.encode(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func uniq(s []string) []string { | ||
m := map[string]bool{} | ||
u := []string{} | ||
|
||
for _, v := range s { | ||
if !m[v] { | ||
m[v] = true | ||
u = append(u, v) | ||
} | ||
} | ||
|
||
return u | ||
} | ||
|
||
func getPaths(dns []string, from string) ([]string, error) { | ||
paths := []string{} | ||
|
||
for _, dn := range dns { | ||
if err := filepath.Walk(dn, func(path string, info os.FileInfo, err error) error { | ||
if filepath.Ext(path) == "."+from { | ||
paths = append(paths, path) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return paths, nil | ||
} | ||
|
||
func createConvImages(paths []string, from, to string) ([]convImage, error) { | ||
images := []convImage{} | ||
for _, p := range paths { | ||
i := convImage{ | ||
filename: strings.Replace(p, "."+from, "", 1), | ||
fromExt: strings.ToLower(from), | ||
toExt: strings.ToLower(to), | ||
} | ||
images = append(images, i) | ||
} | ||
|
||
return images, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package converter | ||
|
||
import "errors" | ||
|
||
type exts []string | ||
|
||
func (es exts) include(w string) error { | ||
for _, e := range es { | ||
if e == w { | ||
return nil | ||
} | ||
} | ||
return errors.New(w + " is not allowed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/gopherdojo/dojo8/kadai1/tanaka0325 | ||
|
||
go 1.14 | ||
|
||
require golang.org/x/image v0.0.0-20200618115811-c13761719519 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/gopherdojo/dojo8 v0.0.0-20200703052727-6a79d18126bf h1:lpYevjFQMxI5VNBc3WXV6Z5pDDrdppdDKwmeBoyt5BE= | ||
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= | ||
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/gopherdojo/dojo8/kadai1/tanaka0325/converter" | ||
|
||
func main() { | ||
converter.Convert() | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改訂2版 みんなのGo言語に、以下の様な形で書かれていました!
上記のような冗長な記述を避けたりする等の目的で、サードパーティ製のパッケージも出ている様です。
私はまだ利用したことないのですが、ご参考までに!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
おお!!ありがとうございます!!!すごくありがたいです🙏