Skip to content

Commit

Permalink
フラットパッケージに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Sep 14, 2024
1 parent 9b875b5 commit 0baaf7d
Show file tree
Hide file tree
Showing 25 changed files with 42 additions and 77 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions internal/command/build.go → build.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package command
package vss

import (
"log"

"github.com/bradhe/stopwatch"
"github.com/vssio/go-vss/internal/build"
"github.com/vssio/go-vss/internal/config"
)

type BuildCommand struct {
Expand All @@ -25,13 +23,13 @@ func (c *BuildCommand) Run(args []string) int {
// init stop watch
sw := stopwatch.Start()

config, err := config.LoadConfig()
config, err := LoadConfig()
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}

builder := build.NewBuilder(config)
builder := NewBuilder(config)
err = builder.Run()
if err != nil {
log.Printf("[ERROR] %s", err)
Expand Down
28 changes: 13 additions & 15 deletions internal/build/build.go → builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package build
package vss

import (
"bytes"
Expand All @@ -13,15 +13,14 @@ import (
"github.com/adrg/frontmatter"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/cbroglie/mustache"
"github.com/vssio/go-vss/internal/config"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
)

// Builder is a struct for building a static site.
type Builder struct {
config *config.Config
config *Config

// init in Run()
templateMap map[string]*mustache.Template
Expand All @@ -30,7 +29,7 @@ type Builder struct {
}

// NewBuilder returns a new Builder.
func NewBuilder(config *config.Config) *Builder {
func NewBuilder(config *Config) *Builder {
return &Builder{
config: config,
}
Expand All @@ -43,7 +42,7 @@ func (b Builder) GetDistPath() string {

// ReloadConfig reloads the config file.
func (b *Builder) ReloadConfig() error {
c, err := config.LoadConfig()
c, err := LoadConfig()
if err != nil {
return err
}
Expand All @@ -58,7 +57,7 @@ func (b *Builder) SetBaseUrl(baseURL string) {

// Run builds the static site.
func (b Builder) Run() error {
if err := createDistDir(b.config.Dist); err != nil {
if err := createDistDir(b.config.Dist, true); err != nil {
return err
}

Expand Down Expand Up @@ -321,21 +320,20 @@ func existDir(dir string) bool {
return info.IsDir()
}

func createDistDir(dist string) error {
func createDistDir(dist string, overwrite bool) error {
// TODO: cache dist directory
if existDir(dist) {
if !overwrite {
return errors.New("dist directory already exists")
}
log.Printf("[INFO] re creating dist directory: %s", dist)
if err := os.RemoveAll(dist); err != nil {
return err
}
if err := os.Mkdir(dist, os.ModePerm); err != nil {
return err
}
} else {
log.Printf("[INFO] creating dist directory: %s", dist)
if err := os.Mkdir(dist, os.ModePerm); err != nil {
return err
}
}
log.Printf("[INFO] creating dist directory: %s", dist)
if err := os.Mkdir(dist, os.ModePerm); err != nil {
return err
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/build/build_test.go → builder_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package build
package vss

import (
"fmt"
Expand Down
16 changes: 7 additions & 9 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,40 @@ package vss

import (
"github.com/mitchellh/cli"

"github.com/vssio/go-vss/internal/command"
)

// Commands initializes the Commands factory map.
func initCommands(metaPtr *command.Meta) map[string]cli.CommandFactory {
func initCommands(metaPtr *Meta) map[string]cli.CommandFactory {
if metaPtr == nil {
metaPtr = new(command.Meta)
metaPtr = new(Meta)
}
meta := *metaPtr
meta.Version = version
meta.Revision = revision

all := map[string]cli.CommandFactory{
"build": func() (cli.Command, error) {
return &command.BuildCommand{
return &BuildCommand{
Meta: meta,
}, nil
},
"serve": func() (cli.Command, error) {
return &command.ServeCommand{
return &ServeCommand{
Meta: meta,
}, nil
},
"new": func() (cli.Command, error) {
return &command.NewCommand{
return &NewCommand{
Meta: meta,
}, nil
},
"self update": func() (cli.Command, error) {
return &command.SelfUpdateCommand{
return &SelfUpdateCommand{
Meta: meta,
}, nil
},
"self version": func() (cli.Command, error) {
return &command.SelfVersionCommand{
return &SelfVersionCommand{
Meta: meta,
}, nil
},
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go → config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package vss

import (
"log"
Expand Down
2 changes: 1 addition & 1 deletion internal/command/const_unix.go → const_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !windows

package command
package vss

const binaryName = "vss"
2 changes: 1 addition & 1 deletion internal/command/const_windows.go → const_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build windows

package command
package vss

const binaryName = "vss.exe"
2 changes: 1 addition & 1 deletion internal/build/filedata.go → filedata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package build
package vss

// FileData is a struct for markdown file data.
type FileData struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/build/frontmatter.go → frontmatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package build
package vss

import (
"fmt"
Expand Down
1 change: 0 additions & 1 deletion internal/serve/serve.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/command/meta.go → meta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package vss

// Meta contains the common fields required by all commands.
type Meta struct {
Expand Down
6 changes: 2 additions & 4 deletions internal/command/new.go → new.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package command
package vss

import (
"fmt"
"log"

"github.com/vssio/go-vss/internal/skeleton"
)

type NewCommand struct {
Expand All @@ -26,7 +24,7 @@ func (c *NewCommand) Run(args []string) int {
return 1
}
log.Println("[INFO] generate skeleton started")
err := skeleton.GenerateSkeleton(args[0])
err := GenerateSkeleton(args[0])
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
Expand Down
2 changes: 1 addition & 1 deletion internal/command/self_update.go → self_update.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package vss

import (
"archive/tar"
Expand Down
2 changes: 1 addition & 1 deletion internal/command/self_version.go → self_version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package vss

import (
"fmt"
Expand Down
10 changes: 4 additions & 6 deletions internal/command/serve.go → serve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package vss

import (
"log"
Expand All @@ -8,8 +8,6 @@ import (
"slices"

"github.com/fsnotify/fsnotify"
"github.com/vssio/go-vss/internal/build"
"github.com/vssio/go-vss/internal/config"
)

const port = "8080"
Expand All @@ -30,7 +28,7 @@ func (d htmlDir) Open(name string) (http.File, error) {

type ServeCommand struct {
Meta
builder *build.Builder
builder *Builder
}

func (c *ServeCommand) Help() string {
Expand All @@ -44,7 +42,7 @@ func (c *ServeCommand) Synopsis() string {
func (c *ServeCommand) Run(args []string) int {
log.Printf("[INFO] serve started")

config, err := config.LoadConfig()
config, err := LoadConfig()
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
Expand All @@ -53,7 +51,7 @@ func (c *ServeCommand) Run(args []string) int {
config.BaseUrl = "http://localhost:" + port

// init site
c.builder = build.NewBuilder(config)
c.builder = NewBuilder(config)
err = c.builder.Run()
if err != nil {
log.Printf("[ERROR] %s", err)
Expand Down
29 changes: 3 additions & 26 deletions internal/skeleton/skeleton.go → skeleton.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package skeleton
package vss

import (
"embed"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -29,7 +27,8 @@ func GenerateSkeleton(distDir string) error {
// copyEmbedFiles copies all files in the embed.FS to the destination directory.
func copyEmbedFiles(efs *embed.FS, distDir string) error {
// Create the destination directory if it doesn't exist
if err := createDistDir(distDir); err != nil {
// TODO(zztkm): add a flag to force overwrite the dist directory
if err := createDistDir(distDir, false); err != nil {
return err
}

Expand Down Expand Up @@ -65,25 +64,3 @@ func copyEmbedFiles(efs *embed.FS, distDir string) error {

return nil
}

func createDistDir(dist string) error {
// TODO: cache dist directory
if existDir(dist) {
return errors.New("dist directory already exists")
} else {
log.Printf("[INFO] creating dist directory: %s", dist)
if err := os.MkdirAll(dist, os.ModePerm); err != nil {
return err
}
}
return nil
}

// existDir checks if a directory exists.
func existDir(dir string) bool {
info, err := os.Stat(dir)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
3 changes: 1 addition & 2 deletions vss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"

"github.com/mitchellh/cli"
"github.com/vssio/go-vss/internal/command"
)

const version = "0.11.0"
Expand All @@ -16,7 +15,7 @@ func Version() string {
}

func Run(args []string) int {
metaPtr := new(command.Meta)
metaPtr := new(Meta)
c := &cli.CLI{
Name: "vss",
Version: version,
Expand Down

0 comments on commit 0baaf7d

Please sign in to comment.