This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
67 lines (50 loc) · 1.23 KB
/
Copy pathconfig.go
File metadata and controls
67 lines (50 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/myyra/hrflow/hrflow"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
type config struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
func configPath() (string, error) {
dir := os.Getenv("HOME")
if dir == "" {
return "", errors.New("$HOME is not defined")
}
configFile := filepath.Join(dir, ".hrflow")
return configFile, nil
}
func checkConfig(c *cli.Context) error {
configFile, err := configPath()
if err != nil {
return errors.Wrap(err, "getting config path")
}
info, err := os.Stat(configFile)
if os.IsNotExist(err) || info.IsDir() {
return fmt.Errorf("can't find config file at %s", configFile)
}
return nil
}
func clientFromConfig() (*hrflow.Client, error) {
configPath, err := configPath()
if err != nil {
return nil, errors.Wrap(err, "getting config path")
}
configFile, err := os.Open(configPath)
if err != nil {
return nil, errors.Wrap(err, "opening config file")
}
defer configFile.Close()
var cfg config
err = yaml.NewDecoder(configFile).Decode(&cfg)
if err != nil {
return nil, errors.Wrap(err, "decoding config")
}
return hrflow.NewClient(cfg.Username, cfg.Password), nil
}