-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathconfig.go
149 lines (134 loc) · 4.67 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright © 2023-present, Meta Platforms, Inc. and affiliates
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
// 'go lint': need blank import for embedding default config
"bytes"
"io"
// needed for embedded filesystem
_ "embed"
"fmt"
"os"
"path/filepath"
"github.com/facebookincubator/ttpforge/pkg/logging"
"github.com/facebookincubator/ttpforge/pkg/repos"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)
// TestConfig is used to pass test-specific settings to BuildRootCommand
// its entries are copied into the global config object
type TestConfig struct {
// used for capturing output in tests
// note: these are presently only supported by the `run`
// command because they are passed to lower layers through
// TTPExecutionContext
Stdout io.Writer
Stderr io.Writer
}
// Config stores the variables from the TTPForge global config file
// we export it for use in tests, but packages besides `cmd` probably
// should not touch it
type Config struct {
RepoSpecs []repos.Spec `yaml:"repos"`
repoCollection repos.RepoCollection
cfgFile string
testCfg *TestConfig
}
var (
//go:embed default-config.yaml
defaultConfigContents string
defaultConfigFileName = "config.yaml"
defaultResourceDir = ".ttpforge"
logConfig logging.Config
)
func getDefaultConfigFilePath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
defaultConfigPath := filepath.Join(homeDir, defaultResourceDir, defaultConfigFileName)
return defaultConfigPath, nil
}
// loadRepoCollection verifies that all repositories specified
// in the configuration file are present on the filesystem
// and clones missing ones if needed
func (cfg *Config) loadRepoCollection() (repos.RepoCollection, error) {
// locate our config file directory to expend config-relative paths
var basePath string
if cfg.cfgFile != "" {
cfgFileAbsPath, err := filepath.Abs(cfg.cfgFile)
if err != nil {
return nil, err
}
basePath = filepath.Dir(cfgFileAbsPath)
}
fsys := afero.NewOsFs()
return repos.NewRepoCollection(fsys, cfg.RepoSpecs, basePath)
}
// save() writes the current config back to its file - used by `install“ command
func (cfg *Config) save() error {
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)
err := yamlEncoder.Encode(&cfg)
if err != nil {
return fmt.Errorf("marshalling config failed: %v", err)
}
// YAML won't add this stylistic choice so we do it ourselves
cfgStr := "---\n" + b.String()
err = os.WriteFile(cfg.cfgFile, []byte(cfgStr), 0)
return err
}
func (cfg *Config) init() error {
// if no config file was specified, look for the default
// unless we are running as part of a unit test
if cfg.cfgFile == "" && cfg.testCfg == nil {
defaultConfigFilePath, err := getDefaultConfigFilePath()
if err != nil {
return fmt.Errorf("could not lookup default config file path: %v", err)
}
exists, err := afero.Exists(afero.NewOsFs(), defaultConfigFilePath)
if err != nil {
return fmt.Errorf("could not check existence of file %v: %v", defaultConfigFilePath, err)
}
if exists {
cfg.cfgFile = defaultConfigFilePath
} else {
logging.L().Warn("No config file specified and default configuration file not found!")
logging.L().Warn("You probably want to run `ttpforge init`!")
logging.L().Warn("However, if you know what you are doing, then carry on :)")
}
}
// load config file if we found one
if cfg.cfgFile != "" {
cfgContents, err := os.ReadFile(cfg.cfgFile)
if err != nil {
return err
}
if err = yaml.Unmarshal(cfgContents, cfg); err != nil {
return err
}
}
var err error
if cfg.repoCollection, err = cfg.loadRepoCollection(); err != nil {
return err
}
// setup logging
return logging.InitLog(logConfig)
}