This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
94 lines (79 loc) · 2.15 KB
/
config.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"github.com/apex/log"
arlo "github.com/jeffreydwalter/arlo-go"
"github.com/phayes/permbits"
"gopkg.in/AlecAivazis/survey.v1"
yaml "gopkg.in/yaml.v2"
)
var conf = &Config{}
type Config struct {
Username string `yaml:"username" survey:"username"`
Password string `yaml:"password" survey:"password"`
}
func readConfig(path string) error {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return errors.New("file does not exist; have you run \"arlo-dl setup\"?")
}
return err
}
if perms := permbits.FileMode(fi.Mode()); perms != 0o600 {
logger.WithFields(log.Fields{
"path": path,
"mode": perms,
}).Warn("permissions of config file are insecure, please use 0600")
}
b, err := os.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(b, conf)
if err != nil {
return err
}
return nil
}
type CommandSetup struct{}
func (cmd *CommandSetup) Execute(_ []string) error {
login:
questions := []*survey.Question{
{
Name: "username",
Prompt: &survey.Input{Message: "What is your Arlo username/email?"},
Validate: survey.Required,
},
{
Name: "password",
Prompt: &survey.Password{Message: "What is your Arlo password?"},
Validate: survey.Required,
},
}
err := survey.Ask(questions, conf)
if err != nil {
return err
}
logger.Info("validating login")
if _, err = arlo.Login(conf.Username, conf.Password); err != nil {
logger.WithError(err).Error("failed to login, please enter new credentials")
goto login
}
f, err := os.OpenFile(cli.Flags.ConfigFile, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
logger.Fatalf("error creating %q: %v", cli.Flags.ConfigFile, err)
}
defer f.Close()
_, err = fmt.Fprintf(f, "---\nusername: %s\npassword: %s\n", conf.Username, conf.Password)
if err != nil {
logger.Fatalf("error writing to %q: %v", cli.Flags.ConfigFile, err)
}
logger.WithField("config", cli.Flags.ConfigFile).Info("successfully wrote config")
return nil
}