go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/blogctl/pkg/config/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package config
     9  
    10  import (
    11  	"go.charczuk.com/projects/blogctl/pkg/constants"
    12  )
    13  
    14  // These are set by ldflags.
    15  var (
    16  	Version = ""
    17  	GitRef  = ""
    18  )
    19  
    20  // Extra is just exta data you might want to pass into the renderer.
    21  type Extra = map[string]any
    22  
    23  // Config is the blog config
    24  type Config struct {
    25  	// Title is the title for the blog.
    26  	Title string `json:"title" yaml:"title"`
    27  	// Author is your name.
    28  	Author string `json:"author" yaml:"author"`
    29  	// Description is a description for the blog, will be used in html head meta.
    30  	Description string `json:"description,omitempty" yaml:"description,omitempty"`
    31  	// BaseURL is the base url for the blog.
    32  	BaseURL string `json:"baseURL,omitempty" yaml:"baseURL,omitempty"`
    33  
    34  	// resource paths
    35  
    36  	// PostsPath is the path to the posts to compile.
    37  	PostsPath string `json:"postsPath,omitempty" yaml:"postsPath,omitempty"`
    38  	// PagesPath is the path to a folder with pages to compile.
    39  	// They are rendered and copied to the root of the output.
    40  	PagesPath string `json:"pagesPath,omitempty" yaml:"pagesPath,omitempty"`
    41  	// OutputPath is the compiled site path.
    42  	OutputPath string `json:"outputPath,omitempty" yaml:"outputPath,omitempty"`
    43  	// PatialsPath is the path to a folder with partials to include
    44  	// when rendering pages and the posts.
    45  	PartialsPath string `json:"partialsPath,omitempty" yaml:"partialsPath,omitempty"`
    46  	// StaticPath is the path to a folder with static files to copy over.
    47  	StaticsPath string `json:"staticsPath,omitempty" yaml:"staticsPath,omitempty"`
    48  	// ThumbnailCachePath is the path to the thumbnail cache.
    49  	ThumbnailCachePath string `json:"thumbnailCachePath,omitempty" yaml:"thumbnailCachePath,omitempty"`
    50  
    51  	// SlugTemplate is the template for post slugs.
    52  	// It defaults to "/{{ .Meta.Posted.Year }}/{{ .Meta.Posted.Month }}/{{ .Meta.Posted.Day }}/{{ .Meta.Title | slugify }}/"
    53  	SlugTemplate string `json:"slugTemplate,omitempty" yaml:"slugTemplate,omitempty"`
    54  	// ImagePostTemplate is the path to the post template file.
    55  	// It is what is rendered when you go to /<POST_SLUG>/ for image posts.
    56  	ImagePostTemplatePath string `json:"imagePostTemplatePath,omitempty" yaml:"imagePostTemplatePath,omitempty"`
    57  	// TextPostTemplatePath is the path to the text post template file.
    58  	// It is what is rendered when you go to /<POST_SLUG>/ for text posts.
    59  	TextPostTemplatePath string `json:"textPostTemplatePath,omitempty" yaml:"textPostTemplatePath,omitempty"`
    60  	// TagTemplate is the path to the tag template file.
    61  	// It is what is rendered when you go to /tags/:tag_name
    62  	TagTemplatePath string `json:"tagTemplatePath,omitempty" yaml:"tagTemplatePath,omitempty"`
    63  	// ImageSizes lets you set what size thumbnails to create from post files.
    64  	// This defaults to 2048px, 1024px, and 512px.
    65  	ImageSizes []int `json:"imageSizes,omitempty" yaml:"imageSizes,omitempty"`
    66  	// Extra is optional and allows you to provide variables for templates.
    67  	Extra map[string]string `json:"extra,omitempty" yaml:"extra,omitempty"`
    68  
    69  	// S3 governs how the blog is deployed.
    70  	S3 S3 `json:"s3,omitempty" yaml:"s3,omitempty"`
    71  	// Cloudfront governs options for how the s3 files are cached.
    72  	Cloudfront Cloudfront `json:"cloudfront,omitempty" yaml:"cloudfront,omitempty"`
    73  
    74  	// below are knobs you can turn tweak specific things.
    75  
    76  	// PostSortKey is the key that you can use to sort posts in the feed by.
    77  	PostSortKey string `json:"postSortKey,omitempty" yaml:"postSortKey,omitempty"`
    78  	// PostSortAscending determines if we should sort ascending or descending.
    79  	PostSortAscending *bool `json:"postSortAscending,omitempty" yaml:"postSortAscending,omitempty"`
    80  	// SkipImageOriginal instructs the engine to not copy the original image.
    81  	SkipCopyOriginalImage bool `json:"skipImageOriginal,omitempty" yaml:"skipImageOriginal,omitempty"`
    82  	// SkipTags instructs the engine to not create tag summary pages.
    83  	SkipGenerateTags bool `json:"skipGenerateTags,omitempty" yaml:"skipGenerateTags,omitempty"`
    84  	// SkipGenerateJSONData instructs the engine not to create a data.json file.
    85  	SkipGenerateJSONData bool `json:"skipGenerateJSONData,omitempty" yaml:"skipGenerateJSONData,omitempty"`
    86  }
    87  
    88  // Fields returns fields to prompt for when creating a new config.
    89  func (c *Config) Fields() []Field {
    90  	return []Field{
    91  		{Prompt: "Title (the title of the blog)", FieldReference: &c.Title},
    92  		{Prompt: "Author (your name)", FieldReference: &c.Author},
    93  		{Prompt: "Base URL (fully qualified, i.e https://foo.com)", FieldReference: &c.BaseURL},
    94  		{Prompt: "Output Path (where the compiled blog goes)", FieldReference: &c.OutputPath, Default: constants.DefaultOutputPath},
    95  		{Prompt: "Posts Path (where your posts live)", FieldReference: &c.PostsPath, Default: constants.DefaultPostsPath},
    96  		{Prompt: "Pages Path (pages to render)", FieldReference: &c.PagesPath, Default: constants.DefaultPagesPath},
    97  		{Prompt: "Partials Path (partials to include)", FieldReference: &c.PartialsPath, Default: constants.DefaultPartialsPath},
    98  		{Prompt: "Statics Path (files to copy to output)", FieldReference: &c.StaticsPath, Default: constants.DefaultStaticsPath},
    99  		{Prompt: "Slug Template (template literal for slugs)", FieldReference: &c.SlugTemplate, Default: constants.DefaultSlugTemplate},
   100  		{Prompt: "Image Post Template Path (template file to use for image posts)", FieldReference: &c.ImagePostTemplatePath, Default: constants.DefaultImagePostTemplatePath},
   101  		{Prompt: "Text Post Template Path (template file to use for text posts)", FieldReference: &c.TextPostTemplatePath, Default: constants.DefaultTextPostTemplatePath},
   102  		{Prompt: "Tag Template Path (template file to use for each tag)", FieldReference: &c.TagTemplatePath, Default: constants.DefaultTagTemplatePath},
   103  		{Prompt: "Thumbnail Cache Path (resized image cache path)", FieldReference: &c.ThumbnailCachePath, Default: constants.DefaultThumbnailCachePath},
   104  		{Prompt: "Posts Sort Key (what to sort images by)", FieldReference: &c.PostSortKey, Default: constants.PostSortKeyCapture},
   105  	}
   106  }
   107  
   108  // TitleOrDefault returns the title or a default.
   109  func (c Config) TitleOrDefault() string {
   110  	return c.Title
   111  }
   112  
   113  // AuthorOrDefault returns the author or a default.
   114  func (c Config) AuthorOrDefault() string {
   115  	return c.Author
   116  }
   117  
   118  // BaseURLOrDefault returns the base url or a default.
   119  func (c Config) BaseURLOrDefault() string {
   120  	return c.BaseURL
   121  }
   122  
   123  // OutputPathOrDefault returns the output path.
   124  func (c Config) OutputPathOrDefault() string {
   125  	if c.OutputPath != "" {
   126  		return c.OutputPath
   127  	}
   128  	return constants.DefaultOutputPath
   129  }
   130  
   131  // PostsPathOrDefault returns the images path.
   132  func (c Config) PostsPathOrDefault() string {
   133  	if c.PostsPath != "" {
   134  		return c.PostsPath
   135  	}
   136  	return constants.DefaultPostsPath
   137  }
   138  
   139  // SlugTemplateOrDefault returns the slug template or default.
   140  func (c Config) SlugTemplateOrDefault() string {
   141  	if c.SlugTemplate != "" {
   142  		return c.SlugTemplate
   143  	}
   144  	return constants.DefaultSlugTemplate
   145  }
   146  
   147  // ImagePostTemplateOrDefault returns the single post template or a default.
   148  func (c Config) ImagePostTemplateOrDefault() string {
   149  	if c.ImagePostTemplatePath != "" {
   150  		return c.ImagePostTemplatePath
   151  	}
   152  	return constants.DefaultImagePostTemplatePath
   153  }
   154  
   155  // TextPostTemplateOrDefault returns the single text post template or a default.
   156  func (c Config) TextPostTemplateOrDefault() string {
   157  	if c.TextPostTemplatePath != "" {
   158  		return c.TextPostTemplatePath
   159  	}
   160  	return constants.DefaultTextPostTemplatePath
   161  }
   162  
   163  // TagTemplateOrDefault returns the single tag template or a default.
   164  func (c Config) TagTemplateOrDefault() string {
   165  	if c.TagTemplatePath != "" {
   166  		return c.TagTemplatePath
   167  	}
   168  	return constants.DefaultTagTemplatePath
   169  }
   170  
   171  // PagesPathOrDefault returns page file paths or defaults.
   172  func (c Config) PagesPathOrDefault() string {
   173  	if c.PagesPath != "" {
   174  		return c.PagesPath
   175  	}
   176  	return constants.DefaultPagesPath
   177  }
   178  
   179  // PartialsPathOrDefault returns partial file paths or defaults.
   180  func (c Config) PartialsPathOrDefault() string {
   181  	if c.PartialsPath != "" {
   182  		return c.PartialsPath
   183  	}
   184  	return constants.DefaultPartialsPath
   185  }
   186  
   187  // StaticsPathOrDefault returns static file paths or defaults.
   188  func (c Config) StaticsPathOrDefault() string {
   189  	if c.StaticsPath != "" {
   190  		return c.StaticsPath
   191  	}
   192  	return constants.DefaultStaticsPath
   193  }
   194  
   195  // ThumbnailCachePathOrDefault returns static file paths or defaults.
   196  func (c Config) ThumbnailCachePathOrDefault() string {
   197  	if c.ThumbnailCachePath != "" {
   198  		return c.ThumbnailCachePath
   199  	}
   200  	return constants.DefaultThumbnailCachePath
   201  }
   202  
   203  // ImageSizesOrDefault returns the image sizes or a default set.
   204  func (c Config) ImageSizesOrDefault() []int {
   205  	if c.ImageSizes != nil {
   206  		return c.ImageSizes
   207  	}
   208  	return constants.DefaultImageSizes
   209  }
   210  
   211  // PostSortKeyOrDefault returns the post sort key or a default.
   212  func (c Config) PostSortKeyOrDefault() string {
   213  	if c.PostSortKey != "" {
   214  		return c.PostSortKey
   215  	}
   216  	return constants.PostSortKeyCapture
   217  }
   218  
   219  // PostSortAscendingOrDefault returns the post sort direction or a default.
   220  func (c Config) PostSortAscendingOrDefault() bool {
   221  	if c.PostSortAscending != nil {
   222  		return *c.PostSortAscending
   223  	}
   224  	return false
   225  }