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

remove config.yaml #10

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 46 additions & 9 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ cd note-cli-demo

Create directory. a6b420c6-9bb2-4060-869c-20c171fc9827
Create file. a6b420c6-9bb2-4060-869c-20c171fc9827.md
Create file. config.yaml
```

```
.
└── a6b420c6-9bb2-4060-869c-20c171fc9827
├── config.yaml
└── a6b420c6-9bb2-4060-869c-20c171fc9827.md
```

- ```a6b420c6-9bb2-4060-869c-20c171fc9827.md``` 記事ファイル。ファイル名はディレクトリ名と同じで今回はランダム値(UUID)を使用しています。
- ```config.yaml``` 設定ファイル。記事のタイトルや著者名などを設定するファイルです。
- ~~```config.yaml``` 設定ファイル。記事のタイトルや著者名などを設定するファイルです。~~

```yaml:config.yaml
title: article title
author: your name
```

**config.yamlは廃止しました。記事タイトルや著者名などの設定情報は記事markdownファイルのメタデータとして記載するように変更されています。**

3. 記事画像を生成する。

以下のコマンドを実行することで```output.png```が生成されます。
Expand Down Expand Up @@ -136,9 +136,9 @@ Complete generate OGP image
```create article```コマンドを実行するとユニークなランダム値(UUID)を使用してディレクトリを作成します。ディレクトリ内には以下のファイルも併せて作成され配置されます。

- ```<directory name>.md``` 記事ファイル。投稿したい記事の内容をこのファイル内に書き込んでいきます。
- ```config.yaml``` 記事や画像ファイルの生成に関する設定をこのファイルで行います。
- ~~```config.yaml``` 記事や画像ファイルの生成に関する設定をこのファイルで行います。~~

### config.yaml
### ~~config.yaml~~

```yaml
title: article title
Expand All @@ -150,6 +150,26 @@ author: your name
|title|string|生成する画像に載せる記事タイトル|
|author|string|記事執筆者|

### \<directory name>.md

マークダウンファイルは以下のようなメタデータを持ちます。画像の出力には```title```と```author```の項目が使用されます。

```yaml:article.md
---
title: ""
tags: []
date: "2023-09-29"
author: ""
---
```

| Field | Type | Description |
| --- | --- | --- |
|title|string|生成する画像に載せる記事タイトル|
|tags|[]string|記事に紐づけるタグ|
|date|string|ファイル作成日|
|author|string|記事執筆者|

### ```--time(-t)```

このフラグを付けることでデフォルトのUUIDではなく、現在時刻でディレクトリおよびファイルを作成することができます。現在時刻はコマンド実行のOSのタイムゾーンに依存し、```YYYY-mm-dd```のフォーマットで生成されます。
Expand All @@ -158,7 +178,6 @@ author: your name
% note-cli create article -t
Create directory. 2023-09-08
Create file. 2023-09-08.md
Create file. config.yaml
```

既にディレクトリが存在している場合、```YYYY-mm-dd-{number}```という形式でnumberをインクリメントしてディレクトリを作成します。
Expand All @@ -167,7 +186,6 @@ Create file. config.yaml
% note-cli create article -t
Create directory. 2023-09-08-2
Create file. 2023-09-08-2.md
Create file. config.yaml
```

### ```--name(-n)```
Expand All @@ -178,12 +196,31 @@ Create file. config.yaml
% note-cli create article -n article-A
Create directory. article-A
Create file. article-A.md
Create file. config.yaml
```

### ```--author(-a)```

このフラグを付けることで記事著者名を事前に渡すことができます。

```
% go run main.go create article -a Yamanaka.J
Create directory. cc4ab85a-2aa7-48a6-8472-3b36ef4778fa
Create file. cc4ab85a-2aa7-48a6-8472-3b36ef4778fa/cc4ab85a-2aa7-48a6-8472-3b36ef4778fa.md

% cat cc4ab85a-2aa7-48a6-8472-3b36ef4778fa/cc4ab85a-2aa7-48a6-8472-3b36ef4778fa.md
---
title: ""
tags: []
date: "2023-09-29"
author: "Yamanaka.J"
---
```

## create image

```create image```コマンドを実行することで[OGP](https://ogp.me/)画像風の画像を生成することができます。画像の生成にはカレントディレクトリに<a href="#configyaml">config.yaml</a>が存在している必要があります。もし、config.yamlが存在していなかった場合はコマンドが失敗します。
```create image```コマンドを実行することで[OGP](https://ogp.me/)画像風の画像を生成することができます。~~画像の生成にはカレントディレクトリに<a href="#configyaml">config.yaml</a>が存在している必要があります。もし、config.yamlが存在していなかった場合はコマンドが失敗します。~~

**config.yamlは廃止となっています。代わりに、markdownファイルのメタデータが存在している必要があります。メタデータを読み取ることができなかった場合、互換性を保つためにconfig.yamlを探し、それでもなければコマンドは失敗します。**

```
% note-cli create image
Expand Down
55 changes: 46 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ cd note-cli-demo

