Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature&refactor(blog resource):add asciinema and polish project layout
Signed-off-by: Dong Gang <dong.gang@daocloud.io>
- Loading branch information
Dong Gang
committed
Jul 8, 2020
1 parent
da5ef1b
commit aafd97c
Showing
9 changed files
with
363 additions
and
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
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
Oops, something went wrong.