Skip to content

Commit

Permalink
feature&refactor(blog resource):add asciinema and polish project layout
Browse files Browse the repository at this point in the history
Signed-off-by: Dong Gang <dong.gang@daocloud.io>
  • Loading branch information
Dong Gang committed Jul 8, 2020
1 parent da5ef1b commit aafd97c
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 187 deletions.
60 changes: 60 additions & 0 deletions cmd/workflow/blog/asciinema.go
@@ -0,0 +1,60 @@
package blog

import (
"hack/cmd/util"
"hack/pkg"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func NewUnsynchronizedAsciinemas(cfg util.BlogConfig) *cobra.Command {
cobra := &cobra.Command{
Use: "asciinema",
Short: "list unsynchronized asciinema",
Long: "list unsynchronized asciinema",
Run: func(cmd *cobra.Command, args []string) {
RunUnsynchronized(cfg)
},
}
return cobra
}

func RunUnsynchronized(cfg util.BlogConfig) {
asciinemas := ListDraftAsciinemas(cfg.BlogSourceDir, cfg.ImageRepoDir)
a := pkg.Asciinemas(asciinemas)
a.Print(os.Stdout)
}

//ListDraftImage list unsynchronized images
func ListDraftAsciinemas(srcDir, targetDir string) []pkg.Asciinema {
images := make([]pkg.Asciinema, 0)
filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
ok := IsAsciinema(path)
if !info.IsDir() && ok {
srcAscii, err := pkg.NewAsciinema(path)
if err != nil {
logrus.Warn(err)
return nil
}

// check if blog directory exists
blogDir := filepath.Join(targetDir, srcAscii.Blog)
if !dirExists(blogDir) {
err := os.MkdirAll(blogDir, 0777)
if err != nil {
logrus.Errorf("failed create blog dir :%s", err)
return nil
}
}
targetAsciinema := filepath.Join(blogDir, filepath.Base(srcAscii.Path))
if !fileExists(targetAsciinema) {
images = append(images, *srcAscii)
}
}
return nil
})
return images
}
1 change: 1 addition & 0 deletions cmd/workflow/blog/blog.go
Expand Up @@ -15,5 +15,6 @@ func NewBlogCommand(cfg util.BlogConfig) *cobra.Command {
cmd.AddCommand(NewSyncImage(cfg))
cmd.AddCommand(NewPublishCommand(cfg))
cmd.AddCommand(NewImageScan(cfg))
cmd.AddCommand(NewUnsynchronizedAsciinemas(cfg))
return cmd
}
40 changes: 37 additions & 3 deletions cmd/workflow/blog/image_scan.go
Expand Up @@ -2,8 +2,11 @@ package blog

import (
"hack/cmd/util"
"hack/pkg"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -13,14 +16,45 @@ func NewImageScan(cfg util.BlogConfig) *cobra.Command {
Short: "list unsynchronized image",
Long: "list unsynchronized image",
Run: func(cmd *cobra.Command, args []string) {
Run(cfg)
RunImageScan(cfg)
},
}
return cmd
}

func Run(cfg util.BlogConfig) {
func RunImageScan(cfg util.BlogConfig) {
images := ListDraftImage(cfg.BlogSourceDir, cfg.ImageRepoDir)
i := Images(images)
i := pkg.Images(images)
i.Print(os.Stdout)
}

//ListDraftImage list unsynchronized images
func ListDraftImage(srcDir, targetDir string) []pkg.Image {
images := make([]pkg.Image, 0)
filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
ok, imageType := pkg.IsImage(path)
if !info.IsDir() && ok {
srcImg, err := pkg.NewImage(path, imageType)
if err != nil {
logrus.Warn(err)
return nil
}

// check if blog directory exists
blogDir := filepath.Join(targetDir, srcImg.Blog)
if !dirExists(blogDir) {
err := os.MkdirAll(blogDir, 0777)
if err != nil {
logrus.Errorf("failed create blog dir :%s", err)
return nil
}
}
targetImage := filepath.Join(targetDir, srcImg.NameWithBlog())
if !fileExists(targetImage) {
images = append(images, *srcImg)
}
}
return nil
})
return images
}

0 comments on commit aafd97c

Please sign in to comment.