Skip to content
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

Kadai1 rnakamine #26

Closed
wants to merge 25 commits into from
Closed

Kadai1 rnakamine #26

wants to merge 25 commits into from

Conversation

rnakamine
Copy link

@rnakamine rnakamine commented Feb 17, 2021

課題1

画像変換コマンドを作ろう

仕様

  • ディレクトリを指定する
  • 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト)
  • ディレクトリ以下は再帰的に処理する
  • 変換前と変換後の画像形式を指定できる(オプション)

要件

  • mainパッケージと分離する
  • 自作パッケージと標準パッケージと準標準パッケージのみ使う
    • 準標準パッケージ:golang.org/x以下のパッケージ
  • ユーザ定義型を作ってみる
  • GoDocを生成してみる
  • Go Modulesを使ってみる

実行結果

  • フォーマット指定なし
$ go run main.go cli.go -dir testdata
Converting.. testdata/pSA001.jpg -> testdata/pSA001.png
Converting.. testdata/pSC043.jpg -> testdata/pSC043.png
Converting.. testdata/pSD002.jpg -> testdata/pSD002.png
Converting.. testdata/sub/pFA005.jpg -> testdata/sub/pFA005.png
Converting.. testdata/sub/pSB001.jpg -> testdata/sub/pSB001.png
  • pngからgifに変換
$ go run main.go cli.go -dir testdata -from png -to gif
Converting.. testdata/pSA001.png -> testdata/pSA001.gif
Converting.. testdata/pSC043.png -> testdata/pSC043.gif
Converting.. testdata/pSD002.png -> testdata/pSD002.gif
Converting.. testdata/sub/pFA005.png -> testdata/sub/pFA005.gif
Converting.. testdata/sub/pSB001.png -> testdata/sub/pSB001.gif

GoDoc

image

return err
}

if err = os.Remove(i.FromPath); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLIツールとして、いきなり元ファイル消してくるのはちょっと怖いかも......

Copy link
Author

@rnakamine rnakamine Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebiiim
なるほどです!元ファイルも残す感じにしますね🙆‍♂️

-delオプションをつけて削除するかユーザー側で判断させるようにしました!

kadai1/cli.go Outdated
flags.StringVar(&to, "to", "png", "Extensions after conversion")

if err := flags.Parse(args[1:]); err != nil {
fmt.Fprintln(c.errStream, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fprintlnはファイルへの書き込みを行うので失敗してerrorを返す事があります。
そこのハンドリングをすべきなのと、後にエラーハンドリングの漏れをチェックしようとしたとき(それ用のerrcheckというツールがあります)全て引っかかってしまうので、Runのリターンとしてerrorを返すようにし、Run関数の外部でerrorの処理をする方が良さそうです。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gosagawa

598737f

cli.Runでエラーがあった場合はreturnでerrorを返すようにして、main関数で処理(fmt.Println()で表示させるだけですが)するようにしてみました!

// GetConvertImages retrieves the images to be converted from the specified directory
func GetConvertImages(dir, from, to string) ([]ConvertImage, error) {
var images []ConvertImage
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filepath.Walkはディレクトリも走査するので、例えばtest.jpgというディレクトリがあった場合に変換対象に含まれてしまいます。あとで画像変換するときに落ちるのですが、この関数内でファイルかどうかはチェックしておいた方が良さそうです。

Copy link
Author

@rnakamine rnakamine Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gosagawa

73c36d8

ディレクトリかどうかのチェックを入れて、ディレクトリでなければ変換対象に含むように修正しました!

@rnakamine rnakamine closed this Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants