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
sadah / 課題1 #8
Open
sadah
wants to merge
1
commit into
master
Choose a base branch
from
kadai1-sadah
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
sadah / 課題1 #8
Changes from all commits
Commits
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 @@ | ||
.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,100 @@ | ||
# imgconv | ||
|
||
imgconv is an image converter. | ||
|
||
# Install | ||
|
||
Use go get to install this package: | ||
|
||
``` | ||
$ go get github.com/gopherdojo/dojo8/kadai1/sadah/imgconv | ||
``` | ||
|
||
# Build | ||
|
||
|
||
``` | ||
$ git clone [email protected]:gopherdojo/dojo8.git | ||
$ cd dojo8/kadai1/sadah/cmd/imgconv | ||
$ go build -o imgconv | ||
``` | ||
|
||
# Usage | ||
|
||
``` | ||
./imgconv [options...] <path> | ||
``` | ||
|
||
To check the all available options, | ||
|
||
``` | ||
$ imgconv -h | ||
imgconv is an image converter | ||
Usage: imgconv [options...] [path] | ||
Use "imgconv --help" for more information about a command. | ||
|
||
Supported formats: [.jpg .jpeg .JPG .JPEG .png .PNG .gif .GIF .bmp .BMP .tiff .TIFF] | ||
|
||
-s string | ||
Set a source extension (default "jpg") | ||
-source string | ||
Set a source extension (default "jpg") | ||
-t string | ||
Set a target extension (default "png") | ||
-target string | ||
Set a target extension (default "png") | ||
-v Show version | ||
-verbose | ||
Print verbose messages | ||
-version | ||
Show version | ||
``` | ||
|
||
# Spec | ||
|
||
## Required Spec | ||
|
||
次の仕様を満たすコマンドを作って下さい | ||
|
||
* [x] ディレクトリを指定する | ||
* [x] 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
* [x] ディレクトリ以下は再帰的に処理する | ||
* [x] 変換前と変換後の画像形式を指定できる(オプション) | ||
|
||
以下を満たすように開発してください | ||
* [x] mainパッケージと分離する | ||
* [x] 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
* 準標準パッケージ:golang.org/x以下のパッケージ | ||
* [x] ユーザ定義型を作ってみる | ||
* [x] GoDocを生成してみる | ||
* [x] Go Modulesを使ってみる | ||
|
||
## Featured Spec | ||
|
||
* コマンドライン引数で PATH を指定。指定がない場合はカレントディレクトリが対象となる | ||
* ディレクトリ以下は再帰的に処理する | ||
* デフォルトでは指定したディレクトリ以下のJPGファイルをPNGに変換 | ||
* `-s` で変換前、 `-t` 変換後の画像形式を指定できる | ||
* jpg, png, gif, bmp, tiffに対応 | ||
* main パッケージと imgconv パッケージを分離 | ||
* 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
* 拡張子リストに `exts` というユーザ定義型を利用 | ||
* GoDocを生成した | ||
* Go Modulesを使った | ||
|
||
# 感想 | ||
|
||
`type` をどういったときに使うとよいのか、まだいまひとつわかっていない。 | ||
|
||
|
||
struct の初期化で、flagから受け取った値を使いたかったが、よい使い方が思いつかなかった。 | ||
|
||
愚直に代入すればできるけど、ポインタをうまく使う方法がわからなかった。以下の `&verbose` を struct に定義したかった…。 | ||
|
||
```go | ||
flag.BoolVar(&verbose, "verbose", false, optVerboseText) | ||
``` | ||
|
||
# Auther | ||
|
||
@sadah |
Binary file not shown.
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,110 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gopherdojo/dojo8/kadai1/sadah/imgconv" | ||
) | ||
|
||
const ( | ||
name = "imgconv" | ||
version = "0.0.1" | ||
defalutPath = "./" | ||
defalutSrcExt = "jpg" | ||
defalutTarExt = "png" | ||
usage = `imgconv is an image converter | ||
Usage: imgconv [options...] [path] | ||
Use "imgconv --help" for more information about a command. | ||
|
||
Supported formats:` | ||
optVerboseText = "Print verbose messages" | ||
optShowVersionText = "Show version" | ||
optSrcExtText = "Set a source extension" | ||
optTarExtText = "Set a target extension" | ||
) | ||
|
||
var ( | ||
verbose bool | ||
showVersion bool | ||
srcExt string | ||
tarExt string | ||
path string | ||
) | ||
|
||
func init() { | ||
flag.BoolVar(&verbose, "verbose", false, optVerboseText) | ||
|
||
flag.BoolVar(&showVersion, "v", false, optShowVersionText) | ||
flag.BoolVar(&showVersion, "version", false, optShowVersionText) | ||
|
||
flag.StringVar(&srcExt, "s", defalutSrcExt, optSrcExtText) | ||
flag.StringVar(&srcExt, "source", defalutSrcExt, optSrcExtText) | ||
|
||
flag.StringVar(&tarExt, "t", defalutTarExt, optTarExtText) | ||
flag.StringVar(&tarExt, "target", defalutTarExt, optTarExtText) | ||
|
||
flag.Usage = func() { | ||
usageTxt := usage | ||
fmt.Fprintf(os.Stderr, "%s %s\n\n", usageTxt, imgconv.SupportedExts) | ||
flag.PrintDefaults() | ||
} | ||
flag.Parse() | ||
|
||
if verbose { | ||
log.SetOutput(os.Stderr) | ||
} else { | ||
log.SetOutput(ioutil.Discard) | ||
} | ||
|
||
if showVersion { | ||
fmt.Println(version) | ||
os.Exit(0) | ||
} | ||
|
||
arg0 := flag.Arg(0) | ||
if arg0 == "" { | ||
path = defalutPath | ||
} else { | ||
path = arg0 | ||
} | ||
|
||
srcExt = "." + srcExt | ||
tarExt = "." + tarExt | ||
} | ||
|
||
func run() (err error) { | ||
log.Println("Target Path: " + path) | ||
log.Println("Source Extension: " + srcExt) | ||
log.Println("Target Extension: " + tarExt) | ||
|
||
if err = imgconv.VerifySupportedExt(srcExt); err != nil { | ||
return | ||
} | ||
|
||
if err = imgconv.VerifySupportedExt(tarExt); err != nil { | ||
return | ||
} | ||
|
||
return filepath.Walk(path, | ||
func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if filepath.Ext(path) == srcExt { | ||
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. ディレクトリかファイルかのチェックもあってもいいかもしれないです。 |
||
return imgconv.Conv(path, tarExt) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
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/sadah | ||
|
||
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,3 @@ | ||
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,95 @@ | ||
package imgconv | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"image/gif" | ||
"image/jpeg" | ||
"image/png" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"golang.org/x/image/bmp" | ||
"golang.org/x/image/tiff" | ||
) | ||
|
||
const ( | ||
canNotOpenImageFile = "Can not open an image file: %s" | ||
canNotWriteImageFile = "Can not write an image file: %s" | ||
unsupportedExtension = "Unsupported extension: %s" | ||
) | ||
|
||
type exts []string | ||
|
||
var ( | ||
// SupportedExts is a list of supported extensions | ||
SupportedExts exts = []string{ | ||
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. map使ったほうが楽そうです。 |
||
".jpg", ".jpeg", ".JPG", ".JPEG", | ||
".png", ".PNG", | ||
".gif", ".GIF", | ||
".bmp", ".BMP", | ||
".tiff", ".TIFF", | ||
} | ||
) | ||
|
||
// VerifySupportedExt verifies that the extension is supported. | ||
func VerifySupportedExt(ext string) error { | ||
var isValidExt bool | ||
|
||
for _, e := range SupportedExts { | ||
if e == ext { | ||
isValidExt = true | ||
} | ||
} | ||
if !isValidExt { | ||
return fmt.Errorf(unsupportedExtension, ext) | ||
} | ||
return nil | ||
} | ||
|
||
// Conv converts the image file to tarExt format. | ||
func Conv(path, tarExt string) error { | ||
src := path | ||
dst := path[:len(path)-len(filepath.Ext(path))] + tarExt | ||
|
||
log.Printf("Source file: %s", src) | ||
log.Printf("Target file: %s", dst) | ||
|
||
sf, err := os.Open(src) | ||
if err != nil { | ||
return fmt.Errorf(canNotOpenImageFile, src) | ||
} | ||
defer sf.Close() | ||
|
||
df, err := os.Create(dst) | ||
if err != nil { | ||
return fmt.Errorf(canNotWriteImageFile, dst) | ||
} | ||
defer df.Close() | ||
|
||
img, _, err := image.Decode(sf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch strings.ToLower(filepath.Ext(dst)) { | ||
case ".png": | ||
err = png.Encode(df, img) | ||
case ".jpeg", ".jpg": | ||
err = jpeg.Encode(df, img, &jpeg.Options{Quality: jpeg.DefaultQuality}) | ||
case ".gif": | ||
err = gif.Encode(df, img, &gif.Options{NumColors: 256, Quantizer: nil, Drawer: nil}) | ||
case ".bmp": | ||
err = bmp.Encode(df, img) | ||
case ".tiff": | ||
err = tiff.Encode(df, img, nil) | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf(canNotWriteImageFile, dst) | ||
} | ||
|
||
return nil | ||
} |
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.
名前付き戻り値はドキュメンテーションまたはdefer内で戻り値を変更したい場合以外は基本的には用いないほうが良いです。