-
Notifications
You must be signed in to change notification settings - Fork 27
/
global.go
100 lines (88 loc) · 2.66 KB
/
global.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
95
96
97
98
99
100
package go2chef
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"errors"
"log"
"os"
)
var (
// AutoRegisterPlugins is a central place for plugins to check
// whether they should auto-register. Normally they should.
AutoRegisterPlugins = true
// EarlyLogger is the logger used for pre-config logging. You can
// substitute it if your use case requires.
EarlyLogger = log.New(os.Stderr, "GO2CHEF ", log.LstdFlags)
)
var (
// ErrConfigHasNoNameKey is thrown when a config block doesn't have a `name` key
ErrConfigHasNoNameKey = errors.New("config map has no `name` key")
// ErrConfigHasNoTypeKey is thrown when a config block doesn't have a `type` key
ErrConfigHasNoTypeKey = errors.New("config map has no `type` key")
)
// GetNameType gets the name and type attributes from a config map
func GetNameType(config map[string]interface{}) (string, string, error) {
cname, err := GetName(config)
if err != nil {
return "", "", ErrConfigHasNoNameKey
}
ctype, err := GetType(config)
if err != nil {
return "", "", ErrConfigHasNoTypeKey
}
return cname, ctype, nil
}
// GetName gets the name attribute from a config map
func GetName(config map[string]interface{}) (string, error) {
cname, ok := config["name"]
if !ok {
return "", ErrConfigHasNoNameKey
}
if _, ok := cname.(string); !ok {
return "", ErrConfigHasNoNameKey
}
return cname.(string), nil
}
// GetType gets the type attribute from a config map
func GetType(config map[string]interface{}) (string, error) {
ctype, ok := config["type"]
if !ok {
return "", ErrConfigHasNoTypeKey
}
if _, ok := ctype.(string); !ok {
return "", ErrConfigHasNoTypeKey
}
return ctype.(string), nil
}
// ErrComponentDoesNotExist represents errors where a requested component (plugin)
// hasn't been registered.
type ErrComponentDoesNotExist struct {
Component string
}
// Error returns the error string
func (e *ErrComponentDoesNotExist) Error() string {
return "component " + e.Component + " does not exist"
}
// ErrChefAlreadyInstalled represents errors where Chef is already installed and
// provides a mechanism to pass metadata regarding the installed and requested
// versions back up the error chain.
type ErrChefAlreadyInstalled struct {
Installed string
Requested string
}
// Error returns the error string
func (e *ErrChefAlreadyInstalled) Error() string {
return "Package is already installed: " + e.Installed + ", requested " + e.Requested
}
// PathExists returns whether the given file or directory exists
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}