~emersion/hut-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH] Lazily load config and cache it in context

Details
Message ID
<20230318122720.15551-1-contact@emersion.fr>
DKIM signature
pass
Download raw message
Patch: +38 -24
This allows us to use the config file for other purposes, for
instance to pick the right git remote for the `hut git` subcommands.

References: https://lists.sr.ht/~emersion/hut-dev/patches/36688
References: https://lists.sr.ht/~emersion/hut-dev/patches/30413
References: https://lists.sr.ht/~emersion/hut-dev/patches/30164
---
 client.go | 24 +-----------------------
 config.go | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/client.go b/client.go
index 7a575229688e..deec3c2fdb99 100644
--- a/client.go
+++ b/client.go
@@ -1,12 +1,10 @@
package main

import (
	"errors"
	"fmt"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"strings"
	"time"
@@ -29,27 +27,7 @@ func createClient(service string, cmd *cobra.Command) *Client {
}

func createClientWithInstance(service string, cmd *cobra.Command, instanceName string) *Client {
	customConfigFile := true
	configFile, err := cmd.Flags().GetString("config")
	if err != nil {
		log.Fatal(err)
	} else if configFile == "" {
		configFile = defaultConfigFilename()
		customConfigFile = false
	}

	cfg, err := loadConfig(configFile)
	if err != nil {
		// This error message doesn't make sense if a config was
		// provided with "--config". In that case, the normal log
		// message is always desired.
		if !customConfigFile && errors.Is(err, os.ErrNotExist) {
			os.Stderr.WriteString("Looks like hut's config file hasn't been set up yet.\nRun `hut init` to configure it.\n")
			os.Exit(1)
		}
		log.Fatalf("failed to load config file: %v", err)
	}

	cfg := loadConfig(cmd)
	if len(cfg.Instances) == 0 {
		log.Fatalf("no sr.ht instance configured")
	}
diff --git a/config.go b/config.go
index 0c7d1ee55eed..d8298e5737de 100644
--- a/config.go
+++ b/config.go
@@ -2,6 +2,8 @@ package main

import (
	"bufio"
	"context"
	"errors"
	"fmt"
	"log"
	"os"
@@ -45,7 +47,7 @@ func instancesEqual(a, b string) bool {
	return a == b || strings.HasSuffix(a, "."+b) || strings.HasSuffix(b, "."+a)
}

func loadConfig(filename string) (*Config, error) {
func loadConfigFile(filename string) (*Config, error) {
	rootBlock, err := scfg.Load(filename)
	if err != nil {
		return nil, err
@@ -110,6 +112,40 @@ func loadConfig(filename string) (*Config, error) {
	return cfg, nil
}

func loadConfig(cmd *cobra.Command) *Config {
	type configContextKey struct{}
	if v := cmd.Context().Value(configContextKey{}); v != nil {
		return v.(*Config)
	}

	customConfigFile := true
	configFile, err := cmd.Flags().GetString("config")
	if err != nil {
		log.Fatal(err)
	} else if configFile == "" {
		configFile = defaultConfigFilename()
		customConfigFile = false
	}

	cfg, err := loadConfigFile(configFile)
	if err != nil {
		// This error message doesn't make sense if a config was
		// provided with "--config". In that case, the normal log
		// message is always desired.
		if !customConfigFile && errors.Is(err, os.ErrNotExist) {
			os.Stderr.WriteString("Looks like hut's config file hasn't been set up yet.\nRun `hut init` to configure it.\n")
			os.Exit(1)
		}
		log.Fatalf("failed to load config file: %v", err)
	}

	ctx := cmd.Context()
	ctx = context.WithValue(ctx, configContextKey{}, cfg)
	cmd.SetContext(ctx)

	return cfg
}

func defaultConfigFilename() string {
	configDir, err := os.UserConfigDir()
	if err != nil {
-- 
2.39.2
Details
Message ID
<20230318135816.gynk6tyk6l3vnhwe@xenrox.net>
In-Reply-To
<20230318122720.15551-1-contact@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
Pushed, thanks!
Reply to thread Export thread (mbox)