Skip to content

Commit b62fac2

Browse files
committed
Fix homedir issue
1 parent c5a2362 commit b62fac2

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

cmd/candy/cmd/root.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,33 @@ var (
2525
)
2626

2727
func init() {
28-
var err error
29-
homeDir, err = userHomeDir()
30-
if err != nil {
31-
candy.Log().Fatal("error getting user home directory", zap.Error(err))
32-
}
33-
34-
rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(homeDir, ".candyconfig"), "Config file")
28+
rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(userHomeDir(), ".candyconfig"), "Config file")
3529
}
3630

37-
func userHomeDir() (string, error) {
31+
func userHomeDir() string {
32+
if homeDir != "" {
33+
return homeDir
34+
}
35+
3836
var (
3937
sudo = os.Getenv("SUDO_USER")
4038
euid = os.Geteuid()
39+
err error
4140
)
4241

4342
if sudo != "" && euid == 0 {
4443
u, err := user.Lookup(sudo)
4544
if err != nil {
46-
return "", nil
45+
candy.Log().Fatal("error looking up sudo user", zap.String("user", sudo), zap.Error(err))
4746
}
4847

49-
return u.HomeDir, nil
48+
return u.HomeDir
5049
}
5150

52-
homeDir, err := os.UserHomeDir()
51+
homeDir, err = os.UserHomeDir()
5352
if err != nil {
54-
return "", err
53+
candy.Log().Fatal("error getting user home directory", zap.Error(err))
5554
}
5655

57-
return homeDir, nil
56+
return homeDir
5857
}

cmd/candy/cmd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
}
3333

3434
func addServerFlags(cmd *cobra.Command) {
35-
cmd.Flags().String("host-root", filepath.Join(homeDir, ".candy"), "Path to the directory containing applications that will be served by Candy")
35+
cmd.Flags().String("host-root", filepath.Join(userHomeDir(), ".candy"), "Path to the directory containing applications that will be served by Candy")
3636
cmd.Flags().StringSlice("domain", defaultDomains, "The top-level domains for which Candy will respond to DNS queries")
3737
cmd.Flags().String("http-addr", ":28080", "The Proxy server HTTP address")
3838
cmd.Flags().String("https-addr", ":28443", "The Proxy server HTTPS address")
@@ -66,7 +66,7 @@ func startServer(c *cobra.Command, ctx context.Context) error {
6666
candy.Log().Info("using config", zap.Any("cfg", cfg))
6767

6868
if err := os.MkdirAll(cfg.HostRoot, 0o0755); err != nil {
69-
return fmt.Errorf("failed to create host directory: %w", err)
69+
return fmt.Errorf("failed to create host directory %s: %w", cfg.HostRoot, err)
7070
}
7171

7272
svr := server.New(cfg)

0 commit comments

Comments
 (0)