Create directory. a6b420c6-9bb2-4060-869c-20c171fc9827
Create file. a6b420c6-9bb2-4060-869c-20c171fc9827.md
Create file. config.yaml
```

```
.
└── a6b420c6-9bb2-4060-869c-20c171fc9827
├── config.yaml
└── a6b420c6-9bb2-4060-869c-20c171fc9827.md
```

- ```a6b420c6-9bb2-4060-869c-20c171fc9827.md``` Article file. The file name is the same as the directory name, this time using a random value (UUID).
- ```config.yaml``` Config file. This file is used to set the title of the article, the name of the author, etc.
- ~~```config.yaml``` Config file. This file is used to set the title of the article, the name of the author, etc.~~

```yaml:config.yaml
title: article title
author: your name
```

**config.yaml is obsolete. Configuration information such as article title and author name has been changed to be included as metadata in the article markdown file.**

3. Create article image.

The following command will generate ``output.png``.
Expand Down Expand Up @@ -136,9 +136,9 @@ Icons can also be placed on the image by providing an icon image. You can also c
```create article``` command creates a directory with a unique random value (UUID). The following files will also be created and placed in the directory.

- ```<directory name>.md``` Article file.The content of the article you wish to submit will be written in this file.
- ```config.yaml``` This file contains settings related to the generation of articles and image files.
- ~~```config.yaml``` This file contains settings related to the generation of articles and image files.~~

### config.yaml
### ~~config.yaml~~

```yaml
title: article title
Expand All @@ -150,6 +150,26 @@ author: your name
|title|string|Article Title|
|author|string|Article Author|

### \<directory name>.md

The markdown file will have the following metadata The ``title`` and ``author`` fields are used for image output.

```yaml:article.md
---
title: ""
tags: []
date: "2023-09-29"
author: ""
---
```

| Field | Type | Description |
| --- | --- | --- |
|title|string|Article Title|
|tags|[]string|tags|
|date|string|File created day|
|author|string|Article Author|

### ```--time(-t)```

This flag allows directories and files to be created with the current time instead of the default UUID. The current time depends on the time zone of the operating system under which the command is executed and is generated in the format ``YYYY-mm-dd``.
Expand All @@ -158,7 +178,6 @@ This flag allows directories and files to be created with the current time inste
% note-cli create article -t
Create directory. 2023-09-08
Create file. 2023-09-08.md
Create file. config.yaml
```

If the directory already exists, it creates the directory by incrementing number in the form ``YYYY-mm-dd-{number}``.
Expand All @@ -167,7 +186,6 @@ If the directory already exists, it creates the directory by incrementing number
% note-cli create article -t
Create directory. 2023-09-08-2
Create file. 2023-09-08-2.md
Create file. config.yaml
```

### ```--name(-n)```
Expand All @@ -178,12 +196,31 @@ This flag allows you to create a directory with an arbitrary name. **If the dire
% note-cli create article -n article-A
Create directory. article-A
Create file. article-A.md
Create file. config.yaml
```

### ```--author(-a)```

This flag allows the name of the article author to be passed in advance.

```
% go run main.go create article -a Yamanaka.J
Create directory. cc4ab85a-2aa7-48a6-8472-3b36ef4778fa
Create file. cc4ab85a-2aa7-48a6-8472-3b36ef4778fa/cc4ab85a-2aa7-48a6-8472-3b36ef4778fa.md

% cat cc4ab85a-2aa7-48a6-8472-3b36ef4778fa/cc4ab85a-2aa7-48a6-8472-3b36ef4778fa.md
---
title: ""
tags: []
date: "2023-09-29"
author: "Yamanaka.J"
---
```

## create image

Image like [OGP](https://ogp.me/) can be generated by executing the ```create image``` command.The <a href="#configyaml">config.yaml</a> must exist in the current directory to generate images.If config.yaml does not exist, the command will fail.
Image like [OGP](https://ogp.me/) can be generated by executing the ```create image``` command.~~The <a href="#configyaml">config.yaml</a> must exist in the current directory to generate images.If config.yaml does not exist, the command will fail.~~

**config.yaml is obsolete. Instead, the metadata for the markdown file must be present. If the metadata cannot be read, config.yaml is searched for compatibility, and if it is still missing, the command will fail.**

```
% note-cli create image
Expand Down
4 changes: 3 additions & 1 deletion cmd/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can also specify the -t(--time) option to make the current timestamp the fil
If nothing is specified, the file is created with a unique file name by UUID.
`,
Args: cobra.NoArgs,
RunE: run.CreateArticleFunc(&timeFlag, &name),
RunE: run.CreateArticleFunc(&timeFlag, &name, &autor),
Example: `note-cli create article
note-cli create article --name article-a
note-cli create article -t`,
Expand All @@ -24,11 +24,13 @@ note-cli create article -t`,
var (
timeFlag bool
name string
autor string
)

func init() {
articleCmd.Flags().BoolVarP(&timeFlag, "time", "t", false, "Create directory and file names with the current timestamp")
articleCmd.Flags().StringVarP(&name, "name", "n", "", "Create a directory with the specified name")
articleCmd.Flags().StringVarP(&autor, "author", "a", "", "Author name")

articleCmd.MarkFlagsMutuallyExclusive("time", "name")

Expand Down
1 change: 1 addition & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var imageCmd = &cobra.Command{
Use: "image",
Short: "Create title image.",
Long: `Create title image`,
Args: cobra.NoArgs,
RunE: run.CreateImageFunc(&templateNo, &iconPath, &outputPath),
}

Expand Down
22 changes: 22 additions & 0 deletions internal/file/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package file

import (
"bytes"
"fmt"
"io"
"os"
)

func Exist(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}

func Extract(r io.Reader, start, end string) ([]byte, error) {
// read data
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}

// extract content
startBytes := []byte(start)
endBytes := []byte(end)
startIndex := bytes.Index(b, startBytes)
endIndex := bytes.Index(b[startIndex+len(startBytes):], endBytes)
if startIndex == -1 || endIndex == -1 {
return nil, fmt.Errorf("could not find target content. start: %s end: %s", start, end)
}

return b[startIndex+len(startBytes) : startIndex+len(startBytes)+endIndex], nil
}