Skip to content

Commit

Permalink
Move Abspath function to headscale utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed May 31, 2022
1 parent 0612927 commit 36dca35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
26 changes: 6 additions & 20 deletions cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/fs"
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -272,19 +271,6 @@ func GetDNSConfig() (*tailcfg.DNSConfig, string) {
return nil, ""
}

func AbsolutePathFromConfigPath(path string) string {
// If a relative path is provided, prefix it with the the directory where
// the config file was found.
if (path != "") && !strings.HasPrefix(path, string(os.PathSeparator)) {
dir, _ := filepath.Split(viper.ConfigFileUsed())
if dir != "" {
path = filepath.Join(dir, path)
}
}

return path
}

func GetHeadscaleConfig() headscale.Config {
dnsConfig, baseDomain := GetDNSConfig()
derpConfig := GetDERPConfig()
Expand Down Expand Up @@ -350,7 +336,7 @@ func GetHeadscaleConfig() headscale.Config {
GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"),

IPPrefixes: prefixes,
PrivateKeyPath: AbsolutePathFromConfigPath(viper.GetString("private_key_path")),
PrivateKeyPath: headscale.AbsolutePathFromConfigPath(viper.GetString("private_key_path")),
BaseDomain: baseDomain,

DERP: derpConfig,
Expand All @@ -360,7 +346,7 @@ func GetHeadscaleConfig() headscale.Config {
),

DBtype: viper.GetString("db_type"),
DBpath: AbsolutePathFromConfigPath(viper.GetString("db_path")),
DBpath: headscale.AbsolutePathFromConfigPath(viper.GetString("db_path")),
DBhost: viper.GetString("db_host"),
DBport: viper.GetInt("db_port"),
DBname: viper.GetString("db_name"),
Expand All @@ -369,13 +355,13 @@ func GetHeadscaleConfig() headscale.Config {

TLSLetsEncryptHostname: viper.GetString("tls_letsencrypt_hostname"),
TLSLetsEncryptListen: viper.GetString("tls_letsencrypt_listen"),
TLSLetsEncryptCacheDir: AbsolutePathFromConfigPath(
TLSLetsEncryptCacheDir: headscale.AbsolutePathFromConfigPath(
viper.GetString("tls_letsencrypt_cache_dir"),
),
TLSLetsEncryptChallengeType: viper.GetString("tls_letsencrypt_challenge_type"),

TLSCertPath: AbsolutePathFromConfigPath(viper.GetString("tls_cert_path")),
TLSKeyPath: AbsolutePathFromConfigPath(viper.GetString("tls_key_path")),
TLSCertPath: headscale.AbsolutePathFromConfigPath(viper.GetString("tls_cert_path")),
TLSKeyPath: headscale.AbsolutePathFromConfigPath(viper.GetString("tls_key_path")),
TLSClientAuthMode: tlsClientAuthMode,

DNSConfig: dnsConfig,
Expand Down Expand Up @@ -436,7 +422,7 @@ func getHeadscaleApp() (*headscale.Headscale, error) {
// We are doing this here, as in the future could be cool to have it also hot-reload

if cfg.ACL.PolicyPath != "" {
aclPath := AbsolutePathFromConfigPath(cfg.ACL.PolicyPath)
aclPath := headscale.AbsolutePathFromConfigPath(cfg.ACL.PolicyPath)
err = app.LoadACLPolicy(aclPath)
if err != nil {
log.Fatal().
Expand Down
16 changes: 16 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
Expand Down Expand Up @@ -334,3 +337,16 @@ func IsStringInSlice(slice []string, str string) bool {

return false
}

func AbsolutePathFromConfigPath(path string) string {
// If a relative path is provided, prefix it with the the directory where
// the config file was found.
if (path != "") && !strings.HasPrefix(path, string(os.PathSeparator)) {
dir, _ := filepath.Split(viper.ConfigFileUsed())
if dir != "" {
path = filepath.Join(dir, path)
}
}

return path
}

0 comments on commit 36dca35

Please sign in to comment.