ipn/conffile: don't depend on hujson on iOS/Android

Fixes #13772

Change-Id: I3ae03a5ee48c801f2e5ea12d1e54681df25d4604
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2024-10-10 07:57:09 -07:00
committed by Brad Fitzpatrick
parent 91f58c5e63
commit 508980603b
3 changed files with 37 additions and 2 deletions

View File

@@ -8,10 +8,11 @@ package conffile
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"runtime"
"github.com/tailscale/hujson"
"tailscale.com/ipn"
)
@@ -39,8 +40,21 @@ func (c *Config) WantRunning() bool {
// from the VM's metadata service's user-data field.
const VMUserDataPath = "vm:user-data"
// hujsonStandardize is set to hujson.Standardize by conffile_hujson.go on
// platforms that support config files.
var hujsonStandardize func([]byte) ([]byte, error)
// Load reads and parses the config file at the provided path on disk.
func Load(path string) (*Config, error) {
switch runtime.GOOS {
case "ios", "android":
// compile-time for deadcode elimination
return nil, fmt.Errorf("config file loading not supported on %q", runtime.GOOS)
}
if hujsonStandardize == nil {
// Build tags are wrong in conffile_hujson.go
return nil, errors.New("[unexpected] config file loading not wired up")
}
var c Config
c.Path = path
var err error
@@ -54,7 +68,7 @@ func Load(path string) (*Config, error) {
if err != nil {
return nil, err
}
c.Std, err = hujson.Standardize(c.Raw)
c.Std, err = hujsonStandardize(c.Raw)
if err != nil {
return nil, fmt.Errorf("error parsing config file %s HuJSON/JSON: %w", path, err)
}

View File

@@ -0,0 +1,20 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build !ios && !android
package conffile
import "github.com/tailscale/hujson"
// Only link the hujson package on platforms that use it, to reduce binary size
// & memory a bit.
//
// (iOS and Android don't have config files)
// While the linker's deadcode mostly handles the hujson package today, this
// keeps us honest for the future.
func init() {
hujsonStandardize = hujson.Standardize
}