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

Kadai2-dshirae #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions kadai1/dshirae/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 開発内容

## 課題1

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

### 次の仕様を満たすコマンドを作って下さい
- ディレクトリを指定する
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト)
- ディレクトリ以下は再帰的に処理する
- 変換前と変換後の画像形式を指定できる(オプション)

### 以下を満たすように開発してください
- mainパッケージと分離する
- 自作パッケージと標準パッケージと準標準パッケージのみ使う
- 準標準パッケージ:golang.org/x以下のパッケージ
- ユーザ定義型を作ってみる
- GoDocを生成してみる

## 提出内容

`$ chpics dirname [-i=imgs -o=imgs]`

### default動作
指定されたdirectory内に含まれる全てのjpg画像をpng画像に変換する

### option
- imgs : jpg/png/gif

###

## 課題の提出方法

```
1回目の課題を提出する場合は次のようにコードを書いて下さい。

ブランチ名をkadai1-tenntennのようにする
kadai1/tenntennのようにディレクトリを作る
READMEに説明や文章による課題の回答を書く
PRを送る

※FBには時間がかかる可能性があります。
```
35 changes: 35 additions & 0 deletions kadai1/dshirae/chimgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"

"./convert"
)

// e.g.) chimgs dir [-i=jpg/png] [-o=png/jpg]
func main() {

var (
src = flag.String("i", "jpeg", "string flag")
dest = flag.String("o", "png", "string flag")
)
flag.Parse()

if (flag.NArg() == 0) && (flag.NFlag() == 0) {
fmt.Println("Usage: chimgs DIR [-i=imgext] [-o=imgext]")
Copy link
Member

Choose a reason for hiding this comment

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

標準エラー出力にだす

os.Exit(1)
}
dirname := flag.Arg(0)

filepath.Walk(dirname,
Copy link
Member

Choose a reason for hiding this comment

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

エラー処理

func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
err = convert.PixFile(path, *src, *dest)
return err
})
}
71 changes: 71 additions & 0 deletions kadai1/dshirae/convert/PixFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package convert

/*
画像変換コマンド

次の仕様を満たすコマンドを作って下さい

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

以下を満たすように開発してください

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

import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
)

// picture file 変換関数
func PixFile(org string, src string, dest string) error {

// open file
file, err := os.Open(org)
if err != nil {
return err
}
defer file.Close()

// image reading.
img, format, err := image.Decode(file)
if err != nil {
// not image
return err
}

// 元ファイルが指定外ならスキップ
if format != src {
return nil
}

// 出力先ファイル
savefile, err := os.Create(org + "." + dest)
if err != nil {
return err
}
defer savefile.Close()
Copy link
Member

Choose a reason for hiding this comment

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

エラー処理


switch dest {
case "jpg", "jpeg":
opts := &jpeg.Options{}
jpeg.Encode(savefile, img, opts)
case "png":
png.Encode(savefile, img)
case "gif":
opts := &gif.Options{}
gif.Encode(savefile, img, opts)
}

return nil
}
23 changes: 23 additions & 0 deletions kadai2/dshirae/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 課題2

## テストを書いてみよう

### 1回目の宿題のテストを作ってみてください
- テストのしやすさを考えてリファクタリングしてみる
課題1に実装しなかった、`ユーザ定義型を作ってみる` を実装した
- テストのカバレッジを取ってみる
課題提出時コードにて80% のカバレッジ
`$ go test -coverprofile=cover.out -run ''`
`$ go tool cover -html=cover.out`
- テーブル駆動テストを行う
実装しました
- テストヘルパーを作ってみる
実装したが、うまく使えてない気がする。(再調査)

## io.Readerとio.Writer

### io.Readerとio.Writerについて調べてみよう
- 標準パッケージでどのように使われているか
[WIP]
- io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる
抽象化された事でOS間の違いが吸収される。標準出力(console)など
36 changes: 36 additions & 0 deletions kadai2/dshirae/chimgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"

"./convert"
Copy link
Member

Choose a reason for hiding this comment

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

相対パスにしない

)

// e.g.) chimgs dir [-i=jpg/png] [-o=png/jpg]
func main() {

var pix convert.PixConv

pix.Src = flag.String("i", "jpeg", "string flag")
pix.Dest = flag.String("o", "png", "string flag")
flag.Parse()

if (flag.NArg() == 0) && (flag.NFlag() == 0) {
fmt.Println("Usage: chimgs DIR [-i=imgext] [-o=imgext]")
os.Exit(1)
}
dirname := flag.Arg(0)

filepath.Walk(dirname,
func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
pix.Path = path
err = convert.PixFile(data)
return err
})
}
77 changes: 77 additions & 0 deletions kadai2/dshirae/convert/PixFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package convert

/*
画像変換コマンド

次の仕様を満たすコマンドを作って下さい

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

以下を満たすように開発してください

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

import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
)

type PixConv struct {
Path string
Src string
Dest string
}

// picture file 変換関数
func PixFile(filedata PixConv) error {

// open file
file, err := os.Open(filedata.Path)
if err != nil {
return err
}
defer file.Close()

// image reading.
img, format, err := image.Decode(file)
if err != nil {
// not image
return err
}

// 元ファイルが指定外ならスキップ
if format != filedata.Src {
return nil
}

// 出力先ファイル
savefile, err := os.Create(filedata.Path + "." + filedata.Dest)
if err != nil {
return err
}
defer savefile.Close()

switch filedata.Dest {
case "jpg", "jpeg":
opts := &jpeg.Options{}
jpeg.Encode(savefile, img, opts)
case "png":
png.Encode(savefile, img)
case "gif":
opts := &gif.Options{}
gif.Encode(savefile, img, opts)
}

return nil
}
28 changes: 28 additions & 0 deletions kadai2/dshirae/convert/PixFile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package convert

import "testing"

// picture file 変換テスト
func TestPixFile(t *testing.T) {

testTables := []PixConv{
{"./test.jpg", "jpeg", "png"},
{"./test.jpg", "jpeg", "gif"},
{"./test.png", "png", "jpeg"},
{"./test.png", "png", "gif"},
{"./test.gif", "gif", "jpeg"},
{"./test.gif", "gif", "png"},
}

for _, testpix := range testTables {
testPixFile(t, testpix)
}
}

func testPixFile(t *testing.T, testpix PixConv) {
t.Helper()
err := PixFile(testpix)
if err != nil {
t.Fatalf("failed test %#v", err)
}
}