Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on starting a site
Browse files Browse the repository at this point in the history
ChrisWiegman committed Jan 21, 2024
1 parent 2a8f3b5 commit 9effde1
Showing 10 changed files with 464 additions and 483 deletions.
87 changes: 87 additions & 0 deletions internal/settings/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package settings

import (
"fmt"
"os"
"path"
"strconv"
"strings"
)

// GetArrayValue returns a slice of strings with the set value.
func (s *Settings) GetArrayValue(name string) []string {
setting, _ := s.getSetting(name)

return strings.Split(setting.DefaultValue, ",")
}

// GetBoolValue returns a bool with the set value.
func (s *Settings) GetBoolValue(name string) bool {
setting, _ := s.getSetting(name)

settingVal, _ := strconv.ParseBool(setting.DefaultValue)

return settingVal
}

// GetDefaultPermissions returns the default directory permissions and the default file permissions.
func GetDefaultPermissions() (dirPerms, filePerms int) {
return defaultDirPermissions, defaultFilePermissions
}

// GetIntValue returns an int with the set value.
func (s *Settings) GetIntValue(name string) int {
setting, _ := s.getSetting(name)

settingVal, _ := strconv.Atoi(setting.DefaultValue)

return settingVal
}

// GetProtocol returns the appropriate protocol for the URL based on the https setting.
func (s *Settings) GetProtocol() string {
ssl := s.GetBoolValue("ssl")

if ssl {
return "https"
}

return "http"
}

// GetStringValue returns a string with the set value.
func (s *Settings) GetStringValue(name string) string {
setting, _ := s.getSetting(name)

return setting.Value
}

// HasLocalOptions Returns true if local options have been saved to a file or false.
func (s *Settings) HasLocalOptions() bool {
if _, err := os.Stat(path.Join(s.GetStringValue("workingDirectory"), ".kana.json")); os.IsNotExist(err) {
return false
}

return true
}

