Skip to content

Commit

Permalink
Develop init command
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcharter committed Sep 16, 2024
1 parent ae4e214 commit 57dc563
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 12 deletions.
28 changes: 26 additions & 2 deletions builder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,43 @@ import (
"io/fs"
"os"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/jmcharter/lumaca/config"
)

func Initialise() error {
func Initialise(author string, title string) error {
// Check for existence of config.toml
if _, err := os.Stat("config.toml"); err == nil {
fmt.Println("Project already initialised")
return nil // Early return if file exists
}
// Create config.toml
err := os.WriteFile("config.toml", []byte(""), 0644)
if author == "" {
author = "Blog Author"
}
if title == "" {
title = "Blog Title"
}
var cfg_data config.Config
cfg_data.Author.Name = author
cfg_data.Site.Title = title
cfg_data.Site.Author = author
cfg_data.Directories.Posts = "content/posts"
cfg_data.Directories.Pages = "content/pages"
cfg_data.Directories.Static = "static"
cfg_data.Directories.Templates = "templates"
cfg_data.Directories.Dist = "dist"
cfg_data.Files.Extension = ".html"

f, err := os.Create("config.toml")
if err != nil {
return fmt.Errorf("failed to create config.toml: %w", err)
}
err = toml.NewEncoder(f).Encode(cfg_data)
if err != nil {
return fmt.Errorf("failed to write config data to config.toml: %w", err)
}

// Create "templates" and "content/static" directories with files
err = os.MkdirAll("templates", 0755)
Expand Down
1 change: 0 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var buildCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(buildCmd)
buildCmd.Flags().Bool("watch", false, "NOT CURRENTLY IMPLEMENTED - Continuously watch source files for changes and rebuild automitically when changes are detected.")

// Here you will define your flags and configuration settings.
Expand Down
8 changes: 6 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import (
"github.com/spf13/cobra"
)

var cfgAuthor string
var cfgTitle string

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a new lumaca project",
Long: `Initialize a new lumaca project, creating the default directory structure and guiding you through the creation of a config file if one is not already detected.`,
Run: func(cmd *cobra.Command, args []string) {
builder.Initialise()
builder.Initialise(cfgAuthor, cfgTitle)
},
}

func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().StringVarP(&cfgAuthor, "author", "a", "", "Name of the primary blog author (optional)")
initCmd.Flags().StringVarP(&cfgTitle, "title", "t", "", "Title of the blog (optional)")
}
1 change: 0 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var newCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(newCmd)

newCmd.Flags().StringVarP(&title, "title", "t", "", "Title for the new page (required)")
newCmd.Flags().StringVarP(&author, "author", "a", "", "Author of the new page (optional)")
Expand Down
44 changes: 41 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"

Expand All @@ -9,6 +11,7 @@ import (
)

var cfg config.Config
var initialised bool = false

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -19,18 +22,53 @@ var rootCmd = &cobra.Command{

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
//
// func Execute() {
// if initialised {
// err := rootCmd.Execute()
// if err != nil {
// os.Exit(1)
// }
// }
// }
func Execute() {
err := rootCmd.Execute()
if err != nil {
if !initFileExists() {
// Only register init command if init file does not exist
rootCmd.AddCommand(initCmd)
} else {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(newCmd)
rootCmd.AddCommand(buildCmd)
rootCmd.AddCommand(serveCmd)
}

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func initFileExists() bool {
_, err := os.Stat("config.toml")
return !os.IsNotExist(err)
}
func init() {
// Try to initialize configuration, but allow for it to not exist
initConfig()

}

func initConfig() {
var err error
cfg, err = config.InitConfig()
if err != nil {
log.Fatal(err)
if errors.Is(err, config.DecodeFileError) {
// Configuration file does not exist, log an info message and continue
log.Println("Configuration file not found, please run init")
} else {
// Some other error occurred during config initialization
log.Fatal(err)
}
}
initialised = true
}
1 change: 0 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var serveCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(serveCmd)
serveCmd.Flags().IntVarP(&portFlag, "port", "p", 8080, "Port to serve static content on")
}

Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package config

import (
"fmt"
"errors"

"github.com/BurntSushi/toml"
)

var DecodeFileError = errors.New("failed to decode config file")

type Config struct {
Directories struct {
Posts string
Expand All @@ -29,7 +31,7 @@ type Config struct {
func InitConfig() (Config, error) {
var cfg Config
if _, err := toml.DecodeFile("config.toml", &cfg); err != nil {
return cfg, fmt.Errorf("failed to decode config file: %w", err)
return Config{}, DecodeFileError
}
return cfg, nil
}
Binary file added lumaca
Binary file not shown.

0 comments on commit 57dc563

Please sign in to comment.