generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Kadai2 edm20627 #51
Open
keethii27
wants to merge
24
commits into
gopherdojo:master
Choose a base branch
from
keethii27:kadai2-edm20627
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
Kadai2 edm20627 #51
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7dc7e7a
ひとまず動くものを作成
keethii27 5aed38c
ユーザ定義型を作成
keethii27 9dc9755
自作パッケージを作成
keethii27 7d5d81e
vlid関数をメソッドに変更
keethii27 7776099
微修正
keethii27 a504b57
画像変換処理のエラーハンドリング追加
keethii27 44bbfd1
削除フラグの追加
keethii27 87c6eb3
GoDocを生成する
keethii27 44e95d3
サンプル画像を追加
keethii27 f21ec02
README.md 追加
keethii27 49c5e89
.gitignore
keethii27 74fabfa
go mod tidy 実行'
keethii27 f4be072
.gitignoreの配置変更
keethii27 183f52f
課題1をコピー
keethii27 5fefe04
go moduleパス変更
keethii27 e1ba6c2
リファクタリング
keethii27 7caa18b
テスト追加
keethii27 c75f90f
fix エラーハンドリング追加
keethii27 6d344e9
fix タイポ
keethii27 3f72fd8
fix ネストが深いためValidメソッドをリファクタリング
keethii27 38cf933
fix TestValidに正 to 否、否 to 正ケースを追加
keethii27 539b81a
fix テスト用画像を使用しテストできるようにtastdataを作成
keethii27 1133222
fix DeleteOptionのテスト追加
keethii27 6d99bb2
fix 実際にテストでファイル削除しているか確認できるように修正
keethii27 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,17 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
.DS_store |
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,12 @@ | ||
# 画像変換コマンド | ||
指定ディレクトリ以下の画像ファイルを変換します。 | ||
|
||
## 使用方法 | ||
|
||
`cmd --from jpg --to png ディレクトリ` | ||
|
||
## オプション | ||
|
||
* `--from(-f)` 変換対象の画像形式(デフォルト: jpg) | ||
* `--to(-t)` 変換後の画像形式(デフォルト: png) | ||
* `--delete(-d)` 変換前の画像を削除(デフォルト: 無効) |
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,3 @@ | ||
module github.com/edm20627/gopherdojo-studyroom/kadai1/edm20627 | ||
|
||
go 1.15 |
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,108 @@ | ||
package imageconvert | ||
|
||
import ( | ||
"errors" | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Supported extensions. | ||
var SupportedFormat = []string{"png", "jpg", "jpeg", "gif"} | ||
|
||
type ConvertImage struct { | ||
filepaths []string | ||
From, To string | ||
DeleteOption bool | ||
} | ||
|
||
// Get the target files for image conversion. | ||
func (ci *ConvertImage) Get(dirs []string) error { | ||
for _, dir := range dirs { | ||
err := filepath.Walk(dir, | ||
func(path string, info os.FileInfo, err error) error { | ||
if info == nil { | ||
return errors.New(path + " is not directory") | ||
} | ||
if info.IsDir() || filepath.Ext(path)[1:] != ci.From { | ||
return nil | ||
} | ||
ci.filepaths = append(ci.filepaths, path) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Perform image conversion. | ||
func (ci *ConvertImage) Convert() error { | ||
for _, path := range ci.filepaths { | ||
err := convert(path, ci.To, ci.DeleteOption) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Check for supported extensions. | ||
func (ci *ConvertImage) Valid() bool { | ||
for _, v := range SupportedFormat { | ||
if v == ci.From { | ||
for _, v := range SupportedFormat { | ||
if v == ci.To { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func convert(path string, to string, deleteOption bool) error { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, _, err := image.Decode(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := os.Create(path[:len(path)-len(filepath.Ext(path))+1] + to) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
switch to { | ||
case "png": | ||
if err := png.Encode(out, img); err != nil { | ||
return err | ||
} | ||
case "jpg", "jpeg": | ||
if err := jpeg.Encode(out, img, nil); err != nil { | ||
return err | ||
} | ||
case "gif": | ||
if err := gif.Encode(out, img, nil); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if deleteOption { | ||
if err := os.Remove(path); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return 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,43 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/edm20627/gopherdojo-studyroom/kadai1/edm20627/imageconvert" | ||
) | ||
|
||
var ci = imageconvert.ConvertImage{} | ||
|
||
func init() { | ||
flag.StringVar(&ci.From, "-from", "jpg", "Specify the image format before conversion") | ||
flag.StringVar(&ci.From, "f", "jpg", "Specify the image format before conversion (short)") | ||
flag.StringVar(&ci.To, "-to", "png", "Specify the converted image format") | ||
flag.StringVar(&ci.To, "t", "png", "Specify the converted image format (short)") | ||
flag.BoolVar(&ci.DeleteOption, "-delete", false, "Delete the image before conversion") | ||
flag.BoolVar(&ci.DeleteOption, "d", false, "Delete the image before conversion (short)") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
dirs := flag.Args() | ||
|
||
if !ci.Valid() { | ||
fmt.Fprintln(os.Stderr, "supported formt is "+strings.Join(imageconvert.SupportedFormat, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
err := ci.Get(dirs) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
err = ci.Convert() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,17 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
.DS_store |
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,12 @@ | ||
# 画像変換コマンド | ||
指定ディレクトリ以下の画像ファイルを変換します。 | ||
|
||
## 使用方法 | ||
|
||
`cmd --from jpg --to png ディレクトリ` | ||
|
||
## オプション | ||
|
||
* `--from(-f)` 変換対象の画像形式(デフォルト: jpg) | ||
* `--to(-t)` 変換後の画像形式(デフォルト: png) | ||
* `--delete(-d)` 変換前の画像を削除(デフォルト: 無効) |
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,3 @@ | ||
module github.com/edm20627/gopherdojo-studyroom/kadai2/edm20627 | ||
|
||
go 1.15 |
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 @@ | ||
github.com/edm20627/gopherdojo-studyroom v0.0.0-20200808023015-dd3cfe05cf7a h1:qrHQJOIqb8bC1Ulkv5H6eGp3DZgRA93eLUJr6XK/Gjk= |
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,117 @@ | ||
package imageconvert | ||
|
||
import ( | ||
"errors" | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Supported extensions. | ||
var SupportedFormat = []string{"png", "jpg", "jpeg", "gif"} | ||
|
||
var ( | ||
ErrNotSpecified = errors.New("Need to specify directory or file") | ||
ErrMotDirectory = errors.New("Specify directory or file is not directory") | ||
) | ||
|
||
type ConvertImage struct { | ||
Filepaths []string | ||
From, To string | ||
DeleteOption bool | ||
} | ||
|
||
// Get the target files for image conversion. | ||
func (ci *ConvertImage) Get(dirs []string) error { | ||
if len(dirs) == 0 { | ||
return ErrNotSpecified | ||
} | ||
|
||
for _, dir := range dirs { | ||
err := filepath.Walk(dir, | ||
func(path string, info os.FileInfo, err error) error { | ||
if info == nil { | ||
return ErrMotDirectory | ||
} | ||
if info.IsDir() || filepath.Ext(path)[1:] != ci.From { | ||
return nil | ||
} | ||
ci.Filepaths = append(ci.Filepaths, path) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Perform image conversion. | ||
func (ci *ConvertImage) Convert() error { | ||
for _, path := range ci.Filepaths { | ||
err := convert(path, ci.To, ci.DeleteOption) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Check for supported extensions. | ||
func (ci *ConvertImage) Valid() bool { | ||
for _, v := range SupportedFormat { | ||
if v == ci.From { | ||
for _, v := range SupportedFormat { | ||
if v == ci.To { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ネストが深いので以下の方が綺麗かなと思いました
|
||
} | ||
|
||
func convert(path string, to string, deleteOption bool) error { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, _, err := image.Decode(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := os.Create(path[:len(path)-len(filepath.Ext(path))+1] + to) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
switch to { | ||
case "png": | ||
if err := png.Encode(out, img); err != nil { | ||
return err | ||
} | ||
case "jpg", "jpeg": | ||
if err := jpeg.Encode(out, img, nil); err != nil { | ||
return err | ||
} | ||
case "gif": | ||
if err := gif.Encode(out, img, nil); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if deleteOption { | ||
if err := os.Remove(path); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
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.
おそらくs/Mot/Not/ですね