generated from sters/go-project-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
867 additions
and
17 deletions.
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
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,31 @@ | ||
builds: | ||
- main: ./cmd/publish-reserved-content/main.go | ||
env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- darwin | ||
- windows | ||
goarch: | ||
- 386 | ||
- amd64 | ||
- main: ./cmd/find-often-tag-with/main.go | ||
env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- darwin | ||
- windows | ||
goarch: | ||
- 386 | ||
- amd64 | ||
- main: ./cmd/find-similar-tags/main.go | ||
env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- darwin | ||
- windows | ||
goarch: | ||
- 386 | ||
- amd64 |
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
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
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,103 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/morikuni/failure" | ||
"github.com/sters/hugo-utilities/publish" | ||
) | ||
|
||
func abs(p string) string { | ||
f, _ := filepath.Abs(p) | ||
return f | ||
} | ||
|
||
func dirwalk(dir string) ([]string, error) { | ||
dir = abs(dir) | ||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
return nil, failure.Wrap(err) | ||
} | ||
|
||
paths := make([]string, 0, len(files)) | ||
for _, file := range files { | ||
if file.IsDir() { | ||
child, err := dirwalk(filepath.Join(dir, file.Name())) | ||
if err != nil { | ||
return nil, failure.Wrap(err) | ||
} | ||
paths = append(paths, child...) | ||
continue | ||
} | ||
|
||
p, err := filepath.Abs(filepath.Join(dir, file.Name())) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%+v", err) | ||
continue | ||
} | ||
paths = append(paths, p) | ||
} | ||
|
||
return paths, nil | ||
} | ||
|
||
func main() { | ||
var ( | ||
reservedKey string | ||
draftKey string | ||
basePath string | ||
imageReplaceFormat string | ||
) | ||
flag.StringVar(&reservedKey, "reservedKey", "reserved", "hugo content's reservation bool key") | ||
flag.StringVar(&draftKey, "draftKey", "draft", "hugo content's draft bool key") | ||
flag.StringVar(&basePath, "basePath", "", "hugo content's root directory") | ||
flag.StringVar(&imageReplaceFormat, "imageReplaceFormat", "![]($1)", "how to replace image url") | ||
flag.Parse() | ||
|
||
if reservedKey == "" { | ||
fmt.Fprintf(os.Stderr, "reservedKey is required.\n") | ||
os.Exit(1) | ||
} | ||
if draftKey == "" { | ||
fmt.Fprintf(os.Stderr, "draftKey is required.\n") | ||
os.Exit(1) | ||
} | ||
if basePath == "" { | ||
fmt.Fprintf(os.Stderr, "basePath is required.\n") | ||
os.Exit(1) | ||
} | ||
if imageReplaceFormat == "" { | ||
fmt.Fprintf(os.Stderr, "imageReplaceFormat is required.\n") | ||
os.Exit(1) | ||
} | ||
|
||
dirs, err := dirwalk(basePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
p := publish.New(reservedKey, draftKey, imageReplaceFormat) | ||
for _, filepath := range dirs { | ||
err := p.CheckReservedAndPublish(filepath) | ||
c, ok := failure.CodeOf(err) | ||
if !ok { | ||
// = no error | ||
fmt.Fprintf(os.Stdout, "%s is published.\n", filepath) | ||
continue | ||
} | ||
|
||
switch c { | ||
case publish.ErrContentIsReservedButNotDraft: | ||
fmt.Fprintf(os.Stderr, "%s is reserved but not draft.\n", filepath) | ||
case publish.ErrFileContentMismatch: | ||
fmt.Fprintf(os.Stderr, "%s is maybe breaking content.\n", filepath) | ||
case publish.ErrContentIsNotTheTimeYet: | ||
fmt.Fprintf(os.Stderr, "%s is still waiting.\n", filepath) | ||
} | ||
} | ||
} |
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 content | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
|
||
yaml "github.com/goccy/go-yaml" | ||
"github.com/morikuni/failure" | ||
) | ||
|
||
// FrontMatter is metadata for MarkdownContent. | ||
type FrontMatter map[string]interface{} | ||
|
||
func (f FrontMatter) getField(fieldname string) interface{} { | ||
field, ok := f[fieldname] | ||
if !ok { | ||
return nil | ||
} | ||
return field | ||
} | ||
|
||
func (f FrontMatter) GetInt(fieldname string) int { | ||
r, ok := f.getField(fieldname).(int) | ||
if !ok { | ||
return 0 | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (f FrontMatter) GetString(fieldname string) string { | ||
r, ok := f.getField(fieldname).(string) | ||
if !ok { | ||
return "" | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (f FrontMatter) GetStrings(fieldname string) []string { | ||
ss, ok := f.getField(fieldname).([]string) | ||
if ok { | ||
return ss | ||
} | ||
|
||
si, ok := f.getField(fieldname).([]interface{}) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
result := make([]string, 0, len(si)) | ||
for _, s := range si { | ||
r, ok := s.(string) | ||
if !ok { | ||
continue | ||
} | ||
|
||
result = append(result, r) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// MarkdownContent for hugo. | ||
// https://gohugo.io/content-management/formats/ | ||
type MarkdownContent struct { | ||
// FrontMatter is metadata for this content | ||
FrontMatter FrontMatter | ||
// Body for this content | ||
Body string | ||
} | ||
|
||
// Dump to string from this content. | ||
func (m *MarkdownContent) Dump() (string, error) { | ||
meta, err := yaml.Marshal(m.FrontMatter) | ||
if err != nil { | ||
return "", failure.Wrap(err, failure.WithCode(ErrFileContentMismatch)) | ||
} | ||
|
||
return fmt.Sprintf(`%s%s | ||
%s%s`, | ||
hugoSeparator, strings.TrimSpace(string(meta)), hugoSeparator, m.Body), nil | ||
} | ||
|
||
// see https://gohugo.io/content-management/front-matter/#front-matter-formats | ||
const ( | ||
hugoSeparator = "---\n" | ||
hugoContentLength = 3 | ||
) | ||
|
||
// ErrFileContentMismatch on specified filepath. | ||
var ErrFileContentMismatch = failure.StringCode("file content mismatch") | ||
|
||
// ParseMarkdownWithYaml from any reader to make MarkdownContent struct. | ||
func ParseMarkdownWithYaml(r io.Reader) (*MarkdownContent, error) { | ||
raw, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return nil, failure.Wrap(err) | ||
} | ||
|
||
content := strings.Split(string(raw), hugoSeparator) | ||
if len(content) < hugoContentLength { | ||
return nil, failure.New(ErrFileContentMismatch) | ||
} | ||
|
||
c := &MarkdownContent{ | ||
Body: strings.Join(content[2:], hugoSeparator), | ||
} | ||
|
||
if err := yaml.Unmarshal([]byte(content[1]), &c.FrontMatter); err != nil { | ||
return nil, failure.Wrap(err, failure.WithCode(ErrFileContentMismatch)) | ||
} | ||
|
||
return c, nil | ||
} |
Oops, something went wrong.