-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathKeyManager.go
More file actions
90 lines (76 loc) · 2 KB
/
KeyManager.go
File metadata and controls
90 lines (76 loc) · 2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
Lux
Copyright (C) 2022 Jack Devey
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package keymanager
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
// ApiKey stores the user's
// API key.
type ApiKey struct {
Key string `json:"key"`
}
// Store the current key to
// the user's storage
func (k *ApiKey) Store() {
dir, _ := os.UserHomeDir()
path := filepath.Join(dir, ".config", "lux")
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.MkdirAll(path, os.ModePerm)
}
filePath := filepath.Join(path, "key.json")
var f *os.File
if _, err := os.Stat(filePath); os.IsNotExist(err) {
f, _ = os.Create(filePath)
} else {
f, _ = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644)
}
b, _ := json.Marshal(&k)
_, e := f.Write(b)
if e != nil {
print(e.Error())
}
_ = f.Sync()
}
// Extract the key from the user's
// storage
func (k *ApiKey) Extract() {
dir, _ := os.UserHomeDir()
filePath := filepath.Join(dir, ".config", "lux", "key.json")
b, _ := ioutil.ReadFile(filePath)
_ = json.Unmarshal(b, &k)
}
// HasKey check if the software is setup
func HasKey() bool {
return GetAPIKey() != ""
}
// PrintLuxHasAPIKey uses LuxHasAPIKey to
// print an error if the user has no api key.
// DOES NOT PRINT IS BUG NEEDS FIX
func PrintLuxHasAPIKey() bool {
if !HasKey() {
return false
}
return true
}
// GetAPIKey allows packages to
// request the api key
func GetAPIKey() string {
var key ApiKey
key.Extract()
return key.Key
}