func (s *Settings) SetSetting(name, value string) (err error) {
for i := range s.settings {
if s.settings[i].Name == name {
validString := true
for _, validValue := range s.settings[i].ValidValues {
if validValue == value {
validString = true
}
}

if validString {
return s.setSetting(name, value, s.settings[i].HasGlobal)
}

return fmt.Errorf("the value, %s, for setting %s in invalid. please use a value value", value, name)
}
}

return fmt.Errorf("setting, %s, not found", name)
}
4 changes: 2 additions & 2 deletions internal/settings/config.go
Original file line number Diff line number Diff line change
@@ -40,11 +40,11 @@ func (s *Settings) ListSettings(consoleOutput *console.Console) {
local := ""

if s.settings[i].HasGlobal {
global = s.settings[i].GlobalValue
global = s.settings[i].Value
}

if s.settings[i].HasLocal {
local = s.settings[i].LocalValue
local = s.settings[i].Value
}

if s.settings[i].Name != "plugins" && (s.settings[i].HasLocal || s.settings[i].HasGlobal) {
10 changes: 0 additions & 10 deletions internal/settings/defaults.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ var defaultSettings = []Setting{
DefaultValue: "true",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "adminEmail",
@@ -61,7 +60,6 @@ var defaultSettings = []Setting{
DefaultValue: "local",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
ValidValues: []string{
"local",
"development",
@@ -88,7 +86,6 @@ var defaultSettings = []Setting{
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "mariadb",
@@ -103,7 +100,6 @@ var defaultSettings = []Setting{
DefaultValue: "none",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
ValidValues: []string{
"none",
"subdomain",
@@ -133,7 +129,6 @@ var defaultSettings = []Setting{
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "rootCert",
@@ -150,7 +145,6 @@ var defaultSettings = []Setting{
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "siteCert",
@@ -171,15 +165,13 @@ var defaultSettings = []Setting{
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "type",
Type: "string",
DefaultValue: "site",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
ValidValues: []string{
"site",
"plugin",
@@ -203,14 +195,12 @@ var defaultSettings = []Setting{
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
{
Name: "xdebug",
DefaultValue: "false",
HasGlobal: true,
HasLocal: true,
HasFlag: true,
},
}

6 changes: 3 additions & 3 deletions internal/settings/files.go
Original file line number Diff line number Diff line change
@@ -67,8 +67,8 @@ func (s *Settings) EnsureKanaPlugin(appDir string) error {
// EnsureStaticConfigFiles Ensures the application's static config files have been generated and are where they need to be.
func (s *Settings) EnsureStaticConfigFiles() error {
for _, file := range configFiles {
filePath := path.Join(s.GetStringValue("AppDirectory"), file.LocalPath)
destFile := path.Join(s.GetStringValue("AppDirectory"), file.LocalPath, file.Name)
filePath := path.Join(s.GetStringValue("appDirectory"), file.LocalPath)
destFile := path.Join(s.GetStringValue("appDirectory"), file.LocalPath, file.Name)

if err := os.MkdirAll(filePath, os.FileMode(defaultDirPermissions)); err != nil {
return err
@@ -86,7 +86,7 @@ func (s *Settings) EnsureStaticConfigFiles() error {
}

func (s *Settings) GetHtaccess() string {
if s.GetStringValue("Multisite") == "subdomain" {
if s.GetStringValue("multisite") == "subdomain" {
return SubDomainMultisiteHtaccess
}

344 changes: 344 additions & 0 deletions internal/settings/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
package settings

import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"

"github.com/ChrisWiegman/kana-cli/internal/helpers"

"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getSetting returns the string value for the provided type and settings name.
func (s *Settings) getSetting(name string) (Setting, error) {
for i := range s.settings {
if s.settings[i].Name == name {
return s.settings[i], nil
}
}

return Setting{}, fmt.Errorf("setting, %s, not found", name)
}

// loadDirectories loads the app directories we know for the site.
func (s *Settings) loadDirectories() error {
cwd, err := os.Getwd()
if err != nil {
return err
}

err = s.setSetting("workingDirectory", cwd, false)
if err != nil {
return err
}

home, err := homedir.Dir()
if err != nil {
return err
}

configFolderName := s.GetStringValue("configFolderName")

err = s.setSetting("appDirectory", filepath.Join(home, configFolderName), false)
if err != nil {
return err
}

return nil
}

func (s *Settings) loadSettings(settingType string) error {
viperSettings, err := s.loadViperSettings(settingType)
if err != nil {
return err
}

for i := range defaultSettings {
if settingType == "global" && s.settings[i].HasGlobal { //nolint: goconst
s.global = viperSettings
err = s.setSetting(s.settings[i].Name, viperSettings.GetString(s.settings[i].Name), false)
if err != nil {
return err
}

if s.settings[i].HasLocal {
err = s.setSetting(s.settings[i].Name, viperSettings.GetString(s.settings[i].Name), false)
if err != nil {
return err
}
}
} else if settingType == "local" && s.settings[i].HasLocal { //nolint: goconst
s.local = viperSettings
err = s.setSetting(s.settings[i].Name, viperSettings.GetString(s.settings[i].Name), false)
if err != nil {
return err
}
}
}

return nil
}

// loadViperSettings loads the specified type of settings from Viper.
func (s *Settings) loadViperSettings(settingType string) (*viper.Viper, error) {
userSettings := viper.New()

for i := range defaultSettings {
if settingType == "global" && s.settings[i].HasGlobal {
userSettings.SetDefault(s.settings[i].Name, s.settings[i].DefaultValue)
} else if settingType == "local" && s.settings[i].HasLocal {
userSettings.SetDefault(s.settings[i].Name, s.settings[i].Value)
}
}

userSettings.SetConfigType("json")

configPath := s.GetStringValue("workingDirectory")

if settingType == "global" {
appDirectory := s.GetStringValue("appDirectory")

userSettings.SetConfigName("kana")

configPath = path.Join(appDirectory, "config")
} else if settingType == "local" {
userSettings.SetConfigName(".kana")
}

userSettings.AddConfigPath(configPath)

err := userSettings.ReadInConfig()
if err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError

if errors.As(err, &configFileNotFoundError) {
if settingType == "global" {
err = userSettings.SafeWriteConfig()
if err != nil {
return userSettings, err
}
} else {
return userSettings, nil
}
} else {
return userSettings, err
}
}

return userSettings, nil
}

func (s *Settings) populateDefaults() {
for i := range s.settings {
s.settings[i].Value = s.settings[i].DefaultValue
}
}

func (s *Settings) processNameFlag(cmd *cobra.Command) error {
isStartCommand := cmd.Use == "start"

// Don't run this on commands that wouldn't possibly use it.
if cmd.Use == "config" || cmd.Use == "version" || cmd.Use == "help" {
return nil
}

protocol := s.GetProtocol() // We share the protocol whether it is a named site or not.
appDirectory := s.GetStringValue("appDirectory")
appDomain := s.GetStringValue("appDomain")

// Process the name flag if set
if cmd.Flags().Lookup("name").Changed {
err := s.setSetting("isNamedSite", "true", false)
if err != nil {
return err
}

// Check that we're not using invalid start flags for the start command
if isStartCommand {
if cmd.Flags().Lookup("plugin").Changed || cmd.Flags().Lookup("theme").Changed {
return fmt.Errorf("invalid flags detected. 'plugin' and 'theme' flags are not valid with named sites")
}
}

siteName := helpers.SanitizeSiteName(cmd.Flags().Lookup("name").Value.String())
url := fmt.Sprintf("%s://%s.%s", protocol, siteName, appDomain)
siteDirectory := path.Join(appDirectory, "sites", siteName)

err = s.SetSetting("name", siteName)
if err != nil {
return err
}

err = s.SetSetting("SiteDirectory", siteDirectory)
if err != nil {
return err
}

return s.SetSetting("url", url)
} else {
return s.processUnnamedDefaults(appDirectory, appDomain, protocol)
}
}

func (s *Settings) processStartFlags(cmd *cobra.Command, flags StartFlags) (err error) { //nolint
if cmd.Use != "start" {
return nil
}

if cmd.Flags().Lookup("xdebug").Changed {
err = s.SetSetting("xdebug", strconv.FormatBool(flags.Xdebug))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("wpdebug").Changed {
err = s.SetSetting("wpdebug", strconv.FormatBool(flags.WPDebug))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("scriptdebug").Changed {
err = s.SetSetting("scriptdebug", strconv.FormatBool(flags.ScriptDebug))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("ssl").Changed {
err = s.SetSetting("ssl", strconv.FormatBool(flags.SSL))
if err != nil {
return err
}

protocol := s.GetProtocol()
name := s.GetStringValue("name")
domain := s.GetStringValue("domain")

err = s.SetSetting("url", fmt.Sprintf("%s://%s.%s", protocol, name, domain))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("mailpit").Changed {
err = s.SetSetting("mailpit", strconv.FormatBool(flags.Mailpit))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("plugin").Changed && flags.IsPlugin {
err = s.SetSetting("type", "plugin")
if err != nil {
return err
}
}

if cmd.Flags().Lookup("theme").Changed && flags.IsTheme {
err = s.SetSetting("type", "Theme")
if err != nil {
return err
}
}

if cmd.Flags().Lookup("multisite").Changed {
err = s.SetSetting("multisite", flags.Multisite)
if err != nil {
return err
}
}

if cmd.Flags().Lookup("activate").Changed {
err = s.SetSetting("activate", strconv.FormatBool(flags.Activate))
if err != nil {
return err
}
}

if cmd.Flags().Lookup("environment").Changed {
err = s.SetSetting("environment", flags.Environment)
if err != nil {
return err
}
}

if cmd.Flags().Lookup("remove-default-plugins").Changed {
err = s.SetSetting("removeDefaultPlugins", strconv.FormatBool(flags.RemoveDefaultPlugins))
if err != nil {
return err
}
}

return nil
}

// processUnnamedDefaults handles the appropriate settings for sites that aren't named.
func (s *Settings) processUnnamedDefaults(appDirectory, appDomain, protocol string) error {
workingDirectory := s.GetStringValue("workingDirectory")

siteName := helpers.SanitizeSiteName(filepath.Base(workingDirectory))

url := fmt.Sprintf("%s://%s.%s", protocol, siteName, appDomain)

siteDirectory := path.Join(appDirectory, "sites", siteName)

err := s.SetSetting("name", siteName)
if err != nil {
return err
}

err = s.SetSetting("siteDirectory", siteDirectory)
if err != nil {
return err
}

return s.SetSetting("url", url)
}

// setSetting validates, when appropriate, and sets the given settings.
func (s *Settings) setSetting(name, value string, write bool) error {
settings := defaultSettings

for i := range settings {
if s.settings[i].Name == name {
validString := true
for _, validValue := range s.settings[i].ValidValues {
if validValue == value {
validString = true
}
}

if validString {
settings[i].Value = value

if write && settings[i].HasGlobal {
return s.writeGlobalSettings()
}

return nil
}

return fmt.Errorf("the value, %s, for setting %s in invalid. please use a value value", value, name)
}
}

return fmt.Errorf("setting, %s, not found", name)
}

// writeGlobalSettings writes the global settings to disk.
func (s *Settings) writeGlobalSettings() error {
for i := range s.settings {
if s.settings[i].HasGlobal {
s.global.Set(s.settings[i].Name, s.settings[i].Value)
}
}
return s.global.WriteConfig()
}
462 changes: 7 additions & 455 deletions internal/settings/settings.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions internal/settings/ssl.go
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ const certOS = "darwin"
func (s *Settings) EnsureSSLCerts(consoleOutput *console.Console) error {
createCert := false

certPath := path.Join(s.GetStringValue("AppDirectory"), "certs")
rootCert := path.Join(certPath, s.GetStringValue("RootCert"))
certPath := path.Join(s.GetStringValue("appDirectory"), "certs")
rootCert := path.Join(certPath, s.GetStringValue("rootCert"))

_, err := os.Stat(rootCert)
if err != nil && os.IsNotExist(err) {
@@ -35,11 +35,11 @@ func (s *Settings) EnsureSSLCerts(consoleOutput *console.Console) error {

certInfo := minica.CertInfo{
CertDir: certPath,
CertDomain: s.GetStringValue("AppDirectory"),
RootKey: s.GetStringValue("RootKey"),
RootCert: s.GetStringValue("RootCert"),
SiteCert: s.GetStringValue("SiteCert"),
SiteKey: s.GetStringValue("SiteKey"),
CertDomain: s.GetStringValue("appDirectory"),
RootKey: s.GetStringValue("rootKey"),
RootCert: s.GetStringValue("rootCert"),
SiteCert: s.GetStringValue("siteCert"),
SiteKey: s.GetStringValue("siteKey"),
}

err = minica.GenCerts(&certInfo)
6 changes: 3 additions & 3 deletions internal/settings/types.go
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@ type KanaPluginVars struct {
SiteName, Version string
}
type Setting struct {
Name, Type, DefaultValue, AppValue, GlobalValue, LocalValue, FlagValue string
HasGlobal, HasLocal, HasFlag bool
ValidValues []string
Name, Type, DefaultValue, Value string
HasGlobal, HasLocal bool
ValidValues []string
}

type Settings struct {
2 changes: 1 addition & 1 deletion internal/site/site.go
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ func (s *Site) LoadSite(
return err
}

s.Settings.SetSetting("type", siteType)
err = s.Settings.SetSetting("type", siteType)
if err != nil {
return err
}
12 changes: 10 additions & 2 deletions internal/site/wordpress.go
Original file line number Diff line number Diff line change
@@ -288,7 +288,11 @@ func (s *Site) getWordPressContainers() []string {

func (s *Site) activateProject(consoleOutput *console.Console) error {
if s.Settings.GetBoolValue("activate") && s.Settings.GetStringValue("type") != "site" {
consoleOutput.Println(fmt.Sprintf("Activating %s: %s", s.Settings.GetStringValue("type"), consoleOutput.Bold(consoleOutput.Blue(s.Settings.GetStringValue("name")))))
consoleOutput.Println(
fmt.Sprintf(
"Activating %s: %s",
s.Settings.GetStringValue("type"),
consoleOutput.Bold(consoleOutput.Blue(s.Settings.GetStringValue("name")))))

setupCommand := []string{
s.Settings.GetStringValue("type"),
@@ -302,7 +306,11 @@ func (s *Site) activateProject(consoleOutput *console.Console) error {
}

if code != 0 {
consoleOutput.Warn(fmt.Sprintf("Unable to activate %s: %s.", s.Settings.GetStringValue("type"), consoleOutput.Bold(consoleOutput.Blue(s.Settings.GetStringValue("name")))))
consoleOutput.Warn(
fmt.Sprintf(
"Unable to activate %s: %s.",
s.Settings.GetStringValue("type"),
consoleOutput.Bold(consoleOutput.Blue(s.Settings.GetStringValue("name")))))
}
}

0 comments on commit 9effde1

Please sign in to comment.