From 9e68134ba17d0d9d01c54600ec4842b13a6d57bc Mon Sep 17 00:00:00 2001 From: onadrog Date: Sun, 25 Dec 2022 09:50:41 +0100 Subject: [PATCH] core: Base command, auto generate frontmatter --- .github/workflows/release.yaml | 30 +++++++++++++++++ .gitignore | 23 +++++++++++++ .goreleaser.yaml | 36 ++++++++++++++++++++ LICENSE | 21 ++++++++++++ README.md | 30 +++++++++++++++++ cmd/root.go | 54 ++++++++++++++++++++++++++++++ go.mod | 10 ++++++ go.sum | 10 ++++++ helpers/files.go | 60 ++++++++++++++++++++++++++++++++++ helpers/files_test.go | 26 +++++++++++++++ main.go | 28 ++++++++++++++++ 11 files changed, 328 insertions(+) create mode 100644 .github/workflows/release.yaml create mode 100644 .gitignore create mode 100644 .goreleaser.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 helpers/files.go create mode 100644 helpers/files_test.go create mode 100644 main.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..fc9fc1f --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,30 @@ +name: goreleaser + +on: + push: + # run only against tags + tags: + - '*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - run: git fetch --force --tags + - uses: actions/setup-go@v3 + with: + go-version: '>=1.19.4' + cache: true + - uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f2dc77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..b26ce0f --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,36 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' +project_name: astroadd +# modelines, feel free to remove those if you don't want/use them: +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..74fad12 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright © 2023 Sébastien Gordano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..37873be --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Astroadd + +A go cli for astro. +Create page with auto generated frontmatter. + +## Command + +### Usage + +Make sure you run the command inside the root directory of your site projet. +It will create a file in the src/pages path. + +```bash +# $ astroadd [path] + +$ astroadd posts/my-blog-post.md +``` +### output + +```markdown + +--- +title : "My blog post" +pubDate: 2023-01-02T16:46:46+01:00 +description: "" +tags: [] +draft: true +--- +``` + diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..c75f4e1 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,54 @@ +/* +Copyright © 2023 Sébastien Gordano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "github.com/onadrog/astroadd-cli/helpers" + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "astroadd [path]", + Short: "Create a new file", + Long: `Create a new file. + Goes to src/pages + Based on the given path. + Automatically add the frontmatter (title, date, draft). + Ensure you run this within the root directory of your site.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + helpers.WriteContent(args[0]) + }, +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..398c0ab --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/onadrog/astroadd-cli + +go 1.18 + +require github.com/spf13/cobra v1.6.1 + +require ( + github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..442875a --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helpers/files.go b/helpers/files.go new file mode 100644 index 0000000..50f44ce --- /dev/null +++ b/helpers/files.go @@ -0,0 +1,60 @@ +package helpers + +import ( + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + "time" +) + +// Get the os platform separator +var sep = os.PathSeparator +var basePath = fmt.Sprintf("src%cpages%c", sep, sep) + +// Return the title without dashes and the directory path for the given path. +// Or an error +func Sanitize(path string) (title string, err error) { + dir, title := filepath.Split(path) + ext := filepath.Ext(title) + title = strings.ReplaceAll(title, ext, "") + + err = os.MkdirAll(fmt.Sprintf("%s%s", basePath,dir), 0750) + if err != nil && !os.IsExist(err) { + return "", err + } + + re := regexp.MustCompile(`_|-|/|\\|\W`) + title = re.ReplaceAllString(title, " ") + title = strings.ToUpper(title[:1]) + title[1:] + return title, nil +} + +func WriteContent(path string) { + + title, err := Sanitize(path) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + date := time.Now().Format(time.RFC3339) + content := []byte( + fmt.Sprintf(`--- +title: "%s" +pubDate: %s +description: "" +tags: [] +draft: true +---`, + title, date)) + + err = os.WriteFile(fmt.Sprintf("%s%s", basePath, path), content, 0644) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + out := fmt.Sprintf("File successfully created in %s%s",basePath, path) + fmt.Println(out) +} diff --git a/helpers/files_test.go b/helpers/files_test.go new file mode 100644 index 0000000..d7bc955 --- /dev/null +++ b/helpers/files_test.go @@ -0,0 +1,26 @@ +package helpers + +import "testing" + +func TestSanitizer(t *testing.T) { + + cases := []struct { + arg string + expected string + }{ + {"pages/posts.md", "Posts"}, + {"pages/posts-1.md", "Posts 1"}, + {"pages/posts_2.md", "Posts 2"}, + {"pages/posts@3.md", "Posts 3"}, + {"pages/posts_blog@4.md", "Posts blog 4"}, + {"pages/posts_blog@/4.md", "4"}, + } + + for _, tc := range cases { + actual,_ := Sanitize(tc.arg) + if actual != tc.expected { + t.Errorf("Expected %s, got %s", tc.expected, actual) + } + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..d7b9310 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +/* +Copyright © 2023 Sébastien Gordano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package main + +import "github.com/onadrog/astroadd-cli/cmd" + +func main() { + cmd.Execute() +}