- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Working on starting a site
1 parent
2a8f3b5
commit 9effde1
Showing
10 changed files
with
464 additions
and
483 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,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) | ||
} |
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
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
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() | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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