Skip to content

Commit 43219a7

Browse files
committed
Small refactoring (for #108)
1 parent 461ef72 commit 43219a7

4 files changed

Lines changed: 35 additions & 22 deletions

File tree

conf.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package main
1010

1111
import (
1212
"errors"
13-
"fmt"
1413
"io"
1514
"os"
1615
"path/filepath"
@@ -60,23 +59,15 @@ var Conf = Configuration{
6059

6160
// ConfLoad loads the program configuration
6261
func ConfLoad() error {
63-
// Obtain path to executable directory
64-
exepath, err := os.Executable()
65-
if err != nil {
66-
return fmt.Errorf("conf: %s", err)
67-
}
68-
69-
exepath = filepath.Dir(exepath)
70-
7162
// Build list of configuration files
7263
files := []string{
7364
filepath.Join(PathConfDir, ConfFileName),
74-
filepath.Join(exepath, ConfFileName),
65+
filepath.Join(PathExecutableDir, ConfFileName),
7566
}
7667

7768
// Load file by file
7869
for _, file := range files {
79-
err = confLoadInternal(file)
70+
err := confLoadInternal(file)
8071
if err != nil {
8172
return err
8273
}
@@ -86,12 +77,11 @@ func ConfLoad() error {
8677
quirksDirs := []string{
8778
PathLocalQuirksDir,
8879
PathGlobalQuirksDir,
89-
filepath.Join(exepath, "ipp-usb-quirks"),
80+
filepath.Join(PathExecutableDir, "ipp-usb-quirks"),
9081
}
9182

92-
if err == nil {
93-
Conf.Quirks, err = LoadQuirksSet(quirksDirs...)
94-
}
83+
var err error
84+
Conf.Quirks, err = LoadQuirksSet(quirksDirs...)
9585

9686
return err
9787
}

daemon.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ func CloseStdInOutErr() error {
4343

4444
// Daemon runs ipp-usb program in background
4545
func Daemon() error {
46-
// Obtain path to program's executable
47-
exe, err := os.Executable()
48-
if err != nil {
49-
return err
50-
}
51-
5246
// Create stdout/stderr pipes
5347
rstdout, wstdout, err := os.Pipe()
5448
if err != nil {
@@ -82,7 +76,7 @@ func Daemon() error {
8276
}
8377

8478
// Start new process
85-
proc, err := os.StartProcess(exe, args, attr)
79+
proc, err := os.StartProcess(PathExecutableFile, args, attr)
8680
if err != nil {
8781
return err
8882
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func printStatus() {
176176
func main() {
177177
var err error
178178

179+
// Initialize paths
180+
err = PathsInit()
181+
InitLog.Check(err)
182+
179183
// Parse arguments
180184
params := parseArgv()
181185

paths.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package main
1010

1111
import (
12+
"fmt"
1213
"os"
1314
"path/filepath"
1415
)
@@ -38,6 +39,14 @@ var (
3839

3940
// Directory that contains per-device state files
4041
PathDevStateDir = DefaultPathDevStateDir
42+
43+
// Path to the program's executable file.
44+
// Initialized by PathInit()
45+
PathExecutableFile string
46+
47+
// Path to the directory that contains the executable file.
48+
// Initialized by PathInit()
49+
PathExecutableDir string
4150
)
4251

4352
// Default paths:
@@ -77,6 +86,22 @@ const (
7786
DefaultPathMainLogFile = DefaultPathLogDir + "/main.log"
7887
)
7988

89+
// PathsInit initializes paths handling.
90+
func PathsInit() error {
91+
// Initialize PathExecutableFile and PathExecutableDir
92+
var err error
93+
PathExecutableFile, err = os.Executable()
94+
if err != nil {
95+
err = fmt.Errorf(
96+
"Error getting path to the executable file: %s", err)
97+
return err
98+
}
99+
100+
PathExecutableDir = filepath.Dir(PathExecutableFile)
101+
102+
return nil
103+
}
104+
80105
// MakeDirectory creates a directory, specified by the path,
81106
// along with any necessary parents.
82107
//

0 commit comments

Comments
 (0)