Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pkg/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package config

import (
"os"
"os/user"
"path/filepath"
)
Expand All @@ -31,17 +32,35 @@ type Root struct {
}

func NewRoot() (*Root, error) {
user, err := user.Current()
homeDir, err := userHomeDir()
if err != nil {
return nil, err
}

return &Root{
StorageDir: filepath.Join(user.HomeDir, ".modctl"),
StorageDir: filepath.Join(homeDir, ".modctl"),
Pprof: false,
PprofAddr: "localhost:6060",
DisableProgress: false,
LogDir: filepath.Join(user.HomeDir, ".modctl/logs"),
LogDir: filepath.Join(homeDir, ".modctl/logs"),
LogLevel: "info",
}, nil
}

// userHomeDir returns the home directory of the current user. It prefers
// os.UserHomeDir, which honors $HOME (%USERPROFILE% on Windows), so that an
// overridden home directory is respected. os/user.Current queries the user
// database, which may ignore $HOME, so it is only used as a fallback when the
// environment variable is not set.
func userHomeDir() (string, error) {
if dir, err := os.UserHomeDir(); err == nil {
return dir, nil
}

u, err := user.Current()
if err != nil {
return "", err
}

return u.HomeDir, nil
}
51 changes: 51 additions & 0 deletions pkg/config/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 The ModelPack Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"os/user"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewRootRespectsHomeEnv(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
// os.UserHomeDir reads USERPROFILE on Windows.
t.Setenv("USERPROFILE", home)

root, err := NewRoot()
require.NoError(t, err)
assert.Equal(t, filepath.Join(home, ".modctl"), root.StorageDir)
assert.Equal(t, filepath.Join(home, ".modctl", "logs"), root.LogDir)
}

func TestNewRootFallsBackToCurrentUser(t *testing.T) {
u, err := user.Current()
require.NoError(t, err)

t.Setenv("HOME", "")
t.Setenv("USERPROFILE", "")

root, err := NewRoot()
require.NoError(t, err)
assert.Equal(t, filepath.Join(u.HomeDir, ".modctl"), root.StorageDir)
assert.Equal(t, filepath.Join(u.HomeDir, ".modctl", "logs"), root.LogDir)
}
Loading