github.com/tetratelabs/wazero@v1.2.1/internal/gojs/config/config.go (about)

     1  // Package config exists to avoid dependency cycles when keeping most of gojs
     2  // code internal.
     3  package config
     4  
     5  import (
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"syscall"
    12  
    13  	"github.com/tetratelabs/wazero/internal/platform"
    14  )
    15  
    16  type Config struct {
    17  	OsWorkdir bool
    18  	OsUser    bool
    19  
    20  	Uid, Gid, Euid int
    21  	Groups         []int
    22  
    23  	// Workdir is the actual working directory value.
    24  	Workdir string
    25  	Umask   uint32
    26  	Rt      http.RoundTripper
    27  }
    28  
    29  func NewConfig() *Config {
    30  	return &Config{
    31  		OsWorkdir: false,
    32  		OsUser:    false,
    33  		Uid:       0,
    34  		Gid:       0,
    35  		Euid:      0,
    36  		Groups:    []int{0},
    37  		Workdir:   "/",
    38  		Umask:     uint32(0o0022),
    39  		Rt:        nil,
    40  	}
    41  }
    42  
    43  func (c *Config) Clone() *Config {
    44  	ret := *c // copy except maps which share a ref
    45  	return &ret
    46  }
    47  
    48  func (c *Config) Init() error {
    49  	if c.OsWorkdir {
    50  		workdir, err := os.Getwd()
    51  		if err != nil {
    52  			return err
    53  		}
    54  		// Ensure if used on windows, the input path is translated to a POSIX one.
    55  		workdir = platform.ToPosixPath(workdir)
    56  		// Strip the volume of the path, for example C:\
    57  		c.Workdir = workdir[len(filepath.VolumeName(workdir)):]
    58  	}
    59  
    60  	// Windows does not support any of these properties
    61  	if c.OsUser && runtime.GOOS != "windows" {
    62  		c.Uid = syscall.Getuid()
    63  		c.Gid = syscall.Getgid()
    64  		c.Euid = syscall.Geteuid()
    65  		var err error
    66  		if c.Groups, err = syscall.Getgroups(); err != nil {
    67  			return fmt.Errorf("couldn't read groups: %w", err)
    68  		}
    69  	}
    70  	return nil
    71  }