Skip to content

Commit

Permalink
add homedir support to filepath, print extra configs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Aug 3, 2024
1 parent b223671 commit 9d41328
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
7 changes: 6 additions & 1 deletion pkg/client/handlers_gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,12 @@ func (c *Client) validateNewConfig(config *configfile.Config) error {
return fmt.Errorf("copying config: %w", err)
}

if _, err := cnfgfile.Parse(copied, nil); err != nil {
_, err = cnfgfile.Parse(copied, &cnfgfile.Opts{
Name: mnd.Title,
TransformPath: configfile.ExpandHomedir,
Prefix: "filepath:",
})
if err != nil {
return fmt.Errorf("filepath: %w", err)
}

Expand Down
50 changes: 34 additions & 16 deletions pkg/client/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Start() error {
}

func (c *Client) checkFlags(ctx context.Context) error { //nolint:cyclop
msg, newPassword, err := c.loadConfiguration(ctx)
msgs, newPassword, err := c.loadConfiguration(ctx)

ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand All @@ -138,30 +138,35 @@ func (c *Client) checkFlags(ctx context.Context) error { //nolint:cyclop

return c.resetAdminPassword(ctx)
case c.Flags.Write != "" && (err == nil || strings.Contains(err.Error(), "ip:port")):
c.Printf("==> %s", msg)
for _, msg := range msgs {
c.Printf("==> %s", msg)
}

ctx, cancel := context.WithTimeout(ctx, mnd.DefaultTimeout)
defer cancel()

return c.forceWriteWithExit(ctx, c.Flags.Write)
case err != nil:
return fmt.Errorf("%s: %w", msg, err)
return fmt.Errorf("messages: %q, error: %w", msgs, err)
case c.Flags.Restart:
return nil
case c.Config.APIKey == "":
return fmt.Errorf("%s: %w %s_API_KEY", msg, ErrNilAPIKey, c.Flags.EnvPrefix)
return fmt.Errorf("messages: %q, %w %s_API_KEY", msgs, ErrNilAPIKey, c.Flags.EnvPrefix)
default:
return c.start(ctx, msg, newPassword)
return c.start(ctx, msgs, newPassword)
}
}

func (c *Client) start(ctx context.Context, msg, newPassword string) error {
func (c *Client) start(ctx context.Context, msgs []string, newPassword string) error {
c.Logger.SetupLogging(c.Config.LogConfig)
c.Printf(" %s %s v%s-%s Starting! [PID: %v, UID: %d, GID: %d] %s",
mnd.TodaysEmoji(), mnd.Title, version.Version, version.Revision,
os.Getpid(), os.Getuid(), os.Getgid(),
version.Started.Format("Mon, Jan 2, 2006 @ 3:04:05 PM MST -0700"))
c.Printf("==> %s", msg)

for _, msg := range msgs {
c.Printf("==> %s", msg)
}

if c.Flags.Updated {
go ui.Toast("%s updated to v%s-%s", mnd.Title, version.Version, version.Revision) //nolint:errcheck
Expand Down Expand Up @@ -201,32 +206,40 @@ func (c *Client) makeNewConfigFile(ctx context.Context, newPassword string) {
}

// loadConfiguration brings in, and sometimes creates, the initial running configuration.
func (c *Client) loadConfiguration(ctx context.Context) (string, string, error) {
var msg, newPassword string
func (c *Client) loadConfiguration(ctx context.Context) ([]string, string, error) {
var (
msg, newPassword string
err error
moreMsgs = make(map[string]string)

Check failure on line 213 in pkg/client/start.go

View workflow job for this annotation

GitHub Actions / golangci-lint (linux)

ineffectual assignment to moreMsgs (ineffassign)

Check failure on line 213 in pkg/client/start.go

View workflow job for this annotation

GitHub Actions / golangci-lint (freebsd)

ineffectual assignment to moreMsgs (ineffassign)
)
// Find or write a config file. This does not parse it.
// A config file is only written when none is found on Windows, macOS (GUI App only), or Docker.
// And in the case of Docker, only if `/config` is a mounted volume.
write := (!c.Flags.Restart && ui.HasGUI()) || mnd.IsDocker
c.Flags.ConfigFile, newPassword, msg = c.Config.FindAndReturn(ctx, c.Flags.ConfigFile, write)
output := []string{msg}

if c.Flags.Restart {
return msg, newPassword, update.Restart(&update.Command{ //nolint:wrapcheck
return output, newPassword, update.Restart(&update.Command{ //nolint:wrapcheck
Path: os.Args[0],
Args: []string{"--updated", "--delay", "5s", "--config", c.Flags.ConfigFile},
})
}

var err error
// Parse the config file and environment variables.
if c.Input, err = c.Config.Get(c.Flags); err != nil {
return msg, newPassword, fmt.Errorf("getting config: %w", err)
return output, newPassword, fmt.Errorf("getting config: %w", err)
}

if c.triggers, err = c.Config.Setup(c.Flags, c.Logger); err != nil {
return msg, newPassword, fmt.Errorf("setting config: %w", err)
if c.triggers, moreMsgs, err = c.Config.Setup(c.Flags, c.Logger); err != nil {
return output, newPassword, fmt.Errorf("setting config: %w", err)
}

return msg, newPassword, nil
for file, path := range moreMsgs {
output = append(output, fmt.Sprintf("Extra Config File: %s => %s", file, path))
}

return output, newPassword, nil
}

// Load configuration from the website.
Expand Down Expand Up @@ -338,7 +351,8 @@ func (c *Client) reloadConfiguration(ctx context.Context, event website.EventTyp
return fmt.Errorf("getting config: %w", err)
}

if c.triggers, err = c.Config.Setup(c.Flags, c.Logger); err != nil {
var output map[string]string
if c.triggers, output, err = c.Config.Setup(c.Flags, c.Logger); err != nil {

Check failure on line 355 in pkg/client/start.go

View workflow job for this annotation

GitHub Actions / golangci-lint (linux)

if statements should only be cuddled with assignments used in the if statement itself (wsl)

Check failure on line 355 in pkg/client/start.go

View workflow job for this annotation

GitHub Actions / golangci-lint (freebsd)

if statements should only be cuddled with assignments used in the if statement itself (wsl)
return fmt.Errorf("setting config: %w", err)
}

Expand Down Expand Up @@ -368,6 +382,10 @@ func (c *Client) reloadConfiguration(ctx context.Context, event website.EventTyp
}
}

for path, file := range output {
c.Printf(" => Extra Config File: %s => %s", file, path)
}

// This doesn't need to lock because web server is not running.
c.reloading = false // We're done.

Expand Down
32 changes: 23 additions & 9 deletions pkg/configfile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,40 @@ func (c *Config) Get(flag *Flags) (*Config, error) {
return c.CopyConfig()
}

func (c *Config) Setup(flag *Flags, logger *logs.Logger) (*triggers.Actions, error) {
if _, err := cnfgfile.Parse(c, nil); err != nil {
return nil, fmt.Errorf("filepath variables: %w", err)
// ExpandHomedir expands a ~ to a homedir, or returns the original path in case of any error.
func ExpandHomedir(filePath string) string {
expanded, err := homedir.Expand(filePath)
if err != nil {
return filePath
}

return expanded
}

func (c *Config) Setup(flag *Flags, logger *logs.Logger) (*triggers.Actions, map[string]string, error) {
output, err := cnfgfile.Parse(c, &cnfgfile.Opts{
Name: mnd.Title,
TransformPath: ExpandHomedir,
Prefix: "filepath:",
})
if err != nil {
return nil, nil, fmt.Errorf("filepath variables: %w", err)
}

if err := c.setupPassword(); err != nil {
return nil, err
return nil, nil, err
}

c.fixConfig()
logger.LogConfig = c.LogConfig // this is sorta hacky.

if err := c.Services.Setup(c.Service); err != nil {
return nil, fmt.Errorf("service checks: %w", err)
return nil, nil, fmt.Errorf("service checks: %w", err)
}

// Make sure each app has a sane timeout.
err := c.Apps.Setup()
if err != nil {
return nil, fmt.Errorf("setting up app: %w", err)
if err = c.Apps.Setup(); err != nil {
return nil, nil, fmt.Errorf("setting up app: %w", err)
}

// Make sure the port is not in use before starting the web server.
Expand All @@ -185,7 +199,7 @@ func (c *Config) Setup(flag *Flags, logger *logs.Logger) (*triggers.Actions, err
})
c.Services.SetWebsite(c.Server)

return c.setup(logger, flag), err
return c.setup(logger, flag), output, err
}

func (c *Config) fixConfig() {
Expand Down

0 comments on commit 9d41328

Please sign in to comment.