-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
116 lines (100 loc) · 3.36 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package domain
import (
"strings"
"github.com/danilovalente/golangspell/appcontext"
"github.com/danilovalente/golangspell/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
//Flag s defines special behaviors and configurations to the commands
type Flag struct {
Name string `json:"name"`
Shorthand string `json:"shorthand"`
Value string `json:"value"`
Usage string `json:"usage"`
}
//Command is an available command in a specific Spell (plugin)
type Command struct {
Name string `json:"name"`
ShortDescription string `json:"shortDescription"`
LongDescription string `json:"longDescription"`
Flags map[string]*Flag `json:"flags"`
ValidArgs []string `json:"validArgs"`
}
//RunCommandFunction specifies a function for running a command
type RunCommandFunction func(cmd *cobra.Command, args []string)
//CobraCommand creates a cobra.Command from the domain.Command specification
func (command *Command) CobraCommand(runCommandFunction RunCommandFunction) *cobra.Command {
spellCMD := &cobra.Command{
Use: command.Name,
Short: command.ShortDescription,
Long: command.LongDescription,
Run: runCommandFunction,
ValidArgs: command.ValidArgs,
}
for _, flag := range command.Flags {
spellCMD.PersistentFlags().AddFlag(&pflag.Flag{Name: flag.Name, Shorthand: flag.Shorthand, Usage: flag.Usage})
}
return spellCMD
}
//Spell maps a Golangspell plugin
type Spell struct {
Name string `json:"name"`
URL string `json:"url"`
Commands map[string]*Command `json:"commands"`
Installed bool `json:"installed"`
}
//URLToPackage returns the package name referenced by the URL
func (spell *Spell) URLToPackage() string {
return strings.ReplaceAll(strings.ReplaceAll(spell.URL, "http://", ""), "https://", "")
}
//Config holds the Golangspell tool configuration
type Config struct {
Author string `json:"author"`
License string `json:"license"`
DefaultSpells []GolangLibrary `json:"defaultSpells"`
Spellbook map[string]*Spell `json:"spellbook"`
}
/*
ConfigRepository defines the repository capabilities that should be found in a Repository implementation for Config
*/
type ConfigRepository interface {
appcontext.Component
Get() (*Config, error)
Save(config *Config) (string, error)
}
//BuildDefaultConfig used to bootstrap application at first execution
func BuildDefaultConfig() Config {
return Config{
Author: config.Author,
License: config.UserLicense,
DefaultSpells: []GolangLibrary{
{
URL: "https://github.com/danilovalente/golangspell-core", Name: "golangspell-core",
},
},
}
}
//InitConfig lazily loads a Config
func InitConfig() appcontext.Component {
configRepository := appcontext.Current.Get(appcontext.ConfigRepository).(ConfigRepository)
config, err := configRepository.Get()
if err != nil {
panic(err)
}
return config
}
//GetConfig from the Current Application Context
func GetConfig() *Config {
return appcontext.Current.Get(appcontext.Config).(*Config)
}
//GetConfigRepository from the Current Application Context
func GetConfigRepository() ConfigRepository {
return appcontext.Current.Get(appcontext.ConfigRepository).(ConfigRepository)
}
func init() {
if config.Values.TestRun {
return
}
appcontext.Current.Add(appcontext.Config, InitConfig)